From 30920acd378ca91e0088b68d67b7095740654038 Mon Sep 17 00:00:00 2001 From: Steve Ayers Date: Wed, 9 Apr 2025 11:28:27 -0400 Subject: [PATCH 1/4] Port ipv6 fix --- .../java/build/buf/protovalidate/Ipv6.java | 27 ++++++++++--------- 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/src/main/java/build/buf/protovalidate/Ipv6.java b/src/main/java/build/buf/protovalidate/Ipv6.java index 52c276c0..cf64080e 100644 --- a/src/main/java/build/buf/protovalidate/Ipv6.java +++ b/src/main/java/build/buf/protovalidate/Ipv6.java @@ -14,6 +14,7 @@ package build.buf.protovalidate; +import java.text.ParseException; import java.util.ArrayList; import java.util.List; import javax.annotation.Nullable; @@ -176,8 +177,12 @@ private boolean addressPart() { return false; } - if (this.h16()) { - continue; + try { + if (this.h16()) { + continue; + } + } catch (ParseException | NumberFormatException e) { + return false; } if (this.take(':')) { @@ -191,6 +196,9 @@ private boolean addressPart() { if (this.take(':')) { return false; } + } else if (this.index == 1 || this.index == this.str.length()) { + // invalid - string cannot start or end on single colon + return false; } continue; } @@ -266,7 +274,7 @@ private boolean dotted() { * *

Stores 16-bit value in pieces. */ - private boolean h16() { + private boolean h16() throws ParseException, NumberFormatException { int start = this.index; while (this.index < this.str.length() && this.hexDig()) {} @@ -280,18 +288,13 @@ private boolean h16() { if (str.length() > 4) { // too long - return false; + throw new ParseException("invalid hex", this.index); } - try { - int val = Integer.parseInt(str, 16); + int val = Integer.parseInt(str, 16); - this.pieces.add(val); - return true; - } catch (NumberFormatException nfe) { - // Error converting to number - return false; - } + this.pieces.add(val); + return true; } /** From 5ac911b9f14073cfbccb8905d9281e00327c9c61 Mon Sep 17 00:00:00 2001 From: Steve Ayers Date: Wed, 9 Apr 2025 11:32:53 -0400 Subject: [PATCH 2/4] Comments --- src/main/java/build/buf/protovalidate/Ipv6.java | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/main/java/build/buf/protovalidate/Ipv6.java b/src/main/java/build/buf/protovalidate/Ipv6.java index cf64080e..1e0ee1d4 100644 --- a/src/main/java/build/buf/protovalidate/Ipv6.java +++ b/src/main/java/build/buf/protovalidate/Ipv6.java @@ -272,7 +272,11 @@ private boolean dotted() { * *

h16 = 1*4HEXDIG
    *
-   * 

Stores 16-bit value in pieces. + * If 1-4 hex digits are found, the parsed 16-bit unsigned integer is stored + * in pieces and true is returned. + * If 0 hex digits are found, returns false. + * If more than 4 hex digits are found, a ParseException is thrown. + * If the found hex digits cannot be converted to an int, a NumberFormatException is raised. */ private boolean h16() throws ParseException, NumberFormatException { int start = this.index; @@ -282,15 +286,21 @@ private boolean h16() throws ParseException, NumberFormatException { String str = this.str.substring(start, this.index); if (str.isEmpty()) { - // too short + // too short, just return false + // this is not an error condition, it just means we didn't find any + // hex digits at the current position. return false; } if (str.length() > 4) { // too long + // this is an error condition, it means we found a string of more than + // four valid hex digits, which is invalid in ipv6 addresses. throw new ParseException("invalid hex", this.index); } + // Note that this will throw a NumberFormatException if string cannot be + // converted to an int. int val = Integer.parseInt(str, 16); this.pieces.add(val); From 595cb76ca5438a123baeaa0eb9f2ba5b5ea092b8 Mon Sep 17 00:00:00 2001 From: Steve Ayers Date: Wed, 16 Apr 2025 10:33:58 -0400 Subject: [PATCH 3/4] change exception --- src/main/java/build/buf/protovalidate/Ipv6.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/build/buf/protovalidate/Ipv6.java b/src/main/java/build/buf/protovalidate/Ipv6.java index 1e0ee1d4..a24fb6a3 100644 --- a/src/main/java/build/buf/protovalidate/Ipv6.java +++ b/src/main/java/build/buf/protovalidate/Ipv6.java @@ -181,7 +181,7 @@ private boolean addressPart() { if (this.h16()) { continue; } - } catch (ParseException | NumberFormatException e) { + } catch (IllegalStateException | NumberFormatException e) { return false; } @@ -275,10 +275,10 @@ private boolean dotted() { * If 1-4 hex digits are found, the parsed 16-bit unsigned integer is stored * in pieces and true is returned. * If 0 hex digits are found, returns false. - * If more than 4 hex digits are found, a ParseException is thrown. + * If more than 4 hex digits are found, an IllegalStateException is thrown. * If the found hex digits cannot be converted to an int, a NumberFormatException is raised. */ - private boolean h16() throws ParseException, NumberFormatException { + private boolean h16() throws IllegalStateException, NumberFormatException { int start = this.index; while (this.index < this.str.length() && this.hexDig()) {} @@ -296,7 +296,7 @@ private boolean h16() throws ParseException, NumberFormatException { // too long // this is an error condition, it means we found a string of more than // four valid hex digits, which is invalid in ipv6 addresses. - throw new ParseException("invalid hex", this.index); + throw new IllegalStateException("invalid hex"); } // Note that this will throw a NumberFormatException if string cannot be From 7010808bd9d908a5e0d2035fb558a312a92f41e0 Mon Sep 17 00:00:00 2001 From: Steve Ayers Date: Wed, 16 Apr 2025 10:36:33 -0400 Subject: [PATCH 4/4] gen --- src/main/java/build/buf/protovalidate/Ipv6.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/build/buf/protovalidate/Ipv6.java b/src/main/java/build/buf/protovalidate/Ipv6.java index a24fb6a3..239cd894 100644 --- a/src/main/java/build/buf/protovalidate/Ipv6.java +++ b/src/main/java/build/buf/protovalidate/Ipv6.java @@ -14,7 +14,6 @@ package build.buf.protovalidate; -import java.text.ParseException; import java.util.ArrayList; import java.util.List; import javax.annotation.Nullable;