Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Frozen json_setf truncates new added key #25

Open
Sorbier opened this issue Mar 12, 2018 · 2 comments
Open

Frozen json_setf truncates new added key #25

Sorbier opened this issue Mar 12, 2018 · 2 comments

Comments

@Sorbier
Copy link

Sorbier commented Mar 12, 2018

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
Copy link
Member

cpq commented Aug 22, 2018

PR please :)

@Deele
Copy link

Deele commented May 14, 2019

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants