steveloughran commented on code in PR #16500:
URL: https://github.com/apache/iceberg/pull/16500#discussion_r3336637772
##########
core/src/main/java/org/apache/iceberg/DVUtil.java:
##########
@@ -64,6 +65,35 @@ static PositionDeleteIndex readDV(DeleteFile deleteFile,
FileIO fileIO) {
}
}
+ /**
+ * Validates that the deletion-vector offset and length on a {@link
DeleteFile} are well-formed
+ * before they are consumed by a reader. Hostile or corrupted manifest
metadata may otherwise
+ * trigger a {@link NegativeArraySizeException}, an invalid seek, or a
multi-gigabyte allocation
+ * when the DV blob is read.
+ */
+ public static void validateDV(DeleteFile dv) {
+ Preconditions.checkArgument(
Review Comment:
there's enough repetition here to make factoring stuff out something to
consider, though I'll leave that to others to consider.
Everything looks for non-null, positive, and sizes must be < max_int.
split it out and you've got less to test
##########
core/src/main/java/org/apache/iceberg/DVUtil.java:
##########
@@ -64,6 +65,35 @@ static PositionDeleteIndex readDV(DeleteFile deleteFile,
FileIO fileIO) {
}
}
+ /**
+ * Validates that the deletion-vector offset and length on a {@link
DeleteFile} are well-formed
+ * before they are consumed by a reader. Hostile or corrupted manifest
metadata may otherwise
+ * trigger a {@link NegativeArraySizeException}, an invalid seek, or a
multi-gigabyte allocation
+ * when the DV blob is read.
+ */
Review Comment:
nit, add IllegalArgumentException to the javadocs
##########
core/src/test/java/org/apache/iceberg/TestFileMetadata.java:
##########
@@ -0,0 +1,118 @@
+/*
+ * 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.iceberg;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatCode;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+import org.junit.jupiter.api.Test;
+
+public class TestFileMetadata {
+
+ @Test
+ public void dvBuilderRejectsNegativeContentOffset() {
+ assertThatThrownBy(
+ () ->
+ FileMetadata.deleteFileBuilder(PartitionSpec.unpartitioned())
+ .ofPositionDeletes()
+ .withFormat(FileFormat.PUFFIN)
+ .withPath("/tmp/dv.puffin")
+ .withFileSizeInBytes(10)
+ .withRecordCount(1)
+ .withReferencedDataFile("/tmp/data.parquet")
+ .withContentOffset(-1L)
+ .withContentSizeInBytes(10L)
+ .build())
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessageContaining("Content offset must be non-negative for DV");
+ }
+
+ @Test
+ public void dvBuilderRejectsNegativeContentSize() {
+ assertThatThrownBy(
+ () ->
+ FileMetadata.deleteFileBuilder(PartitionSpec.unpartitioned())
+ .ofPositionDeletes()
+ .withFormat(FileFormat.PUFFIN)
+ .withPath("/tmp/dv.puffin")
+ .withFileSizeInBytes(10)
+ .withRecordCount(1)
+ .withReferencedDataFile("/tmp/data.parquet")
+ .withContentOffset(0L)
+ .withContentSizeInBytes(-1L)
+ .build())
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessageContaining("Content size must be non-negative for DV");
+ }
+
+ @Test
+ public void dvBuilderRejectsContentSizeAtIntegerMax() {
+ assertThatThrownBy(
+ () ->
+ FileMetadata.deleteFileBuilder(PartitionSpec.unpartitioned())
+ .ofPositionDeletes()
+ .withFormat(FileFormat.PUFFIN)
+ .withPath("/tmp/dv.puffin")
+ .withFileSizeInBytes(10)
+ .withRecordCount(1)
+ .withReferencedDataFile("/tmp/data.parquet")
+ .withContentOffset(0L)
+ .withContentSizeInBytes(Integer.MAX_VALUE)
+ .build())
Review Comment:
given that all `FileMetadata.Builder.Builder()` setters are valid, you could
factor these out to have a builder which creates a valid file, then each test
overrides with the invalid value, e.g
```
assertThatThrownBy(() ->
deleteFile()
.withContentSizeInBytes(Integer.MAX_VALUE)
.build()))
...
```
It'd make it clearer what is being invalid in each test.
again, personal opinion and I'll yield to others
--
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]