[client-v2] RowBinaryFormatSerializer.writeUInt256 writes 16 bytes; writeInt128/writeInt256 wrap values above the signed maximum

未关闭 适合新手
#3,412 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
72/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
java
领域
database

调研方向

问题出在 RowBinaryFormatSerializer.java 中:writeUInt256(约第 90 行)调用了 writeUnsignedInt128,而它应当调用 writeUnsignedInt256;writeInt128 和 writeInt256(约第 78 行和第 86 行)缺少有符号范围检查。clickhouse-data 中的 BinaryStreamUtils.writeBigInteger(约第 1064 行)也有关系,因为它会对 2^127 这类值丢弃一个前导字节。完成的标准是:UInt256 写入 32 个字节,接受最大到 2^256-1 的值,而 Int128/Int256 在有符号范围之外抛出 IllegalArgumentException。请用 issue 中的复现程序进行验证。

由索引模型根据 Issue 内容生成。

描述

bug client data-types

Description

RowBinaryFormatSerializer (client-v2, the typed writer that an application uses in Client.insert(table, out -> ..., ClickHouseFormat.RowBinary, ...)) writes some big integers incorrectly:

  • writeUInt256(BigInteger) writes 16 bytes, but a UInt256 value has 32 bytes. The server then reads the next columns from the wrong offset. The insert fails with CANNOT_READ_ALL_DATA, or it stores rows with wrong values and no error (5 rows written, 3 different rows stored). It also rejects a valid UInt256 value above the UInt128 maximum.
  • writeInt128(BigInteger) and writeInt256(BigInteger) accept a value above the signed maximum (up to the unsigned maximum) and store it as a negative number with no error: 2^127 is stored as the Int128 minimum. A value below the signed minimum is rejected.

RowBinaryFormatWriter, POJO insert and the jdbc-v2 RowBinary writer write UInt256 correctly (32 bytes).

Steps to reproduce
  1. Run the program below. It creates the tables (ENGINE = Memory) and inserts with RowBinaryFormatSerializer.
  2. Read the tables: SELECT * FROM t_u256 and SELECT * FROM t_i128.
Error Log or Exception StackTrace

Program output:

t_u256: inserted
t_u256_big: java.lang.IllegalArgumentException: BigInteger(340282366920938463463374607431768211456) should be between 0 and 340282366920938463463374607431768211455 inclusive of both values
t_i128: inserted
t_u128: java.lang.IllegalArgumentException: BigInteger(340282366920938463463374607431768211456) should be between 0 and 340282366920938463463374607431768211455 inclusive of both values

Server, SELECT * FROM t_u256 (written: five rows (i, 5, 7)) and SELECT * FROM t_i128 (written: (1, 2^127, 7)):

   ┌─id─┬─────────────────────────────────────────────────────────────────────v─┬─tail─┐
1. │  1 │           31385508679856407096222729520892270990187965513654415130629 │    0 │
2. │  0 │ 107839786712542271326374825694929048657002242602971011756044115247111 │    5 │
3. │  0 │                              1701411835000833130017321933642527670272 │    7 │
   └────┴───────────────────────────────────────────────────────────────────────┴──────┘
   ┌─id─┬────────────────────────────────────────v─┬─tail─┐
1. │  1 │ -170141183460469231731687303715884105728 │    7 │
   └────┴──────────────────────────────────────────┴──────┘

With one row in t_u256, the insert fails instead: Code: 33. DB::Exception: Cannot read all data. Bytes read: 20. Bytes expected: 32: (at row 1) : While executing WaitForAsyncInsert. (CANNOT_READ_ALL_DATA).

Expected Behaviour
  • writeUInt256 writes 32 bytes (server: SELECT byteSize(toUInt256(5)) returns 32) and accepts values up to 2^256 - 1. t_u256 then has the five rows (i, 5, 7).
  • writeInt128 / writeInt256 reject a value outside the signed range with IllegalArgumentException, as writeUInt128 does in the contrast case t_u128.
Root cause
  • RowBinaryFormatSerializer.java:90: writeUInt256 calls BinaryStreamUtils.writeUnsignedInt128 instead of writeUnsignedInt256.
  • RowBinaryFormatSerializer.java:78 and :86 call BinaryStreamUtils.writeInt128 / writeInt256 with no range check, and BinaryStreamUtils.writeBigInteger (clickhouse-data, BinaryStreamUtils.java:1064) drops the leading zero byte of a positive value that is one byte too long, so 2^127 becomes 0x80 00 .. 00.

