Skip to content

HTTP: a JSON integer parameter beyond the int64 range silently wraps around (18446744073709551617 becomes 1), so a lookup by it returns the record whose id is 1 #9004

Closed

Description

@tae898

A JSON integer parameter that does not fit in a Java long is silently reduced to its low 64 bits. Through POST /api/v1/command, INSERT INTO T SET dec = :v with "v": 123456789012345678901234567890 stores -4362896299872285998 in the DECIMAL property, and "v": 18446744073709551617 (2^64 + 1) stores 1. A lookup SELECT id FROM T WHERE id = :id with "id": 18446744073709551617 returns the record whose id is 1. The same statements run embedded with a BigDecimal parameter store the exact values, and the embedded lookup returns no row.

A decimal with more digits than a double holds goes through double even when the target property is DECIMAL: "v": 1.23456789012345678901234567890 is stored as 1.2345678901234567. JSON numbers have no precision limit, and the server has the exact digits in hand when it parses them.

The wrap comes from JSONObject.elementToObject. It calls primitive.getAsLong() and expects a NumberFormatException for a number that does not fit, with a fallback to double, but Gson's LazilyParsedNumber.longValue() (gson 2.14.0 in the distribution) catches that exception itself and returns new BigDecimal(value).longValue(), which keeps the low 64 bits. The fallback never runs. The class is the engine's JSON parser, so the same happens embedded: new JSONObject("{\"x\":18446744073709551617}").toMap() answers {x=1}, and a document filled with fromJSON from the same text stores 1. The SQL parser does not share the problem: the literal SET x = 18446744073709551617 is refused with Invalid integer.

Repro

javac -cp 'lib/*' -d cls HttpBigNumberParam.java
java -Xmx4g -XX:+UseCompactObjectHeaders --add-modules=jdk.incubator.vector --enable-native-access=ALL-UNNAMED -cp 'lib/*:cls' HttpBigNumberParam /tmp/HttpBigNumberParam-root

lib/ holds the server's jars (engine, network, server, and their dependencies). The program starts the server in-process and talks to it with the JDK HTTP client. On Temurin 21, use the same command without -XX:+UseCompactObjectHeaders.

import com.arcadedb.ContextConfiguration;
import com.arcadedb.GlobalConfiguration;
import com.arcadedb.database.Database;
import com.arcadedb.query.sql.executor.ResultSet;
import com.arcadedb.schema.DocumentType;
import com.arcadedb.schema.Type;
import com.arcadedb.server.ArcadeDBServer;

import java.math.BigDecimal;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;
import java.util.Map;

public class HttpBigNumberParam {
  public static void main(String[] args) throws Exception {
    ContextConfiguration cfg = new ContextConfiguration();
    cfg.setValue(GlobalConfiguration.SERVER_ROOT_PASSWORD, "repro-password");
    cfg.setValue(GlobalConfiguration.SERVER_ROOT_PATH, args[0]);
    cfg.setValue(GlobalConfiguration.SERVER_DATABASE_DIRECTORY, args[0] + "/databases");
    ArcadeDBServer server = new ArcadeDBServer(cfg);
    server.start();
    System.out.println("ArcadeDB " + com.arcadedb.Constants.getRawVersion() + " (" + com.arcadedb.Constants.getBuildNumber() + "), Java "
        + System.getProperty("java.version"));
    Database db = server.getOrCreateDatabase("repro");
    db.transaction(() -> {
      DocumentType t = db.getSchema().createDocumentType("T");
      t.createProperty("id", Type.LONG);
      t.createProperty("dec", Type.DECIMAL);
    });

    String insert = "INSERT INTO T SET id = :id, dec = :v, u = :v";
    String[] values = { "123456789012345678901234567890", "18446744073709551617", "1.23456789012345678901234567890" };
    for (int k = 0; k < values.length; k++) {
      final int id = k + 1;
      final BigDecimal v = new BigDecimal(values[k]);
      db.transaction(() -> db.command("sql", insert, Map.of("id", id, "v", v)).close());
      post("command", "{\"language\":\"sql\",\"command\":\"" + insert + "\",\"params\":{\"id\":" + (id + 10) + ",\"v\":" + values[k] + "}}");
    }
    try (ResultSet rs = db.query("sql", "SELECT FROM T ORDER BY id")) {
      rs.stream().forEach(r -> System.out.println("embedded read: id=" + r.getProperty("id") + (((Long) r.getProperty("id")) > 10 ? " (HTTP)    " : " (embedded)")
          + " dec=" + r.getProperty("dec") + ", u=" + r.getProperty("u") + " (" + r.getProperty("u").getClass().getSimpleName() + ")"));
    }

    String lookup = "SELECT id FROM T WHERE id = :id";
    try (ResultSet rs = db.query("sql", lookup, Map.of("id", new BigDecimal("18446744073709551617")))) {
      List<Object> ids = new ArrayList<>();
      rs.stream().forEach(r -> ids.add(r.getProperty("id")));
      System.out.println("embedded:  " + lookup + " {id: 18446744073709551617} -> " + ids);
    }
    System.out.println("HTTP:      " + lookup + " {id: 18446744073709551617} -> "
        + post("query", "{\"language\":\"sql\",\"command\":\"" + lookup + "\",\"params\":{\"id\":18446744073709551617}}"));
    server.stop();
  }

