Skip to content

Commit

Permalink
fix NPE bug
Browse files Browse the repository at this point in the history
  • Loading branch information
gongleigl.gong committed Mar 27, 2018
1 parent 7d8d523 commit 700dfb7
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -109,18 +109,20 @@ public synchronized void deserialize(InputArchive ia) throws IOException {
}
List<ACL> aclList = new ArrayList<ACL>();
Index j = ia.startVector("acls");
if (j != null) {
while (!j.done()) {
ACL acl = new ACL();
acl.deserialize(ia, "acl");
aclList.add(acl);
j.incr();
}
longKeyMap.put(val, aclList);
aclKeyMap.put(aclList, val);
referenceCounter.put(val, new AtomicLongWithEquals(0));
i--;
if (j == null) {
LOG.error("ERROR: incorrent format of InputArchive" + ia);
throw new RuntimeException("ERROR: incorrent format of InputArchive" + ia);
}
while (!j.done()) {
ACL acl = new ACL();
acl.deserialize(ia, "acl");
aclList.add(acl);
j.incr();
}
longKeyMap.put(val, aclList);
aclKeyMap.put(aclList, val);
referenceCounter.put(val, new AtomicLongWithEquals(0));
i--;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

import org.apache.jute.BinaryInputArchive;
import org.apache.jute.BinaryOutputArchive;
import org.apache.jute.InputArchive;
import org.apache.jute.OutputArchive;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.data.ACL;
import org.apache.zookeeper.data.Id;
Expand Down Expand Up @@ -188,10 +190,35 @@ public void testSerializeDeserialize() throws IOException {
callAddUsageNTimes(deserializedCache, aclId3, 3);
callAddUsageNTimes(deserializedCache, aclId4, 4);
callAddUsageNTimes(deserializedCache, aclId5, 5);

assertCachesEqual(cache, deserializedCache);
}

@Test
public void testNPEInDeserialize() throws IOException {
ReferenceCountedACLCache serializeCache = new ReferenceCountedACLCache(){
@Override
public synchronized void serialize(OutputArchive oa) throws IOException {
oa.writeInt(1, "map");
oa.writeLong(1, "long");
oa.startVector(null, "acls");
oa.endVector(null, "acls");
}
};
ByteArrayOutputStream baos = new ByteArrayOutputStream();
BinaryOutputArchive archive = BinaryOutputArchive.getArchive(baos);
serializeCache.serialize(archive);

BinaryInputArchive inArchive = BinaryInputArchive.getArchive(new ByteArrayInputStream(baos.toByteArray()));
ReferenceCountedACLCache deserializedCache = new ReferenceCountedACLCache();
try{
deserializedCache.deserialize(inArchive);
}catch (NullPointerException e){
fail("should not throw NPE while do deserialized");
}catch (RuntimeException e) {
// do nothing.
}
}

private void assertCachesEqual(ReferenceCountedACLCache expected, ReferenceCountedACLCache actual){
assertEquals(expected.aclIndex, actual.aclIndex);
assertEquals(expected.aclKeyMap, actual.aclKeyMap);
Expand Down

0 comments on commit 700dfb7

Please sign in to comment.