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:
parent
45655e776f
commit
7d34567444
@ -59,11 +59,13 @@ char *hexmem(const void *p, size_t l) {
|
||||
const uint8_t *x;
|
||||
char *r, *z;
|
||||
|
||||
assert(p || l == 0);
|
||||
|
||||
z = r = new(char, l * 2 + 1);
|
||||
if (!r)
|
||||
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 & 15);
|
||||
}
|
||||
|
@ -73,6 +73,25 @@ TEST(undecchar) {
|
||||
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) {
|
||||
_cleanup_free_ char *hex = NULL;
|
||||
_cleanup_free_ void *mem = NULL;
|
||||
|
BIN
test/fuzz/fuzz-resource-record/oss-fuzz-54090
Normal file
BIN
test/fuzz/fuzz-resource-record/oss-fuzz-54090
Normal file
Binary file not shown.
Loading…
Reference in New Issue
Block a user