Repository navigation
Description
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:
- 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. - Top-level lists of
nulluse the marker0xDEADBEAF(elsewhere
0xDEADBEEF). The delete functions of top-level lists and maps of
nullfree the marker, which crashes. - Enum getters call
strcmponcJSON_GetStringValue(j), which is
NULLwhen the value is not a string, so a number in an array of
enums crashes. - Pattern checks ignore what
regcompreturns, and callregexecand
regfreeon a regex that did not compile. - 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.
{"type": "object", "additionalProperties": {"type": "integer"}}with{"a": 1, "b": 2}
(the same with"number","boolean"or an enum)- JSON input
[null, null], or the schema
{"type": "object", "additionalProperties": {"type": "null"}}with{"a": null} {"type": "object", "required": ["tags"], "properties": {"tags": {"type": "array", "items": {"enum": ["a", "b"]}}}}
with{"tags": [1]}{"type": "object", "required": ["s"], "properties": {"s": {"type": "string", "pattern": "^(?=a)a+$"}}}
with{"s": "aaa"}{"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:
-
./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]. -
ERROR: AddressSanitizer: SEGV on unknown address 0x0000deadbe9f(
0x0000deadbedffor the map) incJSON_free, called from
cJSON_DeleteTopLevel. -
ERROR: AddressSanitizer: SEGV on unknown address 0x000000000000 #0 ... in strcmp -
Exit status 1: the valid value is rejected.
regcompfails with
REG_BADRPT, and the generated code runs
regcomp(®ex, "^(?=a)a+$", REG_EXTENDED); if (regexec(®ex, ...))
anyway. -
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),NULLis stored.
Steps to Reproduce
node dist/index.js --src-lang schema --lang cjson --top-level TopLevel --src map-int.schema -o TopLevel.h
(or--src-lang jsonfor[null, null])- Copy
test/fixtures/cjson/main.cand the fixture'sdepsnext to
TopLevel.h, and compile as above. ./prog sample.json
Possible Solution
In packages/quicktype-core/src/language/CJSON/CJSONRenderer.ts:
- 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. - Use
0xDEADBEEF, and delete each element of a top-level list or map the
way a class's list or map deletes one (nothing fornull, and a check
for the marker around a nullable element). - Enum getters:
if (cJSON_IsString(j))instead ofif (NULL != j). A
value that is not a string then reads as0, as a string that is not a
case already does. Rejecting such values would be a further
improvement. The getter returns0for them, and lists, maps and unions
store that as if it were a case. - Patterns:
if (0 == regcomp(...)) { if (regexec(...)) { ... } regfree(®ex); }.
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 usingregcompremains. - Maps of classes and unions: store
validatedinstead 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