<p>Continued from issue:<br>
<a class="issue-link js-issue-link" data-error-text="Failed to load issue title" data-id="338383079" data-permission-text="Issue title is private" data-url="https://github.com/kamailio/kamailio/issues/1583" data-hovercard-type="issue" data-hovercard-url="/kamailio/kamailio/issues/1583/hovercard" href="https://github.com/kamailio/kamailio/issues/1583">#1583</a></p>
<p>Say you have a kamailio config file with variables ordered as A(int),B(int),C(string),D(int) – (all belonging to the same group).<br>
Kamailio first creates a linked list of these variables in a given group and calculates the group size as follows <a href="https://github.com/kamailio/kamailio/blob/master/src/core/cfg/cfg_script.c#L34">actual code</a>):</p>
<pre><code>Pseudo-code
---------------------------------------------------------------------------------------------
group->size = 0
group->vars = null // the pointer to the head of the linked list of variables
create_group_linked_list:
for variable in group:
if variable is integer:
round up group_size to the nearest 4 byte multiple
group_size += sizeof(int)
if variable is string:
round up group_size to the nearest 8 byte multiple
group_size += sizeof(str) //str is a kamailio type (size 16 in our C++ compiler)
if group->vars is null:
group->vars = variable
else
variables->next = group->vars
group->vars = variable
---------------------------------------------------------------------------------------------
Linked list creation
NULL
group->size 0
A(int)
group->size 4
B(int) -> A
group->size 8
C(string) -> B -> A
group->size 24
D(int) -> C -> B -> A
group->size 28
</code></pre>
<p>However, this linked list is not used as is. it is copied to a single shared memory block for all child processes.<br>
This is done in the <a href="https://github.com/kamailio/kamailio/blob/master/src/core/cfg/cfg_struct.c#L180">cfg_shmize()</a> which further calls <a href="https://github.com/kamailio/kamailio/blob/master/src/core/cfg/cfg_script.c#L222">cfg_script_fixup()</a><br>
Basically, the function goes through the linked list of variables created above and calculates the offset of each variable in the memory block assuming that the offset of the first variable is 0.<br>
Following is the pseudo-code of what cfg_script_fixup() does for the linked list and offset calculation:</p>
<pre><code>Pseudo-code
--------------------------------------------------------------------------------------------
offset = 0
node = group->vars:
while node != null:
if node's value is int:
round up offset to nearest multiple of 4
node->offset = offset
offset += sizeof(int)
if node's value is str:
round up offset to nearest multiple of 8
node->offset = offset
offset += sizeof(str)
node = node -> next
--------------------------------------------------------------------------------------------
linked list (from above):
D(int) -> C(string) -> B(int) -> A(int)
offset=0
D
D->offset = 0
offset = 4
C
C->offset = 8
offset = 24
B
B->offset = 24
offset = 28
A
A->offset = 28
offset = 32 (which would be the actual group size)
</code></pre>
<p>So, as visible, <strong>the order in which the linked list is being processed on the two cases is causing issues</strong> (from A to D in one case and from D to A in the next case).<br>
while the group size calculation code block leads to a group size of 28, the group size according to the second code block would be 32 and the offset of the last variable would we 28. This would lead to the last variable (all 4 bytes) getting corrupted.</p>
<p>My temporary solution is to recalculate the group size every time a variable is added in the first code block by re-traversing the linked list but is definitely not the ideal solution.</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/1679">view it on GitHub</a>, or <a href="https://github.com/notifications/unsubscribe-auth/AF36ZUmHld6HkYIF55OeGjGYjcXTiyVRks5ulkZcgaJpZM4Xh_w1">mute the thread</a>.<img src="https://github.com/notifications/beacon/AF36ZU3FIt7v32T0I8Ll7h3EDh1UTCUlks5ulkZcgaJpZM4Xh_w1.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 (#1679)"}],"action":{"name":"View Issue","url":"https://github.com/kamailio/kamailio/issues/1679"}}}</script>
<script type="application/ld+json">[
{
"@context": "http://schema.org",
"@type": "EmailMessage",
"potentialAction": {
"@type": "ViewAction",
"target": "https://github.com/kamailio/kamailio/issues/1679",
"url": "https://github.com/kamailio/kamailio/issues/1679",
"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 (#1679)",
"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": 1679
}
]
}
],
"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\": 1679,\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\": 1679\n}"
},
{
"targets": [
{
"os": "default",
"uri": "https://github.com/kamailio/kamailio/issues/1679"
}
],
"@type": "OpenUri",
"name": "View on GitHub"
},
{
"name": "Unsubscribe",
"@type": "HttpPOST",
"target": "https://api.github.com",
"body": "{\n\"commandName\": \"MuteNotification\",\n\"threadId\": 394787893\n}"
}
],
"themeColor": "26292E"
}
]</script>