[PATCH 3/3] lib/uuid: avoid double traversal in __uuid_parse()

From: Josh Law

Date: Thu Mar 12 2026 - 14:43:37 EST


__uuid_parse() calls uuid_is_valid() to walk all 36 characters for
format validation, then walks the string a second time to parse the
hex bytes. Combine both passes into one: validate each hex digit
inline via hex_to_bin() return value and check the four dash positions
after the loop.

uuid_is_valid() remains exported unchanged for callers that only need
validation without parsing.

Signed-off-by: Josh Law <objecting@xxxxxxxxxxxxx>
---
lib/uuid.c | 10 +++++++---
1 file changed, 7 insertions(+), 3 deletions(-)

diff --git a/lib/uuid.c b/lib/uuid.c
index 128a51f1879b..89608f82ca6b 100644
--- a/lib/uuid.c
+++ b/lib/uuid.c
@@ -108,16 +108,20 @@ static int __uuid_parse(const char *uuid, __u8 b[16], const u8 ei[16])
static const u8 si[16] = {0,2,4,6,9,11,14,16,19,21,24,26,28,30,32,34};
unsigned int i;

- if (!uuid_is_valid(uuid))
- return -EINVAL;
-
for (i = 0; i < 16; i++) {
int hi = hex_to_bin(uuid[si[i] + 0]);
int lo = hex_to_bin(uuid[si[i] + 1]);

+ if (hi < 0 || lo < 0)
+ return -EINVAL;
+
b[ei[i]] = (hi << 4) | lo;
}

+ if (uuid[8] != '-' || uuid[13] != '-' ||
+ uuid[18] != '-' || uuid[23] != '-')
+ return -EINVAL;
+
return 0;
}

--
2.34.1