wombatu-kun commented on code in PR #16500: URL: https://github.com/apache/iceberg/pull/16500#discussion_r3315063058
########## data/src/test/java/org/apache/iceberg/TestBaseDeleteLoader.java: ########## @@ -0,0 +1,86 @@ +/* + * 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.assertThatThrownBy; + +import org.apache.iceberg.data.BaseDeleteLoader; +import org.apache.iceberg.data.DeleteLoader; +import org.apache.iceberg.io.InputFile; +import org.apache.iceberg.relocated.com.google.common.collect.ImmutableList; +import org.junit.jupiter.api.Test; + +public class TestBaseDeleteLoader { + + private static final String DATA_FILE = "/tmp/data.parquet"; + + private static final DeleteFile VALID_DV = + FileMetadata.deleteFileBuilder(PartitionSpec.unpartitioned()) + .ofPositionDeletes() + .withFormat(FileFormat.PUFFIN) + .withPath("/tmp/dv.puffin") + .withFileSizeInBytes(10) + .withRecordCount(1) + .withReferencedDataFile(DATA_FILE) + .withContentOffset(0L) + .withContentSizeInBytes(10L) + .build(); + + @Test + public void loadPositionDeletesRejectsNegativeContentOffset() { + DeleteFile dv = tamperedDV(-1L, 10L); + DeleteLoader loader = new BaseDeleteLoader(file -> failingInputFile()); + + assertThatThrownBy(() -> loader.loadPositionDeletes(ImmutableList.of(dv), DATA_FILE)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining("offset must be non-negative"); + } + + @Test + public void loadPositionDeletesRejectsNegativeContentSize() { + DeleteFile dv = tamperedDV(0L, -1L); + DeleteLoader loader = new BaseDeleteLoader(file -> failingInputFile()); + + assertThatThrownBy(() -> loader.loadPositionDeletes(ImmutableList.of(dv), DATA_FILE)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining("length must be non-negative"); + } + + // Wraps a well-formed DV but reports tampered (offset, size). Simulates a corrupted manifest, + // since the FileMetadata builder now rejects negative values at construction time. + private static DeleteFile tamperedDV(Long offset, Long size) { + return new Delegates.DelegatingDeleteFile(VALID_DV) { Review Comment: Done in eacedd538 (Mockito mock). ########## data/src/test/java/org/apache/iceberg/TestBaseDeleteLoader.java: ########## @@ -0,0 +1,86 @@ +/* + * 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.assertThatThrownBy; + +import org.apache.iceberg.data.BaseDeleteLoader; +import org.apache.iceberg.data.DeleteLoader; +import org.apache.iceberg.io.InputFile; +import org.apache.iceberg.relocated.com.google.common.collect.ImmutableList; +import org.junit.jupiter.api.Test; + +public class TestBaseDeleteLoader { + + private static final String DATA_FILE = "/tmp/data.parquet"; + + private static final DeleteFile VALID_DV = + FileMetadata.deleteFileBuilder(PartitionSpec.unpartitioned()) + .ofPositionDeletes() + .withFormat(FileFormat.PUFFIN) + .withPath("/tmp/dv.puffin") + .withFileSizeInBytes(10) + .withRecordCount(1) + .withReferencedDataFile(DATA_FILE) + .withContentOffset(0L) + .withContentSizeInBytes(10L) + .build(); + + @Test + public void loadPositionDeletesRejectsNegativeContentOffset() { + DeleteFile dv = tamperedDV(-1L, 10L); + DeleteLoader loader = new BaseDeleteLoader(file -> failingInputFile()); + + assertThatThrownBy(() -> loader.loadPositionDeletes(ImmutableList.of(dv), DATA_FILE)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining("offset must be non-negative"); + } + + @Test + public void loadPositionDeletesRejectsNegativeContentSize() { + DeleteFile dv = tamperedDV(0L, -1L); + DeleteLoader loader = new BaseDeleteLoader(file -> failingInputFile()); + + assertThatThrownBy(() -> loader.loadPositionDeletes(ImmutableList.of(dv), DATA_FILE)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining("length must be non-negative"); + } + + // Wraps a well-formed DV but reports tampered (offset, size). Simulates a corrupted manifest, + // since the FileMetadata builder now rejects negative values at construction time. + private static DeleteFile tamperedDV(Long offset, Long size) { + return new Delegates.DelegatingDeleteFile(VALID_DV) { + @Override + public Long contentOffset() { + return offset; + } + + @Override + public Long contentSizeInBytes() { + return size; + } + }; + } + + // The validator must fire before any I/O attempt — if it does not, the loader will call this Review Comment: Done in eacedd538. Removed across the whole branch diff. ########## 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: Done 1a0356baf. ########## 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: Done 1a0356baf. Added a validDvBuilder() returning a valid builder; each test now overrides only the single invalid field. ########## core/src/test/java/org/apache/iceberg/TestDVUtil.java: ########## @@ -0,0 +1,104 @@ +/* + * 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.assertThatCode; +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import org.junit.jupiter.api.Test; + +public class TestDVUtil { + + @Test + public void validateDVRejectsNullOffset() { + DeleteFile dv = dv(null, 10L); Review Comment: Done b36c242fa. Took the parameterized variant for both the reject and accept cases, and applied the same shape to TestBaseDeleteLoader. -- 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]
