Core: Fix AvroEncoderUtil header error printing literal %02X#17125
Open
anxkhn wants to merge 1 commit into
Open
Core: Fix AvroEncoderUtil header error printing literal %02X#17125anxkhn wants to merge 1 commit into
anxkhn wants to merge 1 commit into
Conversation
AvroEncoderUtil.decode validated the magic bytes with Preconditions.checkState(cond, "Unrecognized header bytes: 0x%02X 0x%02X", header0, header1). Guava's Preconditions formats messages with Strings.lenientFormat, which only understands the %s placeholder, so the %02X specifiers were emitted verbatim and the two byte arguments were appended as decimal in a trailing bracket. A mismatched header therefore reported "Unrecognized header bytes: 0x%02X 0x%02X [16, 32]" instead of the intended hex, obscuring the actual bytes. Build the message with String.format(Locale.ROOT, ...) and pass it as the single %s argument, matching the existing handling in IcebergDecoder.decode. Add a regression test asserting the uppercase hex is rendered. Generated-by: opencode Signed-off-by: Anas Khan <83116240+anxkhn@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Iceberg's (relocated) Guava
Preconditionsformats messages withStrings.lenientFormat, which only understands the%splaceholder - notjava.util.Formatterconversions like%02X. As a result the%02Xspecifiersare emitted verbatim and the two byte arguments are appended as decimal in a
trailing bracket. A header mismatch therefore reports:
instead of the intended hex, which obscures the actual bytes that were read.
Fix
Build the message with
String.format(Locale.ROOT, "Unrecognized header bytes: 0x%02X 0x%02X", header0, header1)and pass it as the single%sargument tocheckState. This mirrors the existing, correct handling of the identical messagein
IcebergDecoder.decode(
core/src/main/java/org/apache/iceberg/data/avro/IcebergDecoder.java), and usesLocale.ROOTper the project conventions for formatting.With the fix, a mismatched header reports:
The thrown exception type is unchanged (
checkStatestill throwsIllegalStateException); only the message text is corrected.Testing
Added
decodeRejectsUnrecognizedHeaderBytestocore/src/test/java/org/apache/iceberg/avro/TestAvroEncoderUtil.java, assertingthat decoding a buffer with bad magic bytes throws
IllegalStateExceptionwhosemessage contains the rendered uppercase hex (and not the literal
%02X). The testfails before the fix (
... was: "Unrecognized header bytes: 0x%02X 0x%02X [16, 32]") and passes after it../gradlew :iceberg-core:spotlessCheck- passes./gradlew :iceberg-core:test --tests "org.apache.iceberg.avro.TestAvroEncoderUtil"- passesAI Disclosure
message used an unsupported %02X format specifier; apply the minimal fix
mirroring the existing IcebergDecoder site and add a regression test.