cesanta/frozen

Frozen json_setf truncates new added key

Sorbier opened this issue · 2 comments

Hello,

below the code I'm running

strcpy( s_in, "{"Hello":1,"b":2}" );
printf( "Initial : %s\n", s_in ); // {"Hello":1,"b":2} => OK

struct json_out out = JSON_OUT_BUF( s_out, sizeof( s_out ) );
json_setf( s_in, sizeof( s_in ), &out, ".Hello", "7" );
strcpy( s_in, s_out );
printf( "Step 1 : %s\n", s_in ); // {"Hello":7,"b":2} => OK

out.u.buf.len = 0;
json_setf( s_in, sizeof( s_in ), &out, ".b", "8" );
strcpy( s_in, s_out );
printf( "Step 2 : %s\n", s_in ); // {"Hello":7,"b":8} => OK

out.u.buf.len = 0;
json_setf( s_in, sizeof( s_in ), &out, ".Goodbye", "10" ); // {"Hello":7,"b":8,"G":10} => G instead of Goodbye
strcpy( s_in, s_out );
printf( "Step 3 : %s\n", s_in );

On the latest block, trying to add Goodby key, and got G.

cpq commented

PR please :)

Deele commented

Another example:

	char *response;
	
	response = "{ \"result\": null }";
	printf( "initial response: [%s]\n", response);

	char tempResponse[65535];
	struct json_out jsonToOutputBuffer = JSON_OUT_BUF(tempResponse, sizeof(tempResponse));

	jsonToOutputBuffer.u.buf.len = 0;
	json_setf(response, strlen(response), &jsonToOutputBuffer, ".result", "OK");
	response = tempResponse;
	printf( "step 1 response: [%s]\n", response);

	jsonToOutputBuffer.u.buf.len = 0;
	json_setf(response, strlen(response), &jsonToOutputBuffer, ".foo", "bar");
	response = tempResponse;
	printf( "step 2 response: [%s]\n", response);

	jsonToOutputBuffer.u.buf.len = 0;
	json_setf(response, strlen(response), &jsonToOutputBuffer, ".sed", "rag");
	response = tempResponse;
	printf( "step 3 response: [%s]\n", response);

Produces result of:

initial response: [{ "result": null }]
step 1 response: [{ "result": "OK" }]
step 2 response: [{ "result": "OK,"foo":"bar","f]
step 3 response: [,"sed":"rag"]

Expected result was:

initial response: [{ "result": null }]
step 1 response: [{ "result": "OK" }]
step 2 response: [{ "result": "OK,"foo":"bar" }]
step 3 response: [{ "result": "OK,"foo":"bar","sed":"rag" }]

There is clearly an issue in code, that makes this library unusable.