imbajin commented on code in PR #771:
URL: 
https://github.com/apache/hugegraph-toolchain/pull/771#discussion_r4195708930


##########
hugegraph-client/src/main/java/org/apache/hugegraph/serializer/BigDecimalSerializer.java:
##########
@@ -0,0 +1,70 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with this
+ * work for additional information regarding copyright ownership. The ASF
+ * licenses this file to You under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.hugegraph.serializer;
+
+import java.io.IOException;
+import java.math.BigDecimal;
+
+import org.apache.hugegraph.structure.constant.DataType;
+
+import com.fasterxml.jackson.core.JsonGenerator;
+import com.fasterxml.jackson.databind.SerializerProvider;
+import com.fasterxml.jackson.databind.ser.std.StdSerializer;
+
+/**
+ * Write a BigDecimal as a plain JSON number ("1.10", never "1.1E+2").
+ * Jackson's default is the scientific form of BigDecimal.toString(); the
+ * plain form carries every digit, so a server that reads fractions as
+ * BigDecimal (apache/hugegraph#3209) stores a DECIMAL value exactly, and a
+ * server that reads them as double behaves as it always did. The value stays
+ * a number, so numeric keys (DOUBLE, FLOAT, LONG, INT) accept it too.
+ */
+public class BigDecimalSerializer extends StdSerializer<BigDecimal> {
+
+    private static final long serialVersionUID = 1L;
+
+    public BigDecimalSerializer() {
+        super(BigDecimal.class);
+    }
+
+    @Override
+    public void serialize(BigDecimal value, JsonGenerator generator,
+                          SerializerProvider provider) throws IOException {
+        generator.writeNumber(exactString(value));
+    }
+
+    /**
+     * The plain form while the value is within the DECIMAL bounds the server
+     * accepts, else the scientific form: still exact and still a JSON number,
+     * but a value such as 1E+999999999 is not expanded into a billion
+     * characters on the client before the server rejects it.
+     */
+    public static String exactString(BigDecimal value) {
+        int scale = value.scale();
+        // the plain form of a negative scale carries the trailing zeros as
+        // digits (1E+128 has precision 1 but 129 plain digits), and the
+        // server reads an integer token with up to DECIMAL_MAX_PRECISION
+        // digits; a positive scale stays within the scale bound
+        int plainDigits = scale < 0 ? value.precision() - scale : 
value.precision();

Review Comment:
   ⚠️ Blocking: yes. For a BigDecimal constructed with scale Integer.MIN_VALUE, 
precision() - scale overflows this int to a negative value, so the 128-digit 
check passes and toPlainString() attempts an expansion of roughly 2.1 billion 
digits. That can exhaust the client before the server rejects the value. Please 
calculate the projected length as long and reject out-of-bound negative scales 
before calling toPlainString(). Evidence: exactString() uses the int 
subtraction here and selects toPlainString() immediately below.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to