GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — no overlap control in cJSON_SetValuestring · Issue #881 · DaveGamble/cJSON

no overlap control in cJSON_SetValuestring #881

New issue
Copy link
New issue
Copy link
Closed
Closed
no overlap control in cJSON_SetValuestring#881
#885 (+2)
Copy link

Description

@tregua87
tregua87
opened on Aug 9, 2024
Issue body actions

I discovered that the function cJSON_SetValuestring does not perform any control for string overlapping.
The problem is here :

if (strlen(valuestring) <= strlen(object->valuestring))
    {
        strcpy(object->valuestring, valuestring);
        return object->valuestring;
    }

strcpy requires that the two strings do not overlap .

In the following case, the second cJSON_SetValuestring tries to strcpy on the same string.

#include <cjson/cJSON.h>

#include <stdlib.h>
#include <stdint.h>

int main(int argc, char** argv) {
        
        cJSON *obj;
        cJSON *obj_dup;

        char* str;

        obj =  cJSON_Parse("\"fooz\"");
        
        obj_dup =  cJSON_Duplicate(obj, 1);
        if (obj_dup == 0) return 0;
        
        str =  cJSON_SetValuestring(obj_dup, "beeez");
        cJSON_SetValuestring(obj_dup, str); // ASan raises error here
        
        return 0;
}

A simple solution to this error is to add an overlapping check on the pointers, something like:

v1_len = strlen(valuestring);
v2_len = strlen(object->valuestring);
/* [X1, X2] [Y1, Y2] => X2 < Y1 or Y2 < X1 */
if (v1_len <= v2_len && ( valuestring + v1_len < object->valuestring || object->valuestring + v2_len < valuestring ))
/* if (strlen(valuestring) <= strlen(object->valuestring)) */
{
  strcpy(object->valuestring, valuestring);
  return object->valuestring;
}

Let me know if you agree with the problem and the solution. I can quickly prepare a PR.

Activity

  1. vwvw
    added a commit that references this issue on Aug 23, 2024

    fix DaveGamble#881, check overlap before calling strcpy in cJSON_SetV…

    This issue will close once commit d3b2452 is merged into the 'master' branch.
    d3b2452
  2. Alanscut
    closed this as completedin #885on Aug 30, 2024
  3. Alanscut
    added a commit that references this issue on Aug 30, 2024

    fix #881, check overlap before calling strcpy in cJSON_SetValuestring

    This issue will close once commit d6d5449 is merged into the 'master' branch.
    d6d5449
  4. imaami commented on Jan 19, 2025

    @imaami
    imaami
    on Jan 19, 2025
    More actions

    Adding a comment here as well although this is closed. There is no way in C to check whether or not unrelated addresses point to memory areas that overlap. It's simply UB. The test can't be implemented without violating the language standard.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402792 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.