Skip to content

Double-free when printing cJSON_Raw if valuestring==NULL #241

Closed
@projectgus

Description

@projectgus

I was taking a look at the cJSON codebase on behalf of a customer, and I a spot that I wanted to check when printing cJSON_Raw values:

  • If item->valuestring is NULL, the print_value() implementation may deallocate the write buffer and then return. However, at least some callers of print_value() (for example print()) will also deallocate this buffer on failure. This looks like a potential double-free.

I haven't had time to be verify this for certain, or write a test, but I thought I would report it while I had it in front of me. Seems like it's probably an unlikely path to hit in real code.

EDIT: Previous version of this issue had two points. For one, I missed that the memcpy included the terminating byte.

Activity

changed the title Memory issues when printing cJSON_Raw Double-free when printing cJSON_Raw if valuestring==NULL on Feb 7, 2018
FSMaxB

FSMaxB commented on Feb 7, 2018

@FSMaxB
Collaborator

You are right. All callers of print_value deallocate the buffer on failure, except in cJSON_PrintPreallocated, where there are no allocations anyway.

I will fix this and make a new release immediately.

FSMaxB

FSMaxB commented on Feb 7, 2018

@FSMaxB
Collaborator

Thanks a lot for reviewing cJSON!

FSMaxB

FSMaxB commented on Feb 7, 2018

@FSMaxB
Collaborator

1.7.3 has been released with a fix.

projectgus

projectgus commented on Feb 8, 2018

@projectgus
Author

Wow, that's great. Thanks for the quick response @FSMaxB !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

      Participants

      @projectgus@FSMaxB

      Issue actions

        Double-free when printing cJSON_Raw if valuestring==NULL · Issue #241 · DaveGamble/cJSON