util-lib: be stricter when decoding hex strings

If a hex string has an uneven length, generate an error instead of
silently assuming a trailing '0' was in place.
This commit is contained in:
Lennart Poettering
2016-12-21 19:09:31 +01:00
parent c0cee5f1e8
commit a93c4776de
2 changed files with 12 additions and 19 deletions
+7 -6
View File
@@ -97,6 +97,9 @@ int unhexmem(const char *p, size_t l, void **mem, size_t *len) {
assert(len);
assert(p);
if (l % 2 != 0)
return -EINVAL;
z = r = malloc((l + 1) / 2 + 1);
if (!r)
return -ENOMEM;
@@ -107,12 +110,10 @@ int unhexmem(const char *p, size_t l, void **mem, size_t *len) {
a = unhexchar(x[0]);
if (a < 0)
return a;
else if (x+1 < p + l) {
b = unhexchar(x[1]);
if (b < 0)
return b;
} else
b = 0;
b = unhexchar(x[1]);
if (b < 0)
return b;
*(z++) = (uint8_t) a << 4 | (uint8_t) b;
}