Skip to content

benchfn: don't memset a NULL dst buffer of size 0 - #4826

Open
xhon-pelushi wants to merge 1 commit into
meta:devfrom
xhon-pelushi:fix-4776
Open

xhon-pelushi wants to merge 1 commit into
meta:devfrom
xhon-pelushi:fix-4776

Conversation

@xhon-pelushi

Copy link
Copy Markdown

Fixes #4776

Two fullbench scenarios, 18 convertSequences (1st block) and 19 get1BlockSummary (1st block), prepare their inputs with prepConvertSequences(), which never sets a destination buffer: dst stays NULL and dstCapacity stays 0 (both benched functions ignore dst). BMK_benchFunction() in programs/benchfn.c then does the warm-up

memset(p.dstBuffers[i], 0xE5, p.dstCapacities[i]);

i.e. memset(NULL, 0xE5, 0). Passing a null pointer to memset() is undefined behavior even with a length of 0, and UBSan flags it via glibc's nonnull annotation.

This skips the warm-up memset when the destination capacity is 0.

I fixed it in benchfn.c rather than giving prepConvertSequences() a dummy buffer, so any other benchmark with an empty destination is covered too. Happy to switch to the other approach if you prefer.

Testing, building tests/fullbench with MOREFLAGS="-fsanitize=undefined -fno-sanitize-recover=all -g" (gcc 13.3):

  • on dev (01b7154): ./fullbench -i1 -b18 and -b19 each exit 1 with ../programs/benchfn.c:116:13: runtime error: null pointer passed as argument 1, which is declared to never be null
  • with this change: -b18, -b19 and the full ./fullbench -i1 run all exit 0 with no UBSan report

Some fullbench scenarios (convertSequences, get1BlockSummary) do not
need a destination buffer, so their preparation leaves dst == NULL and
dstCapacity == 0. BMK_benchFunction() then called
memset(NULL, 0xE5, 0). Passing a null pointer to memset() is undefined
behavior even when the length is 0, and UBSan reports it through
glibc's nonnull annotation:

  programs/benchfn.c:116:13: runtime error: null pointer passed as
  argument 1, which is declared to never be null

Skip the warm-up memset when the destination capacity is 0.

Fixes meta#4776
@meta-cla

meta-cla Bot commented Oct 6, 2026

Copy link
Copy Markdown

Hi @xhon-pelushi!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks!

@ceedot-rock

Copy link
Copy Markdown

@Cyan4973 — memset on a NULL dst with size 0 is UB even when the size is zero; skipping it is the right call. Two lines, no behavior change otherwise. Looks mergeable to me.

@meta-cla meta-cla Bot added the CLA Signed label Oct 10, 2026
@meta-cla

meta-cla Bot commented Oct 10, 2026

Copy link
Copy Markdown

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Undefined Behavior testing zstd in Gentoo

2 participants