Skip to content

Commit

Permalink
AVRO-3938: Add null guards for Schema.Parser
Browse files Browse the repository at this point in the history
The `Schema.Parser` class has a private final property `validate`.
This means that the value provided to its constructor is saved and
cannot be changed at a later time.
However, if `null` is passed as `validate`, then it is bound to throw a
`NullPointerException` at an arbitrary time.
This commit adds a fail-fast guard against `null` to prevent this.
  • Loading branch information
pingpingy1 committed Feb 15, 2024
1 parent 5ae888c commit 78c2d74
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lang/java/avro/src/main/java/org/apache/avro/Schema.java
Original file line number Diff line number Diff line change
Expand Up @@ -1497,7 +1497,7 @@ public Parser() {
}

public Parser(final NameValidator validate) {
this.validate = validate;
this.validate = Objects.requireNonNull(validate, "validate");
}

/**
Expand Down
7 changes: 7 additions & 0 deletions lang/java/avro/src/test/java/org/apache/avro/TestSchema.java
Original file line number Diff line number Diff line change
Expand Up @@ -621,4 +621,11 @@ void add_types() {
assertNotNull(f1);
assertEquals(schemaRecord1, f1.schema());
}

@Test
void testParserNullValidate() {
assertThrows(NullPointerException.class, () -> {
new Schema.Parser(null);
});
}
}

0 comments on commit 78c2d74

Please sign in to comment.