TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Making memcpy(NULL, NULL, 0) well-defined

234 pointsby gslin5 months ago

9 comments

whytevuhuni5 months ago
How interesting. GCC does indeed remove that branch.<p><a href="https:&#x2F;&#x2F;godbolt.org&#x2F;z&#x2F;aPcr1bfPe" rel="nofollow">https:&#x2F;&#x2F;godbolt.org&#x2F;z&#x2F;aPcr1bfPe</a>
评论 #42390556 未加载
评论 #42388713 未加载
badmintonbaseba5 months ago
I just skimmed through the proposed wording in [N3322]. It looks like it silently fixes a defect too, NULL == NULL was also undefined up until C23. Hilarious.<p>[N3322] <a href="https:&#x2F;&#x2F;www.open-std.org&#x2F;jtc1&#x2F;sc22&#x2F;wg14&#x2F;www&#x2F;docs&#x2F;n3322.pdf" rel="nofollow">https:&#x2F;&#x2F;www.open-std.org&#x2F;jtc1&#x2F;sc22&#x2F;wg14&#x2F;www&#x2F;docs&#x2F;n3322.pdf</a>
评论 #42388309 未加载
评论 #42388491 未加载
评论 #42388985 未加载
评论 #42389367 未加载
voidUpdate5 months ago
I feel like I&#x27;ve misunderstood something here... shouldn&#x27;t memcpy(anything, anything, 0) just do nothing, because you&#x27;re copying 0 bytes?
评论 #42387125 未加载
评论 #42387254 未加载
评论 #42387175 未加载
评论 #42392885 未加载
评论 #42387131 未加载
评论 #42387170 未加载
评论 #42388469 未加载
nmilo5 months ago
&gt; However, the most vocal opposition came from a static analysis perspective: Making null pointers well-defined for zero length means that static analyzers can no longer unconditionally report NULL being passed to functions like memcpy—they also need to take the length into account now.<p>How does this make any sense? We don&#x27;t want to remove a low hanging footgun because static analyzers can no longer detect it?
评论 #42394514 未加载
评论 #42393179 未加载
MrMcCall5 months ago
Isn&#x27;t it more sensible to just check that the params that are about to be sent to memcpy be reasonable?<p>That is why I tend to wrap my system calls with my own internal function (which can be inlined in certain PLs), where I can standardize such tests. Otherwise, the resulting code that performs the checks and does the requisite error handling is bloated.<p>Note that I am also loath to #DEFINE such code because C is already rife with them and my perspective is that the less of them the better.<p>At the end of the day, quick and dirty fixes will prove the adage &quot;short cuts make long delays&quot;, and OpenBSD&#x27;s approach is the only really viable long-term solution, where you just have to rewrite your code if it has ill-advised constructs.<p>For designing libraries such as C&#x27;s stdlib, I don&#x27;t believe in &#x27;undefined behavior&#x27;, clearly define your semantics and say, &quot;If you pass a NULL to memcpy, this is what will happen.&quot; Same for providing a (n == 0), or should (src == dst).<p>And if, for some strange reason, fixing the semantics breaks calling code, then I can&#x27;t imagine that their code wasn&#x27;t f_cked in the first place.
评论 #42388475 未加载
评论 #42392992 未加载
ape45 months ago
Only about 1000 more functions to do this to.
hwc5 months ago
Well, that seems like something that should have been there from the beginning .
MuffinFlavored5 months ago
&gt; because NULL + 0 is undefined behavior in C.<p>Why? It&#x27;s 2024. Make it not be? Sure, some older stuff already written might no longer compile and need to be updated. Put it behind a &quot;newer&quot; standard flag&#x2F;version or whatever.<p>Or is it that it can&#x27;t be caught at compile time and only run time... hmm...
评论 #42388918 未加载
评论 #42388932 未加载
high_na_euv5 months ago
&gt;On the one hand, UB can be important for compiler optimizations<p>e.g?
评论 #42387834 未加载
评论 #42387666 未加载
评论 #42387680 未加载
评论 #42387744 未加载
评论 #42387648 未加载
评论 #42387741 未加载