From 8630cc09933db9b6816a530ba2d2345f54f1d006 Mon Sep 17 00:00:00 2001 From: zjncs <18910855655@163.com> Date: Fri, 4 Sep 2026 12:36:41 +0800 Subject: [PATCH] fix(common): parse +Inf/-Inf as real infinity in StrBuffer.parseDouble StrBuffer.parseDouble returned the raw IEEE-754 bit-pattern constants 0x7FF0000000000000L / 0xFFF0000000000000L widened to double (9.2e18 / -4.5e15) instead of real infinities for the Prometheus text format special values +inf/-inf. OnlineParser.toDouble already maps them to Double.POSITIVE_INFINITY/NEGATIVE_INFINITY; align StrBuffer (used by TextParser) with that behavior and drop the +/-Inf exclusion from the cross-parser test that papered over the divergence. Signed-off-by: zjncs <18910855655@163.com> --- .../collect/prometheus/parser/OnlineParserTest.java | 7 ++----- .../java/org/apache/hertzbeat/common/util/StrBuffer.java | 4 ++-- .../org/apache/hertzbeat/common/util/StrBufferTest.java | 8 ++++---- 3 files changed, 8 insertions(+), 11 deletions(-) diff --git a/hertzbeat-collector/hertzbeat-collector-basic/src/test/java/org/apache/hertzbeat/collector/collect/prometheus/parser/OnlineParserTest.java b/hertzbeat-collector/hertzbeat-collector-basic/src/test/java/org/apache/hertzbeat/collector/collect/prometheus/parser/OnlineParserTest.java index 370ac9a3bd..51ca94a5e5 100644 --- a/hertzbeat-collector/hertzbeat-collector-basic/src/test/java/org/apache/hertzbeat/collector/collect/prometheus/parser/OnlineParserTest.java +++ b/hertzbeat-collector/hertzbeat-collector-basic/src/test/java/org/apache/hertzbeat/collector/collect/prometheus/parser/OnlineParserTest.java @@ -108,11 +108,8 @@ class OnlineParserTest { MetricFamily metricFamily1 = metricFamilyMap1.get(metricFamilyName); Set metricValueSet = metricFamily2.getMetricList().stream().map(MetricFamily.Metric::getValue).collect(Collectors.toSet()); metricFamily1.getMetricList().forEach(metric -> { - // this is for something different between two algorithms above, and both of them is current on this parsing behavior. - if (!(metric.getValue() == Double.POSITIVE_INFINITY || metric.getValue() == Double.NEGATIVE_INFINITY)) { - if (!metricValueSet.contains(metric.getValue())) { - fail(); - } + if (!metricValueSet.contains(metric.getValue())) { + fail(); } }); }); diff --git a/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/util/StrBuffer.java b/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/util/StrBuffer.java index c15bc33594..d58fe09a11 100644 --- a/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/util/StrBuffer.java +++ b/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/util/StrBuffer.java @@ -153,9 +153,9 @@ public class StrBuffer { */ public static double parseDouble(String s) { if (POSITIVE_INF.equalsIgnoreCase(s)) { - return POSITIVE_INF_VALUE; + return Double.POSITIVE_INFINITY; } else if (NEGATIVE_INF.equalsIgnoreCase(s)) { - return NEGATIVE_INF_VALUE; + return Double.NEGATIVE_INFINITY; } return Double.parseDouble(s); } diff --git a/hertzbeat-common-core/src/test/java/org/apache/hertzbeat/common/util/StrBufferTest.java b/hertzbeat-common-core/src/test/java/org/apache/hertzbeat/common/util/StrBufferTest.java index 98a5a93341..0bade03c1d 100644 --- a/hertzbeat-common-core/src/test/java/org/apache/hertzbeat/common/util/StrBufferTest.java +++ b/hertzbeat-common-core/src/test/java/org/apache/hertzbeat/common/util/StrBufferTest.java @@ -91,10 +91,10 @@ class StrBufferTest { assertEquals(123.45, buffer.toDouble()); buffer = new StrBuffer("+inf"); - assertEquals(POSITIVE_INF_VALUE, buffer.toDouble()); + assertEquals(Double.POSITIVE_INFINITY, buffer.toDouble()); buffer = new StrBuffer("-inf"); - assertEquals(NEGATIVE_INF_VALUE, buffer.toDouble()); + assertEquals(Double.NEGATIVE_INFINITY, buffer.toDouble()); } @Test @@ -144,8 +144,8 @@ class StrBufferTest { void testParseDouble() { assertEquals(123.45, StrBuffer.parseDouble("123.45")); - assertEquals(POSITIVE_INF_VALUE, StrBuffer.parseDouble("+inf")); - assertEquals(NEGATIVE_INF_VALUE, StrBuffer.parseDouble("-inf")); + assertEquals(Double.POSITIVE_INFINITY, StrBuffer.parseDouble("+inf")); + assertEquals(Double.NEGATIVE_INFINITY, StrBuffer.parseDouble("-inf")); } }