summaryrefslogtreecommitdiff
path: root/src/libsystemd/sd-id128
diff options
context:
space:
mode:
Diffstat (limited to 'src/libsystemd/sd-id128')
-rw-r--r--src/libsystemd/sd-id128/id128-util.c30
1 files changed, 21 insertions, 9 deletions
diff --git a/src/libsystemd/sd-id128/id128-util.c b/src/libsystemd/sd-id128/id128-util.c
index aaac838b59..c3f527d657 100644
--- a/src/libsystemd/sd-id128/id128-util.c
+++ b/src/libsystemd/sd-id128/id128-util.c
@@ -100,33 +100,45 @@ int id128_read_fd(int fd, Id128Format f, sd_id128_t *ret) {
assert(f < _ID128_FORMAT_MAX);
/* Reads an 128bit ID from a file, which may either be in plain format (32 hex digits), or in UUID format, both
- * followed by a newline and nothing else. */
+ * optionally followed by a newline and nothing else. ID files should really be newline terminated, but if they
+ * aren't that's OK too, following the rule of "Be conservative in what you send, be liberal in what you
+ * accept". */
- l = loop_read(fd, buffer, sizeof(buffer), false); /* we expect a short read of either 33 or 37 chars */
+ l = loop_read(fd, buffer, sizeof(buffer), false); /* we expect a short read of either 32/33 or 36/37 chars */
if (l < 0)
return (int) l;
if (l == 0) /* empty? */
return -ENOMEDIUM;
- if (l == 33) {
- if (f == ID128_UUID)
- return -EINVAL;
+ switch (l) {
+ case 33: /* plain UUID with trailing newline */
if (buffer[32] != '\n')
return -EINVAL;
+ /* fall through */
+ case 32: /* plain UUID without trailing newline */
+ if (f == ID128_UUID)
+ return -EINVAL;
+
buffer[32] = 0;
+ break;
- } else if (l == 37) {
- if (f == ID128_PLAIN)
+ case 37: /* RFC UUID with trailing newline */
+ if (buffer[36] != '\n')
return -EINVAL;
- if (buffer[36] != '\n')
+ /* fall through */
+ case 36: /* RFC UUID without trailing newline */
+ if (f == ID128_PLAIN)
return -EINVAL;
buffer[36] = 0;
- } else
+ break;
+
+ default:
return -EINVAL;
+ }
return sd_id128_from_string(buffer, ret);
}