Skip to content

[BUG]: C (cJSON) output crashes or does not compile for top-level maps and lists, non-string enum values and some patterns #3552

Open

Description

@EloB

Issue Type

quicktype output (generated C code that does not compile, crashes, or has
undefined behavior).

Context (Environment, Version, Language)

Input Format: JSON Schema (and JSON)
Output Language: C (cJSON)

CLI, npm, or app.quicktype.io: CLI, built from master
Version: 24.0.0 (master at c39af7d)

Description

Found while porting quicktype to Rust (quickertype). The cJSON renderer
generates code with five separate problems. They are listed together
because they are all in CJSONRenderer.ts and one pull request fixes
them, but each one can be checked on its own:

  1. A top-level map of integers, doubles, booleans or enums passes the
    value itself where a pointer is expected, so it does not compile (with
    Apple clang 21). The map's values are not type-checked either, unlike
    the list's.
  2. Top-level lists of null use the marker 0xDEADBEAF (elsewhere
    0xDEADBEEF). The delete functions of top-level lists and maps of
    null free the marker, which crashes.
  3. Enum getters call strcmp on cJSON_GetStringValue(j), which is
    NULL when the value is not a string, so a number in an array of
    enums crashes.
  4. Pattern checks ignore what regcomp returns, and call regexec and
    regfree on a regex that did not compile.
  5. Maps of classes or unions parse every value twice: they parse it,
    delete the result and parse it again.

Input Data

All runs use quicktype's test driver test/fixtures/cjson/main.c (which
parses the file it is given, prints it and deletes it) and the libraries
from the cjson fixture's setup command.

  1. {"type": "object", "additionalProperties": {"type": "integer"}} with {"a": 1, "b": 2}
    (the same with "number", "boolean" or an enum)
  2. JSON input [null, null], or the schema
    {"type": "object", "additionalProperties": {"type": "null"}} with {"a": null}
  3. {"type": "object", "required": ["tags"], "properties": {"tags": {"type": "array", "items": {"enum": ["a", "b"]}}}}
    with {"tags": [1]}
  4. {"type": "object", "required": ["s"], "properties": {"s": {"type": "string", "pattern": "^(?=a)a+$"}}}
    with {"s": "aaa"}
  5. {"type": "object", "required": ["m"], "properties": {"m": {"type": "object", "additionalProperties": {"$ref": "#/definitions/V"}}}, "definitions": {"V": {"type": "object", "required": ["n"], "properties": {"n": {"type": "integer"}}}}}

Expected Behaviour / Output

Every case compiles, runs without sanitizer reports and prints its input
back. A top-level map of integers rejects {"a": "x"}.

Current Behaviour / Output

Compiled with cc -g -O0 -fsanitize=address,undefined -isystem deps deps/cJSON.c deps/hashtable.c deps/list.c main.c:

  1. ./TopLevel.h:82:56: error: passing 'double' to parameter of incompatible type 'void *'
       82 |                     hashtable_add(x->value, e->string, cJSON_GetNumberValue(e), sizeof(int64_t *));
    

    For booleans and enums:
    error: incompatible integer to pointer conversion passing 'cJSON_bool' (aka 'int') to parameter of type 'void *' [-Wint-conversion].

  2. ERROR: AddressSanitizer: SEGV on unknown address 0x0000deadbe9f
    

    (0x0000deadbedf for the map) in cJSON_free, called from
    cJSON_DeleteTopLevel.

  3. ERROR: AddressSanitizer: SEGV on unknown address 0x000000000000
        #0 ... in strcmp
    
  4. Exit status 1: the valid value is rejected. regcomp fails with
    REG_BADRPT, and the generated code runs
    regcomp(&regex, "^(?=a)a+$", REG_EXTENDED); if (regexec(&regex, ...))
    anyway.

  5. The output is correct, but the generated code is

    struct V * validated = cJSON_GetVValue(e1);
    if (NULL == validated) { ... return NULL; }
    cJSON_DeleteV(validated);
    hashtable_add(x1, e1->string, cJSON_GetVValue(e1), sizeof(struct V *));

    The work doubles at every level of nested maps, and if the second
    parse fails (out of memory), NULL is stored.

Steps to Reproduce

  1. node dist/index.js --src-lang schema --lang cjson --top-level TopLevel --src map-int.schema -o TopLevel.h
    (or --src-lang json for [null, null])
  2. Copy test/fixtures/cjson/main.c and the fixture's deps next to
    TopLevel.h, and compile as above.
  3. ./prog sample.json

Possible Solution

In packages/quicktype-core/src/language/CJSON/CJSONRenderer.ts:

  1. Top-level getter: lists and maps add their elements the same way. A
    value is allocated, as the list already does
    (int64_t * item = cJSON_malloc(sizeof(*item)); ... hashtable_add(x->value, e->string, item, sizeof(*item));).
    A map's values are type-checked as a list's are. An element that cannot
    be allocated fails the parse instead of being left out.
  2. Use 0xDEADBEEF, and delete each element of a top-level list or map the
    way a class's list or map deletes one (nothing for null, and a check
    for the marker around a nullable element).
  3. Enum getters: if (cJSON_IsString(j)) instead of if (NULL != j). A
    value that is not a string then reads as 0, as a string that is not a
    case already does. Rejecting such values would be a further
    improvement. The getter returns 0 for them, and lists, maps and unions
    store that as if it were a case.
  4. Patterns: if (0 == regcomp(...)) { if (regexec(...)) { ... } regfree(&regex); }.
    JSON Schema patterns are ECMAScript regular expressions, and many
    (\d, lookahead) are not POSIX extended ones, so such a pattern is
    then not checked. That limitation of using regcomp remains.
  5. Maps of classes and unions: store validated instead of deleting it
    and parsing again.

Written by Claude (an AI assistant made by Anthropic) and posted on behalf of the account owner (EloB). The reproduction, fix and tests were checked by running them as described above.

Activity

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