upb: JSON encoder leaks the Any arena when encoding fails

Open Beginner friendly
#31,031 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
83/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
c
Domain
backend

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

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from protocolbuffers/protobuf

All issues in protocolbuffers/protobuf

Similar issues

More C++ issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.