The fix is in RowBinaryFormatSerializer only. Polyglot AI can open a fix PR if a maintainer assigns this issue to @polyglotAI-bot.

Related:

  • #3411 (open): adds the signed Int128 / Int256 range check in SerializerUtils; it does not change RowBinaryFormatSerializer.
  • #3176 (open): the same kind of silent wrap on the RowBinaryFormatWriter path.
Code Example
Reproduction program (Java, 49 lines)
import com.clickhouse.client.api.Client;
import com.clickhouse.client.api.data_formats.RowBinaryFormatSerializer;
import com.clickhouse.client.api.insert.InsertSettings;
import com.clickhouse.data.ClickHouseFormat;
import java.math.BigInteger;

public class Min {
    interface Rows { void write(RowBinaryFormatSerializer s) throws Exception; }

    static void insert(Client client, String table, Rows rows) {
        try {
            client.insert(table, out -> {
                try { rows.write(new RowBinaryFormatSerializer(out)); }
                catch (Exception e) { throw new RuntimeException(e); }
            }, ClickHouseFormat.RowBinary, new InsertSettings()).get().close();
            System.out.println(table + ": inserted");
        } catch (Exception e) {
            Throwable t = e;
            while (t.getCause() != null) t = t.getCause();
            System.out.println(table + ": " + t);
        }
    }

    public static void main(String[] args) throws Exception {
        Client client = new Client.Builder().addEndpoint("http://localhost:8123")
                .setUsername("default").setPassword("").build();
        for (String t : new String[] {"t_u256 (id Int32, v UInt256, tail Int32)",
                "t_u256_big (id Int32, v UInt256, tail Int32)",
                "t_i128 (id Int32, v Int128, tail Int32)",
                "t_u128 (id Int32, v UInt128, tail Int32)"}) {
            client.execute("DROP TABLE IF EXISTS " + t.substring(0, t.indexOf(' '))).get().close();
            client.execute("CREATE TABLE " + t + " ENGINE = Memory").get().close();
        }
        BigInteger p127 = BigInteger.ONE.shiftLeft(127);
        BigInteger p128 = BigInteger.ONE.shiftLeft(128);

        // 1. UInt256: five rows (id = i, v = 5, tail = 7)
        insert(client, "t_u256", s -> {
            for (int i = 1; i <= 5; i++) { s.writeInt32(i); s.writeUInt256(BigInteger.valueOf(5)); s.writeInt32(7); }
        });
        // 2. UInt256: v = 2^128, a valid UInt256 value
        insert(client, "t_u256_big", s -> { s.writeInt32(1); s.writeUInt256(p128); s.writeInt32(7); });
        // 3. Int128: v = 2^127 (Int128 maximum + 1)
        insert(client, "t_i128", s -> { s.writeInt32(1); s.writeInt128(p127); s.writeInt32(7); });
        // Contrast: UInt128: v = 2^128 (UInt128 maximum + 1)
        insert(client, "t_u128", s -> { s.writeInt32(1); s.writeUInt128(p128); s.writeInt32(7); });
        client.close();
    }
}
Configuration
Client Configuration

Default settings (see the program).

Environment
  • Cloud
  • Client version: client-v2 0.12.0-rc1-SNAPSHOT, main at 051a7ecfd
  • Language version: Java 17.0.21
  • OS: Linux
ClickHouse Server
  • ClickHouse Server version: 26.10.4.207
  • ClickHouse Server non-default settings, if any: none
  • CREATE TABLE statements for tables involved: in the program
  • Sample data for all these tables: in the program

Classification: impact: Data integrity · area: Data types & formats (UInt256, Int128, Int256)


Found by automated analysis (Polyglot AI) while working on #2619. Verified against a live server.

主要语言
Java
星标
1.6k
派生
657
平均合并
2 天 6 小时
30 天内合并 PR
36

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

ClickHouse/clickhouse-java 的其他 Issue

查看 ClickHouse/clickhouse-java 的全部 Issue

相似的 Issue

更多 Java Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。