Skip to content

Commit a2376f4

Browse files
committed
Fix odd-length hex string in Parquet CLI
1 parent f0030bd commit a2376f4

2 files changed

Lines changed: 18 additions & 2 deletions

File tree

parquet-cli/src/main/java/org/apache/parquet/cli/BaseCommand.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
package org.apache.parquet.cli;
2121

2222
import com.beust.jcommander.internal.Lists;
23+
import com.google.common.annotations.VisibleForTesting;
2324
import com.google.common.base.Preconditions;
2425
import com.google.common.io.CharStreams;
2526
import com.google.common.io.Resources;
@@ -428,13 +429,16 @@ private void parseAndSetColumnKeys(String columnKeysStr, ConfigurableKeyRetrieve
428429
}
429430
}
430431

431-
private byte[] hexToBytes(String hex) {
432-
432+
@VisibleForTesting
433+
byte[] hexToBytes(String hex) {
434+
String originalHex = hex;
433435
if (hex.startsWith("0x") || hex.startsWith("0X")) {
434436
hex = hex.substring(2);
435437
}
436438

437439
int len = hex.length();
440+
Preconditions.checkArgument(len % 2 == 0, "Invalid hex string: %s", originalHex);
441+
438442
byte[] data = new byte[len / 2];
439443
for (int i = 0; i < len; i += 2) {
440444
data[i / 2] = (byte) ((Character.digit(hex.charAt(i), 16) << 4) + Character.digit(hex.charAt(i + 1), 16));

parquet-cli/src/test/java/org/apache/parquet/cli/BaseCommandTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,18 @@ public void qualifiedURIResourceURITest() throws IOException {
6262
Assert.assertEquals("/a", uri.getPath());
6363
}
6464

65+
@Test
66+
public void hexToBytes() {
67+
Assert.assertArrayEquals(new byte[] {0x10}, this.command.hexToBytes("0x10"));
68+
Assert.assertArrayEquals(new byte[] {0x05, 0x06}, this.command.hexToBytes("0x0506"));
69+
Assert.assertArrayEquals(new byte[] {0x01, 0x02, 0x03}, this.command.hexToBytes("0x010203"));
70+
}
71+
72+
@Test(expected = IllegalArgumentException.class)
73+
public void hexToBytesRejectsOddLengthKey() {
74+
this.command.hexToBytes("0x011");
75+
}
76+
6577
// For Windows
6678
@Test
6779
public void qualifiedPathTestForWindows() throws IOException {

0 commit comments

Comments
 (0)