Skip to content

Commit ce5e15e

Browse files
committed
Fixed a bug when looking up IPv4 addresses in IPv6 tree.
This only happened when there was no IPv4 subtree and the first bit of the address was 1.
1 parent 3595eb8 commit ce5e15e

2 files changed

Lines changed: 14 additions & 14 deletions

File tree

src/main/java/com/maxmind/maxminddb/MaxMindDbReader.java

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -98,17 +98,19 @@ private int findAddressInTree(InetAddress address)
9898
int record = this.startNode(bitLength);
9999

100100
for (int i = 0; i < bitLength; i++) {
101+
if (record >= this.metadata.nodeCount) {
102+
break;
103+
}
101104
int b = 0xFF & rawAddress[i / 8];
102105
int bit = 1 & (b >> 7 - (i % 8));
103106
record = this.readNode(record, bit);
104-
105-
if (record == this.metadata.nodeCount) {
106-
// record is empty
107-
return 0;
108-
} else if (record > this.metadata.nodeCount) {
109-
// record is a data pointer
110-
return record;
111-
}
107+
}
108+
if (record == this.metadata.nodeCount) {
109+
// record is empty
110+
return 0;
111+
} else if (record > this.metadata.nodeCount) {
112+
// record is a data pointer
113+
return record;
112114
}
113115
throw new InvalidDatabaseException("Something bad happened");
114116
}
@@ -135,12 +137,8 @@ private int ipV4StartNode() throws InvalidDatabaseException {
135137
return this.ipV4Start;
136138
}
137139
int node = 0;
138-
for (int i = 0; i < 96; i++) {
139-
int nextNode = this.readNode(node, 0);
140-
if (nextNode >= this.metadata.nodeCount) {
141-
break;
142-
}
143-
node = nextNode;
140+
for (int i = 0; i < 96 && node < this.metadata.nodeCount; i++) {
141+
node = this.readNode(node, 0);
144142
}
145143
this.ipV4Start = node;
146144
return node;

src/test/java/com/maxmind/maxminddb/ReaderTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ public void testNoIpV4SearchTree() throws IOException, URISyntaxException {
5353

5454
assertEquals("::/64", reader.get(InetAddress.getByName("1.1.1.1"))
5555
.textValue());
56+
assertEquals("::/64", reader.get(InetAddress.getByName("192.1.1.1"))
57+
.textValue());
5658
}
5759

5860
private void testMetadata(MaxMindDbReader reader, int ipVersion,

0 commit comments

Comments
 (0)