<p>quite sure I found it !</p>
<pre><code>/**
 * Initialize the relative weight distribution for a destination set
 * - fill the array of 0..99 elements where to keep the index of the
 *   destination address to be used. The Nth call will use
 *   the address with the index at possition N%100
 */
int dp_init_relative_weights(ds_set_t *dset)
{
   int j;
   int k;
   int t;

   if(dset == NULL || dset->dlist == NULL)
      return -1;

   lock_get(&dset->lock);
   int rw_sum = 0;
   /* find the sum of relative weights*/
   for(j = 0; j < dset->nr; j++) { // READING THE FLAG ONCE
      if(ds_skip_dst(dset->dlist[j].flags))
         continue;
      rw_sum += dset->dlist[j].attrs.rweight;
   }

   if(rw_sum == 0) {
      lock_release(&dset->lock);
      return 0;
   }

   /* fill the array based on the relative weight of each destination */
   t = 0;
   for(j = 0; j < dset->nr; j++) {
      if(ds_skip_dst(dset->dlist[j].flags)) // READING THE FLAG AGAIN, SEGFAULT IF THEY CHANGED !
         continue;

      int current_slice =
            dset->dlist[j].attrs.rweight * 100 / rw_sum; //truncate here;
      LM_DBG("rw_sum[%d][%d][%d]\n",j, rw_sum, current_slice);
      for(k = 0; k < current_slice; k++) {
         dset->rwlist[t] = (unsigned int)j;
         t++;
      }
   }

   /* if the array was not completely filled (i.e., the sum of rweights is
    * less than 100 due to truncated), then use last address to fill the rest */
   unsigned int last_insert =                                                                                                                                                                                                                                                      
         t > 0 ? dset->rwlist[t - 1] : (unsigned int)(dset->nr - 1);
   for(j = t; j < 100; j++)
      dset->rwlist[j] = last_insert;

   /* shuffle the content of the array in order to mix the selection
    * of the addresses (e.g., if first address has weight=20, avoid
    * sending first 20 calls to it, but ensure that within a 100 calls,
    * 20 go to first address */
   shuffle_uint100array(dset->rwlist);
   lock_release(&dset->lock);
   return 0;
}
</code></pre>
<pre><code>rw_sum[0][96][50]
rw_sum[1][96][50]
rw_sum[2][96][48]
redistributed array
0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|
fill the truncate t[148] with[2]
0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|
shuffled array
1|0|1|1|0|0|0|1|1|1|1|1|1|1|0|1|1|0|1|0|0|1|0|0|0|0|0|0|1|1|1|1|1|1|1|0|0|1|1|0|0|1|1|0|0|1|0|0|0|1|1|0|1|0|0|0|1|1|0|0|1|0|0|1|0|1|0|1|0|0|1|0|0|1|0|1|0|0|1|0|0|1|0|1|0|1|1|1|1|1|0|0|1|0|1|1|1|0|0|0|
*** stack smashing detected ***: ./bin/shuffle terminated
Aborted
</code></pre>

