DaveGamble / cJSON

Ultralightweight JSON parser in ANSI C
MIT License
10.28k stars 3.15k forks source link

Bug in print_string_ptr() increment is 4 instead of 5 #862

Closed mbeynon closed 1 month ago

mbeynon commented 1 month ago

The sprintf() call before this uses a format string of "u%04x", which is a u followed by 4 hex chars. This means it always prints 5 chars. The increment on this line only increments by 4 instead of 5, which seems to be a bug by inspection.

https://github.com/DaveGamble/cJSON/blob/324973008ced4ea03d1626a00915d0399ecbd9db/cJSON.c#L1021

Thank you

PeterAlfredLee commented 1 month ago

Well, I do not think this is a bug. Please notice there is a input_pointer++ in the for loop here : https://github.com/DaveGamble/cJSON/blob/master/cJSON.c#L984

    for (input_pointer = input; *input_pointer != '\0'; (void)input_pointer++, output_pointer++)

Which means it will be 4 + 1 = 5 chars.

mbeynon commented 1 month ago

Ah yes, you are correct. That code only needs to increment output_pointer for anything beyond the single character that the loop already handles.

My mistake ... sorry for the noise.