Skip to content

Commit f6ba6a8

Browse files
authored
Merge pull request #742 from Netflix/250618.throw-null-ptr-null-pkey
throw null ptr exception when constructing null pkey idx
2 parents 3654ec6 + 22b0852 commit f6ba6a8

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

hollow/src/main/java/com/netflix/hollow/core/index/HollowPrimaryKeyIndex.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -625,6 +625,7 @@ private int fieldHash(int ordinal, int fieldIdx) {
625625
HollowObjectTypeReadState typeState = this.typeState;
626626
HollowObjectSchema schema = typeState.getSchema();
627627

628+
int parentOrdinal = ordinal;
628629
int lastFieldPath = fieldPathIndexes[fieldIdx].length - 1;
629630
for(int i=0;i<lastFieldPath;i++) {
630631
int fieldPosition = fieldPathIndexes[fieldIdx][i];
@@ -633,6 +634,13 @@ private int fieldHash(int ordinal, int fieldIdx) {
633634
schema = typeState.getSchema();
634635
}
635636

637+
if (ordinal == ORDINAL_NONE) {
638+
HollowObjectSchema parentSchema = this.typeState.getSchema();
639+
throw new NullPointerException("Cannot hash null field " +
640+
parentSchema.getFieldName(fieldIdx) + " in type " +
641+
parentSchema.getName() + " at ordinal " + parentOrdinal);
642+
}
643+
636644
int hashCode = HollowReadFieldUtils.fieldHashCode(typeState, ordinal, fieldPathIndexes[fieldIdx][lastFieldPath]);
637645

638646
switch(fieldTypes[fieldIdx]) {

hollow/src/test/java/com/netflix/hollow/core/index/HollowPrimaryKeyIndexTest.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,19 @@ public void testNotBindable() throws IOException {
335335
Assert.assertEquals(0, validPki.getMatchingOrdinal(1L));
336336
}
337337

338+
339+
@Test
340+
public void testNullPKeyIdx() throws IOException {
341+
HollowObjectMapper mapper = new HollowObjectMapper(writeStateEngine);
342+
mapper.add(new TypeNullPKey());
343+
roundTripSnapshot();
344+
345+
try {
346+
HollowPrimaryKeyIndex invalidPkIdx = new HollowPrimaryKeyIndex(this.readStateEngine, "TypeNullPKey", "id");
347+
fail("Index on type with null fields is expected to fail construction");
348+
} catch (NullPointerException e) {}
349+
}
350+
338351
private static void addDataForDupTesting(HollowWriteStateEngine writeStateEngine, int a1Start, double a2, int size) {
339352
TypeB typeB = new TypeB("commonTypeB");
340353
HollowObjectMapper mapper = new HollowObjectMapper(writeStateEngine);
@@ -380,6 +393,15 @@ public TypeB(String b1, boolean isDuplicate) {
380393
}
381394
}
382395

396+
@HollowPrimaryKey(fields = {"id"})
397+
private static class TypeNullPKey {
398+
private final Long id;
399+
400+
public TypeNullPKey() {
401+
this.id = null;
402+
}
403+
}
404+
383405
@Override
384406
protected void initializeTypeStates() { }
385407

0 commit comments

Comments
 (0)