On 27.02.2026 11:58, Edwin Török wrote: > Fixes this `-fsanitize=undefined` error: > ``` > test_x86_emulator.c:614:12: runtime error: null pointer passed as argument 1, > which is declared to never be null > /usr/include/string.h:44:28: note: nonnull attribute specified here > SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior > test_x86_emulator.c:614:12 > ``` > > Although this is more of a grey area: I don't see anything in the > standard that'd forbid calling `memset` with NULL and 0,
There actually is. In the C99 spec clause 2 refers to section 7.1.4, where null pointer arguments are excluded. Imo for memcpy() etc exceptions should be made for the case where the count is also zero, but sadly nothing like that is there. Nit: Why do you talk of memset() though when memcpy() is what you alter? > but `glibc` > does specify it as non-null, which allows the compiler to make > optimizations assuming it never is NULL, so this is undefined behaviour > only on glibc. > Best to avoid the potential undefined behaviour though. > > Signed-off-by: Edwin Török <[email protected]> Acked-by: Jan Beulich <[email protected]> with both nits (above and below) addressed. I can again take care of that when committing. (Same remark applies here as to the usefulness of compiling ... > --- a/tools/tests/x86_emulator/test_x86_emulator.c > +++ b/tools/tests/x86_emulator/test_x86_emulator.c ... this file with sanitizer options active.) > @@ -611,7 +611,8 @@ static int fetch( > if ( verbose ) > printf("** %s(CS:%p,, %u,)\n", __func__, (void *)offset, bytes); > > - memcpy(p_data, (void *)offset, bytes); > + if (bytes) Nit: Style. A well formed if() is even visible in context. Jan > + memcpy(p_data, (void *)offset, bytes); > return X86EMUL_OKAY; > } >
