Skip to content

Commit

Permalink
Merge pull request #590 from razeghi71/main
Browse files Browse the repository at this point in the history
Fix: Return None for Over-Segmented IPv6 Addresses
  • Loading branch information
mpilquist authored Jun 6, 2024
2 parents 70c0632 + 15a101a commit 6fefe1d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 18 deletions.
45 changes: 27 additions & 18 deletions shared/src/main/scala/com/comcast/ip4s/Host.scala
Original file line number Diff line number Diff line change
Expand Up @@ -773,6 +773,7 @@ object Ipv6Address extends Ipv6AddressCompanionPlatform {
}
idx += 1
}

if (result ne null) {
result
} else if (fields.isEmpty && (trimmed.isEmpty || trimmed != "::")) {
Expand All @@ -781,26 +782,34 @@ object Ipv6Address extends Ipv6AddressCompanionPlatform {
val bytes = new Array[Byte](16)
idx = 0
val prefixSize = prefix.size
var prefixIdx = prefixSize - 1
while (prefixIdx >= 0) {
val value = prefix(prefixIdx)
bytes(idx) = (value >> 8).toByte
bytes(idx + 1) = value.toByte
prefixIdx -= 1
idx += 2
}
val suffixSize = suffix.size
val numCondensedZeroes = bytes.size - idx - (suffixSize * 2)
idx += numCondensedZeroes
var suffixIdx = suffixSize - 1
while (suffixIdx >= 0) {
val value = suffix(suffixIdx)
bytes(idx) = (value >> 8).toByte
bytes(idx + 1) = value.toByte
suffixIdx -= 1
idx += 2
val numCondensedZeroes = 8 - (prefixSize + suffixSize)

if (numCondensedZeroes < 0) {
None
} else {
var prefixIdx = prefixSize - 1
while (prefixIdx >= 0) {
val value = prefix(prefixIdx)
bytes(idx) = (value >> 8).toByte
bytes(idx + 1) = value.toByte
prefixIdx -= 1
idx += 2
}

idx += numCondensedZeroes * 2

var suffixIdx = suffixSize - 1
while (suffixIdx >= 0) {
val value = suffix(suffixIdx)
bytes(idx) = (value >> 8).toByte
bytes(idx + 1) = value.toByte
suffixIdx -= 1
idx += 2
}

Some(unsafeFromBytes(bytes))
}
Some(unsafeFromBytes(bytes))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ class Ipv6AddressTest extends BaseTestSuite {
assertEquals(Ipv6Address.fromString(" : "), None)
}

test("parsing from string - does not parse invalid number of sections in ipv6") {
assertEquals(Ipv6Address.fromString("::1:1:1:1:1:1:2:1:3"), None)
assertEquals(Ipv6Address.fromString("1:1:1:1:1:1:2:1:3"), None)
}

test("parsing from string - does parse ::") {
assertEquals(Ipv6Address.fromString("::").isDefined, true)
assertEquals(Ipv6Address.fromString(" :: ").isDefined, true)
Expand Down Expand Up @@ -150,4 +155,5 @@ class Ipv6AddressTest extends BaseTestSuite {
assert(ipv6"fe80::1".isLinkLocal)
assert(ipv6"::ffff:169.254.0.0".isLinkLocal)
}

}

0 comments on commit 6fefe1d

Please sign in to comment.