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

Valgrind: invalid read after json_object_array_del_idx. #332

Closed
louandutoit opened this issue Jun 30, 2017 · 7 comments
Closed

Valgrind: invalid read after json_object_array_del_idx. #332

louandutoit opened this issue Jun 30, 2017 · 7 comments

Comments

@louandutoit
Copy link

louandutoit commented Jun 30, 2017

Hi,
I need to clear an array and add some new elements, but using json_object_array_del_idx is raising a possible issue, when adding new values after deleting.
Below is a snippet of code to reproduce the error. I can work around this by deleting the array object, and recreating, but that is a more time consuming process.

int32_t doSomeArrayStuff() {
	json_object * opContainer = json_object_new_object();
	json_object * opArray = json_object_new_array();
	json_object_array_add(opArray, json_object_new_string("string1"));
	json_object_array_add(opArray, json_object_new_string("string2")); //block allocated here
	json_object_array_add(opArray, json_object_new_string("string3")); //block allocated here
	json_object_array_add(opArray, json_object_new_string("string4"));
	json_object_object_add(opContainer, "myarray", opArray);

	if(json_object_array_del_idx(opArray, 0, 3) != 0) { //block free here
		return 1;
	}

	json_object_array_add(opArray, json_object_new_string("string5")); //invalid read of size 4
	json_object_array_add(opArray, json_object_new_string("string6")); //invalid read of size 4
	json_object_put(opContainer);

	return 0;
}

Running this through valgrind raises:
Invalid read of size 4

At the position as indicated in the comments. I'm using version "json-c 0.12.99".

@hawicz
Copy link
Member

hawicz commented Jul 9, 2017

hmm... your code looks fine, seems like a bug somewhere in json-c. I'll see what I can figure out.

hawicz added a commit that referenced this issue Jul 9, 2017
…would attempt to free previously free'd entries due to not checking the current array length.

Add a test that triggers the problem to ensure it stays fixed.
@hawicz
Copy link
Member

hawicz commented Jul 9, 2017

Sometimes you wonder how it ever worked. It should be fixed now, please try again with the most recent code.

@hawicz hawicz closed this as completed Jul 9, 2017
@louandutoit
Copy link
Author

Thanks

@elbaraka
Copy link

Hi I have exactely the same problem ! any solution please !

@hawicz
Copy link
Member

hawicz commented Jul 13, 2017

@elbaraka, what problem are you talking about? If you're still getting a valgrind error even after the fix from fd9b3b2 then please open an issue with the details.

@elbaraka
Copy link

I set this but always the same problem ! it destroys the structure of objects after deletion

@hawicz
Copy link
Member

hawicz commented Jul 14, 2017

I'll be happy to help you, but I don't understand what you're trying to explain. Please create a separate issue with an example of the problem that you are seeing.

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