Avoid automatic variables in functions using setjmp.

According to https://en.cppreference.com/w/c/program/setjmp automatic variables
modified in a function calling setjmp can have indeterminate values.  Instead,
refactor all functions calling setjmp so that the function calling setjmp
doesn’t have any local variables.

Part I: JSON encoder/decoder.  The code was previously in compliance, but a
convention of avoiding non-const local variables in functions calling setjmp
will make the compliance more obvious.

PiperOrigin-RevId: 502927863
pull/13171/head
Protobuf Team Bot 2 years ago committed by Copybara-Service
parent aa68739fa7
commit 2ab0cdbc69
  1. 13
      upb/json/decode.c
  2. 17
      upb/json/encode.c

@ -1446,6 +1446,14 @@ static void jsondec_wellknown(jsondec* d, upb_Message* msg,
}
}
static bool upb_JsonDecoder_Decode(jsondec* const d, upb_Message* const msg,
const upb_MessageDef* const m) {
if (UPB_SETJMP(d->err)) return false;
jsondec_tomsg(d, msg, m);
return true;
}
bool upb_JsonDecode(const char* buf, size_t size, upb_Message* msg,
const upb_MessageDef* m, const upb_DefPool* symtab,
int options, upb_Arena* arena, upb_Status* status) {
@ -1465,8 +1473,5 @@ bool upb_JsonDecode(const char* buf, size_t size, upb_Message* msg,
d.debug_field = NULL;
d.is_first = false;
if (UPB_SETJMP(d.err)) return false;
jsondec_tomsg(&d, msg, m);
return true;
return upb_JsonDecoder_Decode(&d, msg, m);
}

@ -775,6 +775,17 @@ static size_t jsonenc_nullz(jsonenc* e, size_t size) {
return ret;
}
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;
jsonenc_msgfield(e, msg, m);
if (e->arena) upb_Arena_Free(e->arena);
return jsonenc_nullz(e, size);
}
size_t upb_JsonEncode(const upb_Message* msg, const upb_MessageDef* m,
const upb_DefPool* ext_pool, int options, char* buf,
size_t size, upb_Status* status) {
@ -789,9 +800,5 @@ size_t upb_JsonEncode(const upb_Message* msg, const upb_MessageDef* m,
e.status = status;
e.arena = NULL;
if (UPB_SETJMP(e.err)) return -1;
jsonenc_msgfield(&e, msg, m);
if (e.arena) upb_Arena_Free(e.arena);
return jsonenc_nullz(&e, size);
return upb_JsonEncoder_Encode(&e, msg, m, size);
}

Loading…
Cancel
Save