summaryrefslogtreecommitdiff
path: root/src/libsystemd
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2016-07-25 20:50:24 +0200
committerLennart Poettering <lennart@poettering.net>2016-07-25 20:53:26 +0200
commit65548c58dddf721d03d8a5f5c96b196510f158fb (patch)
tree0d9fe575054eb1c5ae12fb72def2fd75317ee392 /src/libsystemd
parent87410f166eb5e0f06703bd82fdec2fb47afb58ef (diff)
sd-id128: be more liberal when reading files with 128bit IDs
Accept both files with and without trailing newlines. Apparently some rkt releases generated them incorrectly, missing the trailing newlines, and we shouldn't break that.
Diffstat (limited to 'src/libsystemd')
-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);
}