Skip to content

Commit 3d065d1

Browse files
habermancopybara-github
authored andcommitted
Fixed depth limit check by comparing effective depth limits.
Before we were trying to work around the fact that we don't know the default depth limit. The logic is simpler and more robust if we take the default into account. PiperOrigin-RevId: 698856552
1 parent ced605d commit 3d065d1

File tree

4 files changed

+16
-4
lines changed

4 files changed

+16
-4
lines changed

upb/wire/decode.c

+6-2
Original file line numberDiff line numberDiff line change
@@ -1388,19 +1388,23 @@ static upb_DecodeStatus upb_Decoder_Decode(upb_Decoder* const decoder,
13881388
return decoder->status;
13891389
}
13901390

1391+
uint16_t upb_DecodeOptions_GetEffectiveMaxDepth(uint32_t options) {
1392+
uint16_t max_depth = upb_DecodeOptions_GetMaxDepth(options);
1393+
return max_depth ? max_depth : kUpb_WireFormat_DefaultDepthLimit;
1394+
}
1395+
13911396
upb_DecodeStatus upb_Decode(const char* buf, size_t size, upb_Message* msg,
13921397
const upb_MiniTable* mt,
13931398
const upb_ExtensionRegistry* extreg, int options,
13941399
upb_Arena* arena) {
13951400
UPB_ASSERT(!upb_Message_IsFrozen(msg));
13961401
upb_Decoder decoder;
1397-
unsigned depth = (unsigned)options >> 16;
13981402

13991403
upb_EpsCopyInputStream_Init(&decoder.input, &buf, size,
14001404
options & kUpb_DecodeOption_AliasString);
14011405

14021406
decoder.extreg = extreg;
1403-
decoder.depth = depth ? depth : kUpb_WireFormat_DefaultDepthLimit;
1407+
decoder.depth = upb_DecodeOptions_GetEffectiveMaxDepth(options);
14041408
decoder.end_group = DECODE_NOGROUP;
14051409
decoder.options = (uint16_t)options;
14061410
decoder.missing_required = false;

upb/wire/decode.h

+2
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ UPB_INLINE uint16_t upb_DecodeOptions_GetMaxDepth(uint32_t options) {
103103
return options >> 16;
104104
}
105105

106+
uint16_t upb_DecodeOptions_GetEffectiveMaxDepth(uint32_t options);
107+
106108
// Enforce an upper bound on recursion depth.
107109
UPB_INLINE int upb_Decode_LimitDepth(uint32_t decode_options, uint32_t limit) {
108110
uint32_t max_depth = upb_DecodeOptions_GetMaxDepth(decode_options);

upb/wire/encode.c

+6-2
Original file line numberDiff line numberDiff line change
@@ -668,19 +668,23 @@ static upb_EncodeStatus upb_Encoder_Encode(upb_encstate* const encoder,
668668
return encoder->status;
669669
}
670670

671+
uint16_t upb_EncodeOptions_GetEffectiveMaxDepth(uint32_t options) {
672+
uint16_t max_depth = upb_EncodeOptions_GetMaxDepth(options);
673+
return max_depth ? max_depth : kUpb_WireFormat_DefaultDepthLimit;
674+
}
675+
671676
static upb_EncodeStatus _upb_Encode(const upb_Message* msg,
672677
const upb_MiniTable* l, int options,
673678
upb_Arena* arena, char** buf, size_t* size,
674679
bool prepend_len) {
675680
upb_encstate e;
676-
unsigned depth = (unsigned)options >> 16;
677681

678682
e.status = kUpb_EncodeStatus_Ok;
679683
e.arena = arena;
680684
e.buf = NULL;
681685
e.limit = NULL;
682686
e.ptr = NULL;
683-
e.depth = depth ? depth : kUpb_WireFormat_DefaultDepthLimit;
687+
e.depth = upb_EncodeOptions_GetEffectiveMaxDepth(options);
684688
e.options = options;
685689
_upb_mapsorter_init(&e.sorter);
686690

upb/wire/encode.h

+2
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ UPB_INLINE uint16_t upb_EncodeOptions_GetMaxDepth(uint32_t options) {
5959
return options >> 16;
6060
}
6161

62+
uint16_t upb_EncodeOptions_GetEffectiveMaxDepth(uint32_t options);
63+
6264
// Enforce an upper bound on recursion depth.
6365
UPB_INLINE int upb_Encode_LimitDepth(uint32_t encode_options, uint32_t limit) {
6466
uint32_t max_depth = upb_EncodeOptions_GetMaxDepth(encode_options);

0 commit comments

Comments
 (0)