upb: JSON encoder leaks the Any arena when encoding fails
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 83/100
Research direction
Start in upb/json/encode.c at upb_JsonEncoder_Encode(), where the issue identifies the error branch that returns after setjmp and the arena cleanup on success. Check how the encoder’s arena is created and freed, then exercise a failed encode involving Any under LeakSanitizer. Done means the error still returns and the arena is freed on failure.
Written by the indexing model from the issue text.
Description
What version of protobuf and what language are you using?
Version: v36.2, and main as of 2026-10-25 (upb/json/encode.c)
Language: C (upb), found through upb's Ruby amalgamation in an R binding
What did you do?
Encoded to JSON, with upb_JsonEncode(), a message in which encoding fails after a google.protobuf.Any has made the encoder's arena: an Any whose packed bytes do not decode as the packed type, or any later error in the message once an Any has been encoded (an out-of-range Duration after it, say). Built with ASan's LeakSanitizer. (An Any whose packed type is not in the DefPool fails before the arena is made, so it does not leak.)
What did you expect to see?
An error, and no memory left allocated.
What did you see instead?
ERROR: LeakSanitizer: detected memory leaks
in realloc ... upb_Arena_New ... jsonenc_arena ... jsonenc_any ... jsonenc_msgfield ...
jsonenc_arena() creates e->arena lazily for Any. upb_JsonEncoder_Encode() frees it on success, but its setjmp error branch returns without freeing it:
static size_t upb_JsonEncoder_Encode(jsonenc* const e, const upb_Message* const msg,
const upb_MessageDef* const m, const size_t size) {
if (UPB_SETJMP(e->err) != 0) return -1; // e->arena leaks here
jsonenc_msgfield(e, msg, m);
if (e->arena) upb_Arena_Free(e->arena);
return jsonenc_nullz(e, size);
}
So every failed JSON encode of such a message leaks the arena. A fix (the arena pointer is in *e, not a register-held local, so it is valid after the longjmp):
if (UPB_SETJMP(e->err) != 0) {
if (e->arena) upb_Arena_Free(e->arena);
return -1;
}
Anything else we should know about your project / environment
Found by libFuzzer with LeakSanitizer in zuproto, an R binding of upb, which carries this fix as a local patch. JSON serialization is outside the threat model in SECURITY.md, so this is a public issue.
- Dominant language
- C++
- Stars
- 72.2k
- Forks
- 16.4k
- Avg merge
- 1d 8h
- Merged PRs (30d)
- 342
Getting set up
- No Dockerfile or Docker Compose file
- No pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from protocolbuffers/protobuf
-
Difficulty 2/5 1-3 hours Newbie friendliness 77/100
protocolbuffers/protobuf#31768 ·
Maintainers usually reply within 1 day
-
feature request packaging & distribution python
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
protocolbuffers/protobuf#31654 ·
Maintainers usually reply within 1 day
-
google-protobuf 0.36.2-release fails to compile for ARMv7: @progbits rejected in global_asm!Possibly taken @farisbasha claimed this 1 day ago. Openbug help wanted rust
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
protocolbuffers/protobuf#31619 · 3 comments ·
Maintainers usually reply within 1 day
-
Null pointer passed to memcpy in lupb_decode() when the second argument is nilPossibly taken A pull request linked to this issue is open or already merged. Openbug help wanted lua
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
protocolbuffers/protobuf#31392 · 1 comment ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
protocolbuffers/protobuf#31032 ·
Maintainers usually reply within 1 day
All issues in protocolbuffers/protobuf
Similar issues
-
Needs: Triage :mag: Platform: Android
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
react/react-native#60167 ·
Maintainers usually reply within 2 days
-
documentation
Difficulty 2/5 1-3 hours Newbie friendliness 63/100
-
Difficulty 1/5 1-3 hours Newbie friendliness 88/100
Maintainers usually reply within 2 days
-
tests: ofx_render_test doesn't compile against FFmpeg 4.x (const AVCodec* to av_find_best_stream)Open
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
NatronGitHub/openfx-io#221 · 1 comment ·
Maintainers usually reply within 1 day
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
subsurface/subsurface#5145 ·
Maintainers usually reply within 2 days