From 6f9fad556185467f2000156a4e27e76159423ac5 Mon Sep 17 00:00:00 2001 From: yangjie01 Date: Tue, 1 Sep 2026 15:39:49 +0800 Subject: [PATCH] [common] Fix double position advance in DataOutputSerializer.writeBytes writeBytes(String) wrote each byte through writeByte, which goes to write(int) and advances position itself, and then advanced position by the string length a second time. After the call length() reported twice the bytes written and the next write landed past a gap of untouched bytes. The sibling writeChars has the same shape and never had that line. Nothing in the repository calls this overload; it is part of the java.io.DataOutput contract that DataOutputView exposes, so the defect is visible to callers outside the repository. Assisted-by: GLM-5.3 --- .../paimon/io/DataOutputSerializer.java | 1 - .../paimon/io/DataOutputSerializerTest.java | 56 +++++++++++++++++++ 2 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 paimon-common/src/test/java/org/apache/paimon/io/DataOutputSerializerTest.java diff --git a/paimon-common/src/main/java/org/apache/paimon/io/DataOutputSerializer.java b/paimon-common/src/main/java/org/apache/paimon/io/DataOutputSerializer.java index 33f2a8f0bf06..ee87cdbedbfc 100644 --- a/paimon-common/src/main/java/org/apache/paimon/io/DataOutputSerializer.java +++ b/paimon-common/src/main/java/org/apache/paimon/io/DataOutputSerializer.java @@ -153,7 +153,6 @@ public void writeBytes(String s) throws IOException { for (int i = 0; i < sLen; i++) { writeByte(s.charAt(i)); } - this.position += sLen; } @Override diff --git a/paimon-common/src/test/java/org/apache/paimon/io/DataOutputSerializerTest.java b/paimon-common/src/test/java/org/apache/paimon/io/DataOutputSerializerTest.java new file mode 100644 index 000000000000..ac6d99573c16 --- /dev/null +++ b/paimon-common/src/test/java/org/apache/paimon/io/DataOutputSerializerTest.java @@ -0,0 +1,56 @@ +/* + * 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.paimon.io; + +import org.junit.jupiter.api.Test; + +import java.io.IOException; + +import static org.assertj.core.api.Assertions.assertThat; + +/** Test for {@link DataOutputSerializer}. */ +public class DataOutputSerializerTest { + + @Test + public void testWriteBytesAdvancesPositionOnce() throws IOException { + DataOutputSerializer out = new DataOutputSerializer(16); + out.writeBytes("abc"); + + assertThat(out.length()).isEqualTo(3); + assertThat(out.getCopyOfBuffer()).containsExactly('a', 'b', 'c'); + } + + @Test + public void testWriteBytesLeavesTheNextWriteInPlace() throws IOException { + DataOutputSerializer out = new DataOutputSerializer(16); + out.writeBytes("abc"); + out.writeInt(42); + + // "abc" and then 42 as a big-endian int, with no gap of untouched bytes in between. + assertThat(out.getCopyOfBuffer()).containsExactly('a', 'b', 'c', 0, 0, 0, 42); + } + + @Test + public void testWriteBytesAcrossAResize() throws IOException { + DataOutputSerializer out = new DataOutputSerializer(2); + out.writeBytes("abcdef"); + + assertThat(out.getCopyOfBuffer()).containsExactly('a', 'b', 'c', 'd', 'e', 'f'); + } +}