Skip to content

Commit 482752a

Browse files
mkruskal-googlecopybara-github
authored andcommitted
Make Python/C++ reject unmatched end-group tag.
This brings it into conformance with our spec and other languages. PiperOrigin-RevId: 702914786
1 parent ab3adc2 commit 482752a

File tree

3 files changed

+8
-17
lines changed

3 files changed

+8
-17
lines changed

conformance/failure_list_python_cpp.txt

-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,3 @@
77
# TODO: insert links to corresponding bugs tracking the issue.
88
# Should we use GitHub issues or the Google-internal bug tracker?
99
Required.*.JsonInput.Int32FieldQuotedExponentialValue.* # Failed to parse input or produce output.
10-
Required.*.ProtobufInput.UnmatchedEndGroup # Should have failed to parse, but didn't.
11-
Required.*.ProtobufInput.UnmatchedEndGroupUnknown # Should have failed to parse, but didn't.
12-
Required.*.ProtobufInput.UnmatchedEndGroupWithData # Should have failed to parse, but didn't.
13-
Required.*.ProtobufInput.UnmatchedEndGroupWrongType # Should have failed to parse, but didn't.

python/google/protobuf/internal/message_test.py

+5-9
Original file line numberDiff line numberDiff line change
@@ -69,16 +69,12 @@ def testParseErrors(self, message_module):
6969
msg = message_module.TestAllTypes()
7070
self.assertRaises(TypeError, msg.FromString, 0)
7171
self.assertRaises(Exception, msg.FromString, '0')
72-
# TODO: Fix cpp extension to raise error instead of warning.
73-
# b/27494216
7472
end_tag = encoder.TagBytes(1, 4)
75-
if (api_implementation.Type() == 'python' or
76-
api_implementation.Type() == 'upb'):
77-
with self.assertRaises(message.DecodeError) as context:
78-
msg.FromString(end_tag)
79-
if api_implementation.Type() == 'python':
80-
# Only pure-Python has an error message this specific.
81-
self.assertEqual('Unexpected end-group tag.', str(context.exception))
73+
with self.assertRaises(message.DecodeError) as context:
74+
msg.FromString(end_tag)
75+
if api_implementation.Type() != 'upb':
76+
# upb raises a less specific exception.
77+
self.assertRegex(str(context.exception), 'Unexpected end-group tag.*')
8278

8379
# Field number 0 is illegal.
8480
self.assertRaises(message.DecodeError, msg.FromString, b'\3\4')

python/google/protobuf/pyext/message.cc

+3-4
Original file line numberDiff line numberDiff line change
@@ -1900,10 +1900,9 @@ static PyObject* MergeFromString(CMessage* self, PyObject* arg) {
19001900
// ctx has an explicit limit set (length of string_view), so we have to
19011901
// check we ended at that limit.
19021902
if (!ctx.EndedAtLimit()) {
1903-
// TODO: Raise error and return NULL instead.
1904-
// b/27494216
1905-
PyErr_Warn(nullptr, "Unexpected end-group tag: Not all data was converted");
1906-
return PyLong_FromLong(data.len - ctx.BytesUntilLimit(ptr));
1903+
PyErr_Format(DecodeError_class,
1904+
"Unexpected end-group tag: Not all data was converted");
1905+
return nullptr;
19071906
}
19081907
return PyLong_FromLong(data.len);
19091908
}

0 commit comments

Comments
 (0)