From 353b4e003183d88e1e95776046c463461d59f6db Mon Sep 17 00:00:00 2001 From: Thomas Van Lenten Date: Fri, 3 May 2024 08:13:17 -0700 Subject: [PATCH] Ensure overflow values for float/double map to inf/-inf. PiperOrigin-RevId: 630393984 --- conformance/text_format_conformance_suite.cc | 21 ++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/conformance/text_format_conformance_suite.cc b/conformance/text_format_conformance_suite.cc index 3eca94304c..6025199991 100644 --- a/conformance/text_format_conformance_suite.cc +++ b/conformance/text_format_conformance_suite.cc @@ -481,6 +481,27 @@ void TextFormatConformanceTestSuiteImpl::RunAllTests() { ExpectParseFailure("FloatFieldNoOctal", REQUIRED, "optional_float: 012"); ExpectParseFailure("FloatFieldNoNegativeOctal", REQUIRED, "optional_float: -012"); + // https://protobuf.dev/reference/protobuf/textformat-spec/#value says + // overflows are mapped to infinity/-infinity. + RunValidTextFormatTestWithExpected("FloatFieldOverflowInfinity", REQUIRED, + "optional_float: 1e50", + "optional_float: inf"); + RunValidTextFormatTestWithExpected("FloatFieldOverflowNegativeInfinity", + REQUIRED, "optional_float: -1e50", + "optional_float: -inf"); + RunValidTextFormatTestWithExpected("DoubleFieldOverflowInfinity", REQUIRED, + "optional_double: 1e9999", + "optional_double: inf"); + RunValidTextFormatTestWithExpected("DoubleFieldOverflowNegativeInfinity", + REQUIRED, "optional_double: -1e9999", + "optional_double: -inf"); + // Exponent is one more than uint64 max. + RunValidTextFormatTestWithExpected( + "FloatFieldOverflowInfinityHugeExponent", REQUIRED, + "optional_float: 1e18446744073709551616", "optional_float: inf"); + RunValidTextFormatTestWithExpected( + "DoubleFieldOverflowInfinityHugeExponent", REQUIRED, + "optional_double: 1e18446744073709551616", "optional_double: inf"); // String literals x {Strings, Bytes} for (const auto& field_type : std::vector{"String", "Bytes"}) {