Skip to content

Commit 804573f

Browse files
Track depth limit in UntypedMessage parsing.
PiperOrigin-RevId: 569162263
1 parent f5d9c6f commit 804573f

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

src/google/protobuf/json/internal/untyped_message.cc

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,10 @@ PROTOBUF_NOINLINE static absl::Status MakeInvalidLengthDelimType(
197197
field_number));
198198
}
199199

200+
PROTOBUF_NOINLINE static absl::Status MakeTooDeepError() {
201+
return absl::InvalidArgumentError("allowed depth exceeded");
202+
}
203+
200204
absl::Status UntypedMessage::Decode(io::CodedInputStream& stream,
201205
absl::optional<int32_t> current_group) {
202206
while (true) {
@@ -447,6 +451,9 @@ absl::Status UntypedMessage::Decode32Bit(io::CodedInputStream& stream,
447451

448452
absl::Status UntypedMessage::DecodeDelimited(io::CodedInputStream& stream,
449453
const ResolverPool::Field& field) {
454+
if (!stream.IncrementRecursionDepth()) {
455+
return MakeTooDeepError();
456+
}
450457
auto limit = stream.ReadLengthAndPushLimit();
451458
if (limit == 0) {
452459
return MakeUnexpectedEofError();
@@ -510,7 +517,7 @@ absl::Status UntypedMessage::DecodeDelimited(io::CodedInputStream& stream,
510517
break;
511518
}
512519
}
513-
stream.PopLimit(limit);
520+
stream.DecrementRecursionDepthAndPopLimit(limit);
514521
return absl::OkStatus();
515522
}
516523

0 commit comments

Comments
 (0)