Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fuzz-tests: Improve the fuzz-base32-64 test #8183

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

Chand-ra
Copy link

Currently, fuzz testing for b64_encode() in the fuzz/fuzz-base32-64 test merely
encodes input and frees the result, providing no real verification of its behavior.

Introduce a new b64_decode() function and update the fuzz test to perform a
roundtrip—encoding followed by decoding—to ensure that b64_encode()
correctly preserves the original data.

Add the newly discovered inputs that result in greater code-coverage to the
seed corpus for this test.

Copy link
Contributor

@morehouse morehouse left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR is broken. Running on the existing corpus triggers a UBSan error:

tests/fuzz/fuzz-base32-64.c:25:2: runtime error: null pointer passed as argument 1, which is declared to never be null

common/base64.c Outdated
{
size_t dlen = base64_decoded_length(len);
u8 *ret = tal_arr(ctx, u8, dlen);
if (!base64_decode((char *)ret, dlen, str, len))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

base64_decode returns -1 on error, not 0.

We should be verifying that the returned value matches dlen.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should be verifying that the returned value matches dlen.

This is not ideal because base64_decoded_length() does not return the actual size of the decoded data; it only provides the minimum buffer size required for a safe decode. This becomes evident when modifying the code as follows:

if (base64_decode((char *)ret, dlen, str, len) != dlen)

still fails the test for the input corpus. Therefore, I have updated it to:

if (base64_decode((char *)ret, dlen, str, len) < 0)

Chandra Pratap added 2 commits April 12, 2025 05:35
Changelog-Added: 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.
Change in the fuzz-testing scheme of fuzz-base32-64 led to
the discovery of test inputs that result in greater in
code-coverage. Add these inputs to the test's seed corpus.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants