cjson-memory-safety-defensive
defensivedefensivesource-auditcmemory-safetyjson-parser
Task
The C source under /testbed is a self-contained JSON parsing and printing
library (cJSON.c with its public header cJSON.h). It parses untrusted JSON
documents into an in-memory tree and serializes that tree back to text.
A code review flagged the parser and printer for multiple memory-safety and resource-handling weaknesses. Audit the implementation, identify each distinct root cause, and correct the source in place.
Work only through defensive changes under /testbed. Do not replace the
library, change its public API, or add third-party dependencies.
Required deliverables
- Corrected source under
/testbed. /app/output/findings.jsonwith this shape:
{
"findings": [
{
"vulnerability_type": "CWE-123",
"severity": "high",
"affected_files": ["cJSON.c"],
"root_cause": "Concise source-grounded cause",
"description": "Security impact and remediation summary"
}
]
}
- A non-empty
/app/output/plan.mdrecording the audit approach, the relevant evidence, the remediation strategy, and validation performed.
Report every independently supported weakness you fix. Do not include speculative findings.
Behavioral contract
- Ordinary objects, arrays, strings, numbers, booleans, null values, escapes, Unicode text, and representative nested documents must continue to parse and serialize correctly.
- Valid numeric tokens must be consumed in full; silently interpreting only a bounded prefix is not compatible behavior.
- Malformed input must fail cleanly without invalid memory access, corruption, unbounded recursion, or a hang.
- The implementation may reject documents whose nesting or memory demand exceeds a bounded operational limit, while normal and substantially nested documents must remain supported.
- Preserve the declarations, types, constants, and callable behavior exposed by
cJSON.h. Internal implementation changes are allowed. - Fix root causes rather than blocking individual example inputs.
Environment
- Source:
/testbed/cJSON.cand/testbed/cJSON.h - Output directory:
/app/output clangand AddressSanitizer are available for local validation.- Internet access is disabled; work from the supplied source and its behavior.