  static String post(String op, String body) throws Exception {
    HttpRequest req = HttpRequest.newBuilder(URI.create("http://127.0.0.1:2480/api/v1/" + op + "/repro"))
        .header("Authorization", "Basic " + Base64.getEncoder().encodeToString("root:repro-password".getBytes(StandardCharsets.UTF_8)))
        .header("Content-Type", "application/json").POST(HttpRequest.BodyPublishers.ofString(body)).build();
    String answer = HttpClient.newHttpClient().send(req, HttpResponse.BodyHandlers.ofString()).body();
    return answer.substring(answer.indexOf("\"result\""), answer.indexOf("]") + 1);
  }
}

Output on main at cecb369df9, on Temurin 25 with compact object headers (the engine jar's build stamp names 04bb03ebec, the newest commit in cecb369df9 that is not a merge). Temurin 21 prints the same lines apart from the Java version on the first line. Records 1 to 3 were written embedded, records 11 to 13 with the same values over HTTP.

ArcadeDB 26.10.1-SNAPSHOT (04bb03ebec07fce19101595d65ca1e3bdf516755), Java 25.0.4
embedded read: id=1 (embedded) dec=123456789012345678901234567890, u=123456789012345678901234567890 (BigDecimal)
embedded read: id=2 (embedded) dec=18446744073709551617, u=18446744073709551617 (BigDecimal)
embedded read: id=3 (embedded) dec=1.23456789012345678901234567890, u=1.23456789012345678901234567890 (BigDecimal)
embedded read: id=11 (HTTP)     dec=-4362896299872285998, u=-4362896299872285998 (Long)
embedded read: id=12 (HTTP)     dec=1, u=1 (Integer)
embedded read: id=13 (HTTP)     dec=1.2345678901234567, u=1.2345678901234567 (Double)
embedded:  SELECT id FROM T WHERE id = :id {id: 18446744073709551617} -> []
HTTP:      SELECT id FROM T WHERE id = :id {id: 18446744073709551617} -> "result":[{"id":1}]

Versions affected: main cecb369df9 (Temurin 25 and 21), main 417314c18 (2026-09-17), and 26.9.1 print the same lines.

Expected: an integer that does not fit in a long is kept exact (as a BigDecimal or BigInteger) or refused with a 400, never wrapped, so the lookup returns no row; and a JSON number written to a DECIMAL property keeps all of its digits, as the embedded write does.

Activity

  1. added
    engine
    server
    severity:major
    on Oct 3, 2026
  2. added this to the 26.10.1 milestone on Oct 3, 2026
  3. added
    in progress
    on Oct 3, 2026
  4. self-assigned this
    on Oct 3, 2026
  5. removed
    in progress
    on Oct 4, 2026
  6. lvca commented on Oct 4, 2026

    @lvca
    Member

    Confirmed and fixed in #9131 (26.10.1). A JSON integer beyond the 64-bit range is now kept exact as a decimal instead of wrapping around, and a decimal with more than 17 digits keeps all its digits. Absurdly large number tokens (more than 1000 characters or an exponent beyond 1000) are refused with HTTP 400.

    ❤️ Thanks for helping make ArcadeDB better! ArcadeDB is free and open source, sustained by its community. If it's useful to you or your company, please consider becoming a sponsor to keep development going.

  7. tae898 commented on Oct 4, 2026

    @tae898
    ContributorAuthor

    Verified on the merge commit cc1ebe3 of #9131 (engine, server and plugin jars built from it), the repro from the description, on JDK 25 (compact object headers) and JDK 21. The output is the same on both JDKs (apart from the generated session ids).

    JSON parameter 417314c (before) cc1ebe3 (after)
    123456789012345678901234567890 stored -4362896299872285998 (Long, wrapped) stored 123456789012345678901234567890 (BigDecimal)
    18446744073709551617 stored 1 (Integer) stored 18446744073709551617 (BigDecimal)
    1.23456789012345678901234567890 stored 1.2345678901234567 (Double) stored 1.23456789012345678901234567890 (BigDecimal)
    SELECT id FROM T WHERE id = :id with {id: 18446744073709551617} over HTTP returns [{"id":1}] (the wrapped value matched record 1) returns [], as the embedded run does

    The embedded rows (the same values written through the Java API) are unchanged.

    Thanks for the fix.

  8. added a commit that references this issue on Oct 4, 2026
    3bdc022
  9. added a commit that references this issue on Oct 6, 2026
    7f1a7fc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugengineserverseverity:major

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions