GeezerVison is showing github.com — Add assert to ensure the same id is not added multiple times (#14325) · netty/netty@e4ef07f
File tree
Expand file treeCollapse file tree
Open diff view settings
resolver-dns/src/main/java/io/netty5/resolver/dns
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboardExpand all lines: resolver-dns/src/main/java/io/netty5/resolver/dns/DnsQueryIdSpace.java +11-4Lines changed: 11 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -61,8 +61,8 @@ int nextId() { |
61 | 61 | } else if (freeIdx == -1 || |
62 | 62 | // Let's make it somehow random which free slot is used. |
63 | 63 | ThreadLocalRandom.current().nextBoolean()) { |
64 | | - // We have a slot that we can use to create a new bucket if we need to. |
65 | | - freeIdx = bucketIdx; |
| 64 | + // We have a slot that we can use to create a new bucket if we need to. |
| 65 | + freeIdx = bucketIdx; |
66 | 66 | } |
67 | 67 | } |
68 | 68 | if (freeIdx == -1) { |
@@ -178,8 +178,15 @@ void pushId(int id) { |
178 | 178 | assert id <= startId + ids.length && id >= startId; |
179 | 179 | // pick a slot for our index, and whatever was in that slot before will get moved to the tail. |
180 | 180 | int insertionPosition = ThreadLocalRandom.current().nextInt(count + 1); |
181 | | - ids[count] = ids[insertionPosition]; |
182 | | - ids[insertionPosition] = (short) id; |
| 181 | + |
| 182 | + short moveId = ids[insertionPosition]; |
| 183 | + short insertId = (short) id; |
| 184 | + |
| 185 | + // Assert that the ids are different or its the same index. |
| 186 | + assert moveId != insertId || insertionPosition == count; |
| 187 | + |
| 188 | + ids[count] = moveId; |
| 189 | + ids[insertionPosition] = insertId; |
183 | 190 | count++; |
184 | 191 | } |
185 | 192 |
|
|