<p style="font-size:small;-webkit-text-size-adjust:none;color:#666;">—<br />You are receiving this because you are subscribed to this thread.<br />Reply to this email directly, <a href="https://github.com/kamailio/kamailio/issues/1649#issuecomment-423642232">view it on GitHub</a>, or <a href="https://github.com/notifications/unsubscribe-auth/AF36ZZuitC0B9GLZ6Mp9Vipc9UqqfmFBks5udToBgaJpZM4W0cas">mute the thread</a>.<img src="https://github.com/notifications/beacon/AF36ZVokmOodLDy81AJmkIDyeQcKS3z9ks5udToBgaJpZM4W0cas.gif" height="1" width="1" alt="" /></p>
<script type="application/json" data-scope="inboxmarkup">{"api_version":"1.0","publisher":{"api_key":"05dde50f1d1a384dd78767c55493e4bb","name":"GitHub"},"entity":{"external_key":"github/kamailio/kamailio","title":"kamailio/kamailio","subtitle":"GitHub repository","main_image_url":"https://assets-cdn.github.com/images/email/message_cards/header.png","avatar_image_url":"https://assets-cdn.github.com/images/email/message_cards/avatar.png","action":{"name":"Open in GitHub","url":"https://github.com/kamailio/kamailio"}},"updates":{"snippets":[{"icon":"PERSON","message":"@jchavanton in #1649: quite sure I found it !\r\n\r\n```\r\n/**\r\n * Initialize the relative weight distribution for a destination set\r\n * - fill the array of 0..99 elements where to keep the index of the\r\n *   destination address to be used. The Nth call will use\r\n *   the address with the index at possition N%100\r\n */\r\nint dp_init_relative_weights(ds_set_t *dset)\r\n{\r\n   int j;\r\n   int k;\r\n   int t;\r\n\r\n   if(dset == NULL || dset-\u003edlist == NULL)\r\n      return -1;\r\n\r\n   lock_get(\u0026dset-\u003elock);\r\n   int rw_sum = 0;\r\n   /* find the sum of relative weights*/\r\n   for(j = 0; j \u003c dset-\u003enr; j++) { // READING THE FLAG ONCE\r\n      if(ds_skip_dst(dset-\u003edlist[j].flags))\r\n         continue;\r\n      rw_sum += dset-\u003edlist[j].attrs.rweight;\r\n   }\r\n\r\n   if(rw_sum == 0) {\r\n      lock_release(\u0026dset-\u003elock);\r\n      return 0;\r\n   }\r\n\r\n   /* fill the array based on the relative weight of each destination */\r\n   t = 0;\r\n   for(j = 0; j \u003c dset-\u003enr; j++) {\r\n      if(ds_skip_dst(dset-\u003edlist[j].flags)) // READING THE FLAG AGAIN, SEGFAULT IF THEY CHANGED !\r\n         continue;\r\n\r\n      int current_slice =\r\n            dset-\u003edlist[j].attrs.rweight * 100 / rw_sum; //truncate here;\r\n      LM_DBG(\"rw_sum[%d][%d][%d]\\n\",j, rw_sum, current_slice);\r\n      for(k = 0; k \u003c current_slice; k++) {\r\n         dset-\u003erwlist[t] = (unsigned int)j;\r\n         t++;\r\n      }\r\n   }\r\n\r\n   /* if the array was not completely filled (i.e., the sum of rweights is\r\n    * less than 100 due to truncated), then use last address to fill the rest */\r\n   unsigned int last_insert =                                                                                                                                                                                                                                                      \r\n         t \u003e 0 ? dset-\u003erwlist[t - 1] : (unsigned int)(dset-\u003enr - 1);\r\n   for(j = t; j \u003c 100; j++)\r\n      dset-\u003erwlist[j] = last_insert;\r\n\r\n   /* shuffle the content of the array in order to mix the selection\r\n    * of the addresses (e.g., if first address has weight=20, avoid\r\n    * sending first 20 calls to it, but ensure that within a 100 calls,\r\n    * 20 go to first address */\r\n   shuffle_uint100array(dset-\u003erwlist);\r\n   lock_release(\u0026dset-\u003elock);\r\n   return 0;\r\n}\r\n```\r\n\r\n\r\n```\r\nrw_sum[0][96][50]\r\nrw_sum[1][96][50]\r\nrw_sum[2][96][48]\r\nredistributed array\r\n0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|\r\nfill the truncate t[148] with[2]\r\n0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|1|\r\nshuffled array\r\n1|0|1|1|0|0|0|1|1|1|1|1|1|1|0|1|1|0|1|0|0|1|0|0|0|0|0|0|1|1|1|1|1|1|1|0|0|1|1|0|0|1|1|0|0|1|0|0|0|1|1|0|1|0|0|0|1|1|0|0|1|0|0|1|0|1|0|1|0|0|1|0|0|1|0|1|0|0|1|0|0|1|0|1|0|1|1|1|1|1|0|0|1|0|1|1|1|0|0|0|\r\n*** stack smashing detected ***: ./bin/shuffle terminated\r\nAborted\r\n```"}],"action":{"name":"View Issue","url":"https://github.com/kamailio/kamailio/issues/1649#issuecomment-423642232"}}}</script>
<script type="application/ld+json">[
{
"@context": "http://schema.org",
"@type": "EmailMessage",
"potentialAction": {
"@type": "ViewAction",
"target": "https://github.com/kamailio/kamailio/issues/1649#issuecomment-423642232",
"url": "https://github.com/kamailio/kamailio/issues/1649#issuecomment-423642232",
"name": "View Issue"
},
"description": "View this Issue on GitHub",
"publisher": {
"@type": "Organization",
"name": "GitHub",
"url": "https://github.com"
}
},
{
"@type": "MessageCard",
"@context": "http://schema.org/extensions",
"hideOriginalBody": "false",
"originator": "AF6C5A86-E920-430C-9C59-A73278B5EFEB",
"title": "Re: [kamailio/kamailio] dispatcher algorithm 11 with congestion control memory corruption problem (#1649)",
"sections": [
{
"text": "",
"activityTitle": "**Julien Chavanton**",
"activityImage": "https://assets-cdn.github.com/images/email/message_cards/avatar.png",
"activitySubtitle": "@jchavanton",
"facts": [

]
}
],
"potentialAction": [
{
"name": "Add a comment",
"@type": "ActionCard",
"inputs": [
{
"isMultiLine": true,
"@type": "TextInput",
"id": "IssueComment",
"isRequired": false
}
],
"actions": [
{
"name": "Comment",
"@type": "HttpPOST",
"target": "https://api.github.com",
"body": "{\n\"commandName\": \"IssueComment\",\n\"repositoryFullName\": \"kamailio/kamailio\",\n\"issueId\": 1649,\n\"IssueComment\": \"{{IssueComment.value}}\"\n}"
}
]
},
{
"name": "Close issue",
"@type": "HttpPOST",
"target": "https://api.github.com",
"body": "{\n\"commandName\": \"IssueClose\",\n\"repositoryFullName\": \"kamailio/kamailio\",\n\"issueId\": 1649\n}"
},
{
"targets": [
{
"os": "default",
"uri": "https://github.com/kamailio/kamailio/issues/1649#issuecomment-423642232"
}
],
"@type": "OpenUri",
"name": "View on GitHub"
},
{
"name": "Unsubscribe",
"@type": "HttpPOST",
"target": "https://api.github.com",
"body": "{\n\"commandName\": \"MuteNotification\",\n\"threadId\": 382846636\n}"
}
],
"themeColor": "26292E"
}
]</script>