I have a function that parse a JSON text and return a new structure called EZapiEntry. When I check the value of result every thing is OK.
After using this function in an athor c file. I get wrong results.
Code of the function:
EZapiEntry parseEntry()
{
EZapiEntry result;
json_t *entryJson;
entryJson = json_object_get(root,"data");
unsigned int i=0;
EZuc8* maskString=json_string_value(json_object_get(entryJson,"mask"));
result.uiKeySize = json_integer_value(json_object_get(entryJson,"keySize"));
result.uiResultSize = json_integer_value(json_object_get(entryJson,"resultSize"));
EZuc8 val1[result.uiKeySize];
hexStringToBytes(json_string_value(json_object_get(entryJson,"key")),val1);
result.pucKey = val1;
EZuc8 val2[result.uiResultSize];
hexStringToBytes(json_string_value(json_object_get(entryJson,"result")),val2);
result.pucResult = val2;
EZuc8 val3[strlen(maskString)];
hexStringToBytes(maskString,val3);
result.pucMask = val3;
result.uiProfile = json_integer_value(json_object_get(entryJson,"profile"));
printf("\nkeySize : %u ",result.uiKeySize);
printf("\nResultSize : %u ",result.uiResultSize);
printf("\nkey : ");
for (i = 0 ; i <result.uiKeySize ; i++)
{
printf("%02x",result.pucKey[i]);
}
printf("\nresult : ");
for (i = 0 ; i <result.uiResultSize ; i++)
{
printf("%02x",result.pucResult[i]);
}
printf("\nmask : ");
for (i = 0 ; i <strlen(maskString) ; i++)
{
printf("%02x",result.pucMask[i]);
}
printf("\nprofile : ");
printf("%u",result.uiProfile);
return result;
}
code of the use of the function :
entry=parseEntry();
printf("\nkeySize2 : %u ",entry.uiKeySize);
printf("\nResultSize2: %u ",entry.uiResultSize);
printf("\nkey2 : ");
for (i = 0 ; i <entry.uiKeySize ; i++)
{
printf("%02x",entry.pucKey[i]);
}
printf("\nresult2 : ");
for (i = 0 ; i <entry.uiResultSize ; i++)
{
printf("%02x",entry.pucResult[i]);
}
printf("\nprofile2 : ");
printf("%u",entry.uiProfile);
And this is the result :
keySize : 1
ResultSize : 16
key : 03
result : aaaaaa11445544ff00112233445544ff
mask : 0000000a
profile : 0
keySize2 : 1
ResultSize2: 16
key2 : bf
result2 : bfb985b0102e765c00112233445544ff
profile2 : 0
The first values for example Result1 is the correct one. The second is the wrong one
I am really stack, i can't find the problem ! Is there any Help ?
This will result in a dangling pointer after the function returns:
EZuc8 val2[result.uiResultSize];
hexStringToBytes(json_string_value(json_object_get(entryJson,"result")),val2);
result.pucResult = val2;
as val2
will no longer exist when parseEntry()
returns. Similar problem for val1
and val3
. Instead of using VLA malloc()
directly to the relevant member:
result.pucResult = malloc(sizeof(EZuc8) * result.uiResultSize);
hexStringToBytes(json_string_value(json_object_get(entryJson,"result")),
result.pucResult);
Remember to free()
the dynamically allocated memory.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.