summaryrefslogtreecommitdiff
path: root/src/basic/hexdecoct.c
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2016-12-16 13:08:29 +0100
committerLennart Poettering <lennart@poettering.net>2016-12-21 19:09:31 +0100
commita93c4776decffad08bfaf2b2a195aa162af896e8 (patch)
treecf0f52f70d57258e8cc3ec7c09ac8dbc0383e4fb /src/basic/hexdecoct.c
parentc0cee5f1e851f0a5ba7b7935e0290c6cf5c3aaeb (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.c13
1 files changed, 7 insertions, 6 deletions
diff --git a/src/basic/hexdecoct.c b/src/basic/hexdecoct.c
index c5bda6c4d6..6843aedd0a 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;
}