DaveGamble/cJSON

decode_pointer_inplace is not working as it should

shlomo-margalit opened this issue · 0 comments

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;

}