From e6c38af972b9ba5fb4c567e85e3ece9209497941 Mon Sep 17 00:00:00 2001 From: Jie Luo Date: Fri, 6 Jan 2023 03:45:04 -0800 Subject: [PATCH] Expect fail when serialize inf and nan for Value.number_value in json format. fixes #11259 Implemented in java, c++, python and upb. Also added conformance test. https://developers.google.com/protocol-buffers/docs/reference/google.protobuf#google.protobuf.Value where it says: attempting to serialize NaN or Infinity results in error. (We can't serialize these as string "NaN" or "Infinity" values like we do for regular fields, because they would parse as string_value, not number_value). PiperOrigin-RevId: 500139380 --- upb/json/encode.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/upb/json/encode.c b/upb/json/encode.c index fe774f9a3e..752f543595 100644 --- a/upb/json/encode.c +++ b/upb/json/encode.c @@ -533,6 +533,12 @@ static void jsonenc_value(jsonenc* e, const upb_Message* msg, jsonenc_putstr(e, "null"); break; case 2: + if (upb_JsonEncode_HandleSpecialDoubles(e, val.double_val)) { + jsonenc_err( + e, + "google.protobuf.Value cannot encode double values for " + "infinity or nan, because they would be parsed as a string"); + } upb_JsonEncode_Double(e, val.double_val); break; case 3: