1
0
mirror of https://github.com/systemd/systemd.git synced 2024-12-22 17:35:35 +03:00

hexdecoct: fix NULL pointer dereferences in hexmem()

Fixes oss-fuzz#54090 (https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=54090).

Fixes #25655.
This commit is contained in:
Yu Watanabe 2022-12-07 09:06:48 +09:00
parent 45655e776f
commit 7d34567444
3 changed files with 22 additions and 1 deletions

View File

@ -59,11 +59,13 @@ char *hexmem(const void *p, size_t l) {
const uint8_t *x; const uint8_t *x;
char *r, *z; char *r, *z;
assert(p || l == 0);
z = r = new(char, l * 2 + 1); z = r = new(char, l * 2 + 1);
if (!r) if (!r)
return NULL; return NULL;
for (x = p; x < (const uint8_t*) p + l; x++) { for (x = p; x && x < (const uint8_t*) p + l; x++) {
*(z++) = hexchar(*x >> 4); *(z++) = hexchar(*x >> 4);
*(z++) = hexchar(*x & 15); *(z++) = hexchar(*x & 15);
} }

View File

@ -73,6 +73,25 @@ TEST(undecchar) {
assert_se(undecchar('9') == 9); assert_se(undecchar('9') == 9);
} }
static void test_hexmem_one(const char *in, const char *expected) {
_cleanup_free_ char *result = NULL;
_cleanup_free_ void *mem = NULL;
size_t len;
assert_se(result = hexmem(in, strlen_ptr(in)));
log_debug("hexmem(\"%s\") → \"%s\" (expected: \"%s\")", strnull(in), result, expected);
assert_se(streq(result, expected));
assert_se(unhexmem(result, SIZE_MAX, &mem, &len) >= 0);
assert_se(memcmp_safe(mem, in, len) == 0);
}
TEST(hexmem) {
test_hexmem_one(NULL, "");
test_hexmem_one("", "");
test_hexmem_one("foo", "666f6f");
}
static void test_unhexmem_one(const char *s, size_t l, int retval) { static void test_unhexmem_one(const char *s, size_t l, int retval) {
_cleanup_free_ char *hex = NULL; _cleanup_free_ char *hex = NULL;
_cleanup_free_ void *mem = NULL; _cleanup_free_ void *mem = NULL;

Binary file not shown.