Bug 23878 - memcmp() declares args non-NULL, even though it accepts n=0
Summary: memcmp() declares args non-NULL, even though it accepts n=0
Status: RESOLVED INVALID
Alias: None
Product: glibc
Classification: Unclassified
Component: libc (show other bugs)
Version: unspecified
: P2 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2018-11-10 21:06 UTC by Behdad Esfahbod
Modified: 2018-12-09 12:33 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Last reconfirmed:
fweimer: security-


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Behdad Esfahbod 2018-11-10 21:06:04 UTC
This is causing UBSan to flag an error in my code, which is (IMO legitimately) passing NULL to memcmp() when length argument is zero.
Comment 1 Andreas Schwab 2018-11-10 21:24:46 UTC
UBSan is correct, even if the size is zero the pointers must still have valid values.
Comment 2 Behdad Esfahbod 2018-11-10 21:37:17 UTC
Can you please refer me to the standard section for this?  I thought NULL *is* valid pointer for never being dereferenced.
Comment 3 Florian Weimer 2018-11-10 21:43:23 UTC
The standard uses the phrase “object pointed to”.  C does not have zero-sized objects, so pointers to objects are always not NULL.

I believe this is a defect in the standard and something that negatively impacts C++ interoperability, among other things, but unfortunately, this is a minority view.
Comment 4 Andreas Schwab 2018-11-10 21:50:14 UTC
7.24.1#2 "Unless explicitly stated otherwise in the description of a
particular function in this subclause, pointer arguments on such a call shall still have valid values, as described in 7.1.4."
Comment 5 Behdad Esfahbod 2018-11-10 21:50:50 UTC
Thank you both.