From 30494563f235b21c6583f7476b8ee35e9f5f8048 Mon Sep 17 00:00:00 2001 From: Tom Gundersen Date: Sat, 11 Jul 2015 19:14:52 +0200 Subject: basic: util - fix errorhandling in unhexmem() We were ignoring failures from unhexchar, which meant that invalid hex characters were being turned into garbage rather than the string rejected. Fix this by making unhexmem return an error code, also change the API slightly, to return the size of the returned memory, reflecting the fact that the memory is a binary blob,and not a string. For convenience, still append a trailing NULL byte to the returned memory (not included in the returned size), allowing callers to treat it as a string without doing a second copy. --- src/test/test-util.c | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'src/test') diff --git a/src/test/test-util.c b/src/test/test-util.c index 9fbfece14f..f7949ebaac 100644 --- a/src/test/test-util.c +++ b/src/test/test-util.c @@ -410,6 +410,30 @@ static void test_undecchar(void) { assert_se(undecchar('9') == 9); } +static void test_unhexmem(void) { + const char *hex = "efa214921"; + const char *hex_invalid = "efa214921o"; + _cleanup_free_ char *hex2 = NULL; + _cleanup_free_ void *mem = NULL; + size_t len; + + assert_se(unhexmem(hex, strlen(hex), &mem, &len) == 0); + assert_se(unhexmem(hex, strlen(hex) + 1, &mem, &len) == -EINVAL); + assert_se(unhexmem(hex_invalid, strlen(hex_invalid), &mem, &len) == -EINVAL); + + assert_se((hex2 = hexmem(mem, len))); + + free(mem); + + assert_se(memcmp(hex, hex2, strlen(hex)) == 0); + + free(hex2); + + assert_se(unhexmem(hex, strlen(hex) - 1, &mem, &len) == 0); + assert_se((hex2 = hexmem(mem, len))); + assert_se(memcmp(hex, hex2, strlen(hex) - 1) == 0); +} + static void test_cescape(void) { _cleanup_free_ char *escaped; @@ -1808,6 +1832,7 @@ int main(int argc, char *argv[]) { test_unoctchar(); test_decchar(); test_undecchar(); + test_unhexmem(); test_cescape(); test_cunescape(); test_foreach_word(); -- cgit v1.2.3-54-g00ecf