diff options
author | Lennart Poettering <lennart@poettering.net> | 2016-12-16 13:08:29 +0100 |
---|---|---|
committer | Sven Eden <yamakuzure@gmx.net> | 2017-07-17 17:58:35 +0200 |
commit | 9e573be454b20f2d0b4ab341f9ffa132d1f26ac5 (patch) | |
tree | 1b97a74681f8c1a3c3b326b6c10d402f919042ee /src/basic/hexdecoct.c | |
parent | 1fcfc60ccf3123905e6fae922f2899c60e13f6b9 (diff) |
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.
Diffstat (limited to 'src/basic/hexdecoct.c')
-rw-r--r-- | src/basic/hexdecoct.c | 13 |
1 files changed, 7 insertions, 6 deletions
diff --git a/src/basic/hexdecoct.c b/src/basic/hexdecoct.c index c5bda6c4d..6843aedd0 100644 --- a/src/basic/hexdecoct.c +++ b/src/basic/hexdecoct.c @@ -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; } |