DaveGamble / cJSON

Ultralightweight JSON parser in ANSI C
MIT License
10.68k stars 3.21k forks source link

decode_pointer_inplace is not working as it should #820

Open shlomo-margalit opened 8 months ago

shlomo-margalit commented 8 months ago

decode_pointer_inplace on the string abc~1xyz~0123 will produce the string abc~/xy~~01 instead of abc/xyz~123. also the function doesn't return a boolean to indicate that the operation failed, I don't know what the standard says about an invalid ponter in a patch, but I think it should fail the patch.

suggested solution: static BOOL decode_pointer_inplace(unsigned char string) { unsigned char decoded_string = string;

if (string == NULL) {
    return FALSE;
}

for (; *string; (void)decoded_string++, string++)
{
    if (string[0] == '~')
    {
        if (string[1] == '0')
        {
            decoded_string[0] = '~';
        }
        else if (string[1] == '1')
        {
            decoded_string[0] = '/';
        }
        else
        {
            /* invalid escape sequence */
            return FALSE;
        }

        string++;
    }
    else
    {
        decoded_string[0] = string[0];
    }
}

decoded_string[0] = '\0';
return TRUE;

}