Skip to content

Commit c0ec763

Browse files
committed
Add sudo_uuid_from_string() to parse uuid string into a uid.
Also fixes a bug in sudo_uuid_to_string() where the '-' were misplaced. The uuid tests now verify that the generated uuid round-trips from binary -> string -> binary.
1 parent 5c77ce5 commit c0ec763

File tree

4 files changed

+59
-6
lines changed

4 files changed

+59
-6
lines changed

include/sudo_util.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,5 +375,7 @@ sudo_dso_public void sudo_uuid_create_v1(unsigned char uuid_out[restrict static
375375
#define sudo_uuid_create(_a) sudo_uuid_create_v1((_a))
376376
sudo_dso_public char *sudo_uuid_to_string_v1(const unsigned char uuid[restrict static 16], char * restrict dst, size_t dstsiz);
377377
#define sudo_uuid_to_string(_a, _b, _c) sudo_uuid_to_string_v1((_a), (_b), (_c))
378+
sudo_dso_public int sudo_uuid_from_string_v1(const char *str, unsigned char uuid[restrict static 16]);
379+
#define sudo_uuid_from_string(_a, _b) sudo_uuid_from_string_v1((_a), (_b))
378380

379381
#endif /* SUDO_UTIL_H */

lib/util/regress/uuid/uuid_test.c

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* SPDX-License-Identifier: ISC
33
*
4-
* Copyright (c) 2021 Todd C. Miller <[email protected]>
4+
* Copyright (c) 2021, 2025 Todd C. Miller <[email protected]>
55
*
66
* Permission to use, copy, modify, and distribute this software for any
77
* purpose with or without fee is hereby granted, provided that the above
@@ -75,6 +75,8 @@ main(int argc, char *argv[])
7575

7676
/* Do 16 passes. */
7777
for (ntests = 0; ntests < 16; ntests++) {
78+
char uuid_buf[16], uuid_str[37];
79+
7880
sudo_uuid_create(uuid.u8);
7981

8082
/* Variant: two most significant bits (6 and 7) are 0 and 1. */
@@ -95,6 +97,24 @@ main(int argc, char *argv[])
9597
errors++;
9698
continue;
9799
}
100+
101+
/* Test round-tripping uuid -> string -> uuid */
102+
if (sudo_uuid_to_string(uuid.u8, uuid_str, sizeof(uuid_str)) == NULL) {
103+
sudo_warnx("unable to convert uuid to string form");
104+
errors++;
105+
continue;
106+
}
107+
if (sudo_uuid_from_string(uuid_str, uuid_buf) != 0) {
108+
sudo_warnx("unable to parse uuid string \"%s\" to binary",
109+
uuid_str);
110+
errors++;
111+
continue;
112+
}
113+
if (memcmp(uuid.u8, uuid_buf, sizeof(uuid_buf)) != 0) {
114+
sudo_warnx("binary uuid mismatch");
115+
errors++;
116+
continue;
117+
}
98118
}
99119

100120
if (ntests != 0) {

lib/util/util.exp.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ sudo_term_raw_v1
172172
sudo_term_restore_v1
173173
sudo_ttyname_dev_v1
174174
sudo_uuid_create_v1
175+
sudo_uuid_from_string_v1
175176
sudo_uuid_to_string_v1
176177
sudo_vfatal_nodebug_v1
177178
sudo_vfatalx_nodebug_v1

lib/util/uuid.c

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* SPDX-License-Identifier: ISC
33
*
4-
* Copyright (c) 2020 Todd C. Miller <[email protected]>
4+
* Copyright (c) 2020-2021, 2025 Todd C. Miller <[email protected]>
55
*
66
* Permission to use, copy, modify, and distribute this software for any
77
* purpose with or without fee is hereby granted, provided that the above
@@ -80,10 +80,7 @@ sudo_uuid_to_string_v1(const unsigned char uuid[restrict static 16], char * rest
8080
*cp++ = hex[uuid[i] & 0x0f];
8181

8282
switch (i) {
83-
case 4:
84-
case 6:
85-
case 8:
86-
case 10:
83+
case 3: case 5: case 7: case 9:
8784
*cp++ = '-';
8885
break;
8986
}
@@ -92,3 +89,36 @@ sudo_uuid_to_string_v1(const unsigned char uuid[restrict static 16], char * rest
9289

9390
return dst;
9491
}
92+
93+
/*
94+
* Parse 36-byte uuid string into a 16-byte binary uuid.
95+
* Returns 0 on success, -1 if str is not a valid uuid.
96+
*/
97+
int
98+
sudo_uuid_from_string_v1(const char *str, unsigned char uuid[static 16])
99+
{
100+
unsigned int i = 0, j = 0;
101+
int ch;
102+
103+
if (strlen(str) != 36)
104+
return -1;
105+
106+
/* Parse a uuid in the format 123e4567-e89b-12d3-a456-426655440000 */
107+
while (i < 36) {
108+
switch (i) {
109+
case 8: case 13: case 18: case 23:
110+
if (str[i] != '-')
111+
return -1;
112+
i++;
113+
FALLTHROUGH;
114+
default:
115+
ch = sudo_hexchar(str + i);
116+
if (ch == -1)
117+
return -1;
118+
uuid[j++] = ch;
119+
i += 2;
120+
}
121+
}
122+
123+
return 0;
124+
}

0 commit comments

Comments
 (0)