diff --git a/php/ext/google/protobuf/php-upb.c b/php/ext/google/protobuf/php-upb.c index 128821482c..a9301d28ce 100644 --- a/php/ext/google/protobuf/php-upb.c +++ b/php/ext/google/protobuf/php-upb.c @@ -7619,19 +7619,23 @@ static upb_DecodeStatus upb_Decoder_Decode(upb_Decoder* const decoder, return decoder->status; } +uint16_t upb_DecodeOptions_GetEffectiveMaxDepth(uint32_t options) { + uint16_t max_depth = upb_DecodeOptions_GetMaxDepth(options); + return max_depth ? max_depth : kUpb_WireFormat_DefaultDepthLimit; +} + upb_DecodeStatus upb_Decode(const char* buf, size_t size, upb_Message* msg, const upb_MiniTable* mt, const upb_ExtensionRegistry* extreg, int options, upb_Arena* arena) { UPB_ASSERT(!upb_Message_IsFrozen(msg)); upb_Decoder decoder; - unsigned depth = (unsigned)options >> 16; upb_EpsCopyInputStream_Init(&decoder.input, &buf, size, options & kUpb_DecodeOption_AliasString); decoder.extreg = extreg; - decoder.depth = depth ? depth : kUpb_WireFormat_DefaultDepthLimit; + decoder.depth = upb_DecodeOptions_GetEffectiveMaxDepth(options); decoder.end_group = DECODE_NOGROUP; decoder.options = (uint16_t)options; decoder.missing_required = false; @@ -8341,19 +8345,23 @@ static upb_EncodeStatus upb_Encoder_Encode(upb_encstate* const encoder, return encoder->status; } +uint16_t upb_EncodeOptions_GetEffectiveMaxDepth(uint32_t options) { + uint16_t max_depth = upb_EncodeOptions_GetMaxDepth(options); + return max_depth ? max_depth : kUpb_WireFormat_DefaultDepthLimit; +} + static upb_EncodeStatus _upb_Encode(const upb_Message* msg, const upb_MiniTable* l, int options, upb_Arena* arena, char** buf, size_t* size, bool prepend_len) { upb_encstate e; - unsigned depth = (unsigned)options >> 16; e.status = kUpb_EncodeStatus_Ok; e.arena = arena; e.buf = NULL; e.limit = NULL; e.ptr = NULL; - e.depth = depth ? depth : kUpb_WireFormat_DefaultDepthLimit; + e.depth = upb_EncodeOptions_GetEffectiveMaxDepth(options); e.options = options; _upb_mapsorter_init(&e.sorter); diff --git a/php/ext/google/protobuf/php-upb.h b/php/ext/google/protobuf/php-upb.h index 52940da9c7..c101c60208 100644 --- a/php/ext/google/protobuf/php-upb.h +++ b/php/ext/google/protobuf/php-upb.h @@ -5414,6 +5414,8 @@ UPB_INLINE uint16_t upb_DecodeOptions_GetMaxDepth(uint32_t options) { return options >> 16; } +uint16_t upb_DecodeOptions_GetEffectiveMaxDepth(uint32_t options); + // Enforce an upper bound on recursion depth. UPB_INLINE int upb_Decode_LimitDepth(uint32_t decode_options, uint32_t limit) { uint32_t max_depth = upb_DecodeOptions_GetMaxDepth(decode_options); @@ -5514,6 +5516,8 @@ UPB_INLINE uint16_t upb_EncodeOptions_GetMaxDepth(uint32_t options) { return options >> 16; } +uint16_t upb_EncodeOptions_GetEffectiveMaxDepth(uint32_t options); + // Enforce an upper bound on recursion depth. UPB_INLINE int upb_Encode_LimitDepth(uint32_t encode_options, uint32_t limit) { uint32_t max_depth = upb_EncodeOptions_GetMaxDepth(encode_options); diff --git a/ruby/ext/google/protobuf_c/ruby-upb.c b/ruby/ext/google/protobuf_c/ruby-upb.c index 747d85d396..35c858182e 100644 --- a/ruby/ext/google/protobuf_c/ruby-upb.c +++ b/ruby/ext/google/protobuf_c/ruby-upb.c @@ -7619,19 +7619,23 @@ static upb_DecodeStatus upb_Decoder_Decode(upb_Decoder* const decoder, return decoder->status; } +uint16_t upb_DecodeOptions_GetEffectiveMaxDepth(uint32_t options) { + uint16_t max_depth = upb_DecodeOptions_GetMaxDepth(options); + return max_depth ? max_depth : kUpb_WireFormat_DefaultDepthLimit; +} + upb_DecodeStatus upb_Decode(const char* buf, size_t size, upb_Message* msg, const upb_MiniTable* mt, const upb_ExtensionRegistry* extreg, int options, upb_Arena* arena) { UPB_ASSERT(!upb_Message_IsFrozen(msg)); upb_Decoder decoder; - unsigned depth = (unsigned)options >> 16; upb_EpsCopyInputStream_Init(&decoder.input, &buf, size, options & kUpb_DecodeOption_AliasString); decoder.extreg = extreg; - decoder.depth = depth ? depth : kUpb_WireFormat_DefaultDepthLimit; + decoder.depth = upb_DecodeOptions_GetEffectiveMaxDepth(options); decoder.end_group = DECODE_NOGROUP; decoder.options = (uint16_t)options; decoder.missing_required = false; @@ -8341,19 +8345,23 @@ static upb_EncodeStatus upb_Encoder_Encode(upb_encstate* const encoder, return encoder->status; } +uint16_t upb_EncodeOptions_GetEffectiveMaxDepth(uint32_t options) { + uint16_t max_depth = upb_EncodeOptions_GetMaxDepth(options); + return max_depth ? max_depth : kUpb_WireFormat_DefaultDepthLimit; +} + static upb_EncodeStatus _upb_Encode(const upb_Message* msg, const upb_MiniTable* l, int options, upb_Arena* arena, char** buf, size_t* size, bool prepend_len) { upb_encstate e; - unsigned depth = (unsigned)options >> 16; e.status = kUpb_EncodeStatus_Ok; e.arena = arena; e.buf = NULL; e.limit = NULL; e.ptr = NULL; - e.depth = depth ? depth : kUpb_WireFormat_DefaultDepthLimit; + e.depth = upb_EncodeOptions_GetEffectiveMaxDepth(options); e.options = options; _upb_mapsorter_init(&e.sorter); diff --git a/ruby/ext/google/protobuf_c/ruby-upb.h b/ruby/ext/google/protobuf_c/ruby-upb.h index 4db8083784..d8ccd46d3f 100755 --- a/ruby/ext/google/protobuf_c/ruby-upb.h +++ b/ruby/ext/google/protobuf_c/ruby-upb.h @@ -5416,6 +5416,8 @@ UPB_INLINE uint16_t upb_DecodeOptions_GetMaxDepth(uint32_t options) { return options >> 16; } +uint16_t upb_DecodeOptions_GetEffectiveMaxDepth(uint32_t options); + // Enforce an upper bound on recursion depth. UPB_INLINE int upb_Decode_LimitDepth(uint32_t decode_options, uint32_t limit) { uint32_t max_depth = upb_DecodeOptions_GetMaxDepth(decode_options); @@ -5516,6 +5518,8 @@ UPB_INLINE uint16_t upb_EncodeOptions_GetMaxDepth(uint32_t options) { return options >> 16; } +uint16_t upb_EncodeOptions_GetEffectiveMaxDepth(uint32_t options); + // Enforce an upper bound on recursion depth. UPB_INLINE int upb_Encode_LimitDepth(uint32_t encode_options, uint32_t limit) { uint32_t max_depth = upb_EncodeOptions_GetMaxDepth(encode_options);