Skip to content

Simplify IPv6 parsing logic a little #85

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 13 additions & 19 deletions buf/validate/internal/lib/ipv6.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ struct IPv6Parser : ParserCommon, public IPv6Prefix {
if (str.length() < minLength) {
return false;
}
return str[1] == '.' || str[2] == '.' || str[3] == '.';
return str[0] != ':' && (str[1] == '.' || str[2] == '.' || str[3] == '.');
}

bool parseDotted() {
Expand All @@ -71,28 +71,22 @@ struct IPv6Parser : ParserCommon, public IPv6Prefix {
int index = 0;
bool doubleColonFound = false;
uint16_t value;
enum : uint8_t {
Initial,
Hexadecatet,
Separator,
DoubleColon,
} state = Initial;
while (index < hexadecatets_count) {
if ((state == Separator || state == DoubleColon) &&
(doubleColonFound || index == hexadecatets_count - 2) && checkDotted()) {
if ((doubleColonFound || index == hexadecatets_count - 2) && checkDotted()) {
if (!parseDotted()) {
return false;
}
b <<= 32;
index += 2;
break;
} else if (state != Hexadecatet && parseHexadecimalHexadecatet(value)) {
state = Hexadecatet;
} else if (parseHexadecimalHexadecatet(value)) {
b <<= 16;
b |= value;
index++;
} else if (state != Separator && consumeSequence<':', ':'>()) {
state = DoubleColon;
} else if (consumeSequence<':', ':'>()) {
if (consume<Char<':'>>()) {
return false;
}
if (index > hexadecatets_count - 1 || doubleColonFound) {
return false;
}
Expand All @@ -102,14 +96,14 @@ struct IPv6Parser : ParserCommon, public IPv6Prefix {
// This ensures that we can't have more than 7 hexadecatets when there's
// a double-colon, even though we don't actually process a hexadecatet.
index++;
} else if (state == Hexadecatet && consume<Char<':'>>()) {
state = Separator;
} else {
// Unable to match anything: this is the end.
if (state != Hexadecatet && state != DoubleColon) {
// Ending on a separator is invalid.
} else if (consume<Char<':'>>()) {
if (index == 0 || str.empty()) {
// Can not start or end on a single colon.
return false;
}
continue;
} else {
// Unable to match anything: this is the end.
break;
}
}
Expand Down
24 changes: 11 additions & 13 deletions buf/validate/internal/lib/uri.cc
Original file line number Diff line number Diff line change
Expand Up @@ -202,34 +202,32 @@ struct UriParser : ParserCommon {
const size_t hexadecatets_count = 8;
int index = 0;
bool doubleColonFound = false;
enum : uint8_t { Initial, Hexadecatet, Separator, DoubleColon } state = Initial;
while (index < hexadecatets_count) {
if ((state == Separator || state == DoubleColon) &&
(doubleColonFound || index == hexadecatets_count - 2) &&
str.length() >= std::char_traits<char>::length("0.0.0.0") &&
if ((doubleColonFound || index == hexadecatets_count - 2) &&
str.length() >= std::char_traits<char>::length("0.0.0.0") && str[0] != ':' &&
(str[1] == '.' || str[2] == '.' || str[3] == '.')) {
uint8_t _;
return parseDecimalOctet(_) && consume<Char<'.'>>() && //
parseDecimalOctet(_) && consume<Char<'.'>>() && //
parseDecimalOctet(_) && consume<Char<'.'>>() && //
parseDecimalOctet(_);
} else if (uint16_t _; state != Hexadecatet && parseHexadecimalHexadecatet(_)) {
state = Hexadecatet;
} else if (uint16_t _; parseHexadecimalHexadecatet(_)) {
index++;
} else if (state != Separator && consumeSequence<':', ':'>()) {
state = DoubleColon;
if (index++ > hexadecatets_count - 1 || doubleColonFound) {
} else if (consumeSequence<':', ':'>()) {
if (consume<Char<':'>>() || index++ > hexadecatets_count - 1 || doubleColonFound) {
return false;
}
doubleColonFound = true;
} else if (state == Hexadecatet && consume<Char<':'>>()) {
state = Separator;
} else if (consume<Char<':'>>()) {
if (index == 0 || str.empty()) {
return false;
}
continue;
} else {
break;
}
}
return (state == Hexadecatet || state == DoubleColon) &&
(doubleColonFound || index == hexadecatets_count) &&
return (doubleColonFound || index == hexadecatets_count) &&
(!consumeSequence<'%', '2', '5'>() || consumeZoneId());
}

Expand Down
Loading