diff options
author | Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> | 2016-07-25 15:08:29 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-07-25 15:08:29 -0400 |
commit | f86f6f829c3fc9544983040ec9d15bbd7df718e3 (patch) | |
tree | 18cc510b3f76256d1c8b0f1351a4a6283aa364e8 /src/libsystemd/sd-id128/id128-util.c | |
parent | 82fda58bc3fbe21e0ff609e84d625a38e8f6cca3 (diff) | |
parent | 0b81133facb7576e983ec8427ffc3a4a8cc62846 (diff) |
Merge pull request #3802 from poettering/id128-fixes
Id128 fixes and more
Diffstat (limited to 'src/libsystemd/sd-id128/id128-util.c')
-rw-r--r-- | src/libsystemd/sd-id128/id128-util.c | 30 |
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); } |