Skip to content

Commit 20b24da

Browse files
author
Chandra Pratap
committed
fuzz-tests: Enhance b64_encode() validation with roundtrip decoding
Changelog-None: Currently, fuzz testing for b64_encode() merely encodes input and frees the result, providing no real verification of its behavior. Introduce a new b64_decode() function (modeled after b32_decode()) and update the fuzz test to perform a roundtrip—encoding followed by decoding—to ensure that b64_encode() correctly preserves the original data.
1 parent 63e15c1 commit 20b24da

File tree

3 files changed

+13
-0
lines changed

3 files changed

+13
-0
lines changed

common/base64.c

+9
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,12 @@ char *b64_encode(const tal_t *ctx, const void *data, size_t len)
1414
str[enclen] = '\0';
1515
return str;
1616
}
17+
18+
u8 *b64_decode(const tal_t *ctx, const char *str, size_t len)
19+
{
20+
size_t dlen = base64_decoded_length(len);
21+
u8 *ret = tal_arr(ctx, u8, dlen);
22+
if (base64_decode((char *)ret, dlen, str, len) < 0)
23+
return tal_free(ret);
24+
return ret;
25+
}

common/base64.h

+1
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@
55
#include <ccan/tal/tal.h>
66

77
char *b64_encode(const tal_t *ctx, const void *data, size_t len);
8+
u8 *b64_decode(const tal_t *ctx, const char *str, size_t len);
89

910
#endif /* LIGHTNING_COMMON_BASE64_H */

tests/fuzz/fuzz-base32-64.c

+3
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,8 @@ void run(const uint8_t *data, size_t size)
2121
tal_free(decoded);
2222

2323
encoded = b64_encode(NULL, data, size);
24+
decoded = b64_decode(NULL, encoded, strlen(encoded));
25+
assert(memcmp(decoded, data, size) == 0);
2426
tal_free(encoded);
27+
tal_free(decoded);
2528
}

0 commit comments

Comments
 (0)