From ee5aa499afe9fce27ff0cab69d184b541ee56f29 Mon Sep 17 00:00:00 2001 From: Sandy Zhang Date: Tue, 15 Oct 2024 16:39:56 -0700 Subject: [PATCH] Fix unexpected NumberFormatException in Durations.parse() by replacing with documented thrown ParseException. This fixes parsing of invalid second value long to throw the correct exception. Most users should already be handling ParseExceptions e.g. for invalid nanos PiperOrigin-RevId: 686281774 --- .../src/main/java/com/google/protobuf/util/Durations.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/java/util/src/main/java/com/google/protobuf/util/Durations.java b/java/util/src/main/java/com/google/protobuf/util/Durations.java index 05e0e217f4..2d5be34718 100644 --- a/java/util/src/main/java/com/google/protobuf/util/Durations.java +++ b/java/util/src/main/java/com/google/protobuf/util/Durations.java @@ -237,7 +237,12 @@ public final class Durations { nanoValue = secondValue.substring(pointPosition + 1); secondValue = secondValue.substring(0, pointPosition); } - long seconds = Long.parseLong(secondValue); + long seconds; + try { + seconds = Long.parseLong(secondValue); + } catch (NumberFormatException e) { + throw new ParseException("Invalid duration string: " + value, 0); + } int nanos = nanoValue.isEmpty() ? 0 : Timestamps.parseNanos(nanoValue); if (seconds < 0) { throw new ParseException("Invalid duration string: " + value, 0);