<p>I was trying to do a cfg_rpc update on a variable and some of the times, the variable would take on randomly large values or negative values.</p>
<p>The -1 here is probably causing issues.</p>
<p><a href="https://github.com/kamailio/kamailio/blob/1d53ea3dba4e59b05b2e92ecc973c44159b9bb36/src/core/cfg/cfg_struct.c#L625">https://github.com/kamailio/kamailio/blob/1d53ea3dba4e59b05b2e92ecc973c44159b9bb36/src/core/cfg/cfg_struct.c#L625</a></p>
<p>Here's an explanation of what's going on (debugged using gdb). In cfg_struc.c if you put a breakpoint in the following line in cfg_clone_global</p>
<p><a href="https://github.com/kamailio/kamailio/blob/1d53ea3dba4e59b05b2e92ecc973c44159b9bb36/src/core/cfg/cfg_struct.c#L631">https://github.com/kamailio/kamailio/blob/1d53ea3dba4e59b05b2e92ecc973c44159b9bb36/src/core/cfg/cfg_struct.c#L631</a></p>
<p>checking variable values:</p>
<pre><code>--- Old (correct) value ---
 
 
(gdb) print sizeof(*(int *)(((unsigned char *)((*cfg_global)->vars + 984)) + 172))
$33 = 4
(gdb) print *((unsigned char *)&(*(int *)(((unsigned char *)((*cfg_global)->vars + 984)) + 172)) + 0)
$34 = 99 'c'
(gdb) print *((unsigned char *)&(*(int *)(((unsigned char *)((*cfg_global)->vars + 984)) + 172)) + 1)
$35 = 0 '\000'
(gdb) print *((unsigned char *)&(*(int *)(((unsigned char *)((*cfg_global)->vars + 984)) + 172)) + 2)
$36 = 0 '\000'
(gdb) print *((unsigned char *)&(*(int *)(((unsigned char *)((*cfg_global)->vars + 984)) + 172)) + 3)
$37 = 0 '\000'
 
 
--- new (corrupt) value ---
 
 
(gdb) print sizeof(*(int *)(((unsigned char *)((block)->vars + 984)) + 172))
$28 = 4
(gdb) print *((unsigned char *)&(*(int *)(((unsigned char *)((block)->vars + 984)) + 172)) + 0)
$29 = 99 'c'
(gdb) print *((unsigned char *)&(*(int *)(((unsigned char *)((block)->vars + 984)) + 172)) + 1)
$30 = 0 '\000'
(gdb) print *((unsigned char *)&(*(int *)(((unsigned char *)((block)->vars + 984)) + 172)) + 2)
$31 = 0 '\000'
(gdb) print *((unsigned char *)&(*(int *)(((unsigned char *)((block)->vars + 984)) + 172)) + 3)
$32 = 130 '\202'
</code></pre>
<p>As visible, the last byte is getting corrupted.<br>
I checked the memory allocation variables:</p>
<pre><code>--> cfg_block_size
 
 
(gdb) print cfg_block_size
$2 = 1156
 
 
--> sizeof(cfg_block_t)
 
 
print sizeof(cfg_block_t)
$15 = 8
</code></pre>
<p>This means that total memory assigned = 8 + 1156 -(1) = 1163</p>
<pre><code>--> address of new block
 
 
(gdb) print (void *) block
$12 = (void *) 0x7f63086b6758
 
 
--> address of the corrupted variable in the new block
 
 
(gdb) print (void *)(((unsigned char *)((block)->vars + 984)) + 172)
$13 = (void *) 0x7f63086b6be0
 
 
--> offset of the variable from the block start
 
 
(gdb) print 0x7f63086b6be0 - 0x7f63086b6758
$14 = 1160
</code></pre>
<p>since the variable is an integer, memory that should be assigned = 1160 + 4 = 1164<br>
However, we're assigning 1163.</p>
<p>Therefore the last byte is getting corrupted.</p>
<p>Does it make sense to remove the -1 from all the memory allocation in cfg_struct ?</p>

<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/1583">view it on GitHub</a>, or <a href="https://github.com/notifications/unsubscribe-auth/AF36ZYdyWBPtXf9KD9dSoGd-vi9O8yHAks5uDT4sgaJpZM4VDDlj">mute the thread</a>.<img src="https://github.com/notifications/beacon/AF36ZY231Y-g-aryKU-InSwSMiabDpxeks5uDT4sgaJpZM4VDDlj.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":"DESCRIPTION","message":"cfg_rpc updates changing variable values to large random numbers  (#1583)"}],"action":{"name":"View Issue","url":"https://github.com/kamailio/kamailio/issues/1583"}}}</script>
<script type="application/ld+json">[
{
"@context": "http://schema.org",
"@type": "EmailMessage",
"potentialAction": {
"@type": "ViewAction",
"target": "https://github.com/kamailio/kamailio/issues/1583",
"url": "https://github.com/kamailio/kamailio/issues/1583",
"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": "cfg_rpc updates changing variable values to large random numbers  (#1583)",
"sections": [
{
"text": "",
"activityTitle": "**vinesinha**",
"activityImage": "https://assets-cdn.github.com/images/email/message_cards/avatar.png",
"activitySubtitle": "@vinesinha",
"facts": [
{
"name": "Repository: ",
"value": "kamailio/kamailio"
},
{
"name": "Issue #: ",
"value": 1583
}
]
}
],
"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\": 1583,\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\": 1583\n}"
},
{
"targets": [
{
"os": "default",
"uri": "https://github.com/kamailio/kamailio/issues/1583"
}
],
"@type": "OpenUri",
"name": "View on GitHub"
},
{
"name": "Unsubscribe",
"@type": "HttpPOST",
"target": "https://api.github.com",
"body": "{\n\"commandName\": \"MuteNotification\",\n\"threadId\": 353122659\n}"
}
],
"themeColor": "26292E"
}
]</script>