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.

Fun with -fsanitize=undefined and Picolibc

118 pointsby JNRoweabout 1 month ago

7 comments

zero_kabout 1 month ago
Wow, this: &quot;random() was returning values in int range rather than long.&quot; is a very nice bug find. Randomness is VERY hard to check for humans. For example, Python&#x27;s binomial distribution is very bad on some inputs [1], giving widely wrong values, but nobody found it. I bumped into it when I implemented an algorithm to compute the approximate volume of solutions to a DNF, and the results were clearly wrong [2]. The algorithm is explained here by Knuth, in case you are interested [3]<p>[1] <a href="https:&#x2F;&#x2F;www.cs.toronto.edu&#x2F;~meel&#x2F;Slides&#x2F;meel-distform.pdf" rel="nofollow">https:&#x2F;&#x2F;www.cs.toronto.edu&#x2F;~meel&#x2F;Slides&#x2F;meel-distform.pdf</a> [2] <a href="https:&#x2F;&#x2F;github.com&#x2F;meelgroup&#x2F;pepin">https:&#x2F;&#x2F;github.com&#x2F;meelgroup&#x2F;pepin</a> [3] <a href="https:&#x2F;&#x2F;cs.stanford.edu&#x2F;~knuth&#x2F;papers&#x2F;cvm-note.pdf" rel="nofollow">https:&#x2F;&#x2F;cs.stanford.edu&#x2F;~knuth&#x2F;papers&#x2F;cvm-note.pdf</a>
nasretdinovabout 1 month ago
&gt; String to float conversion had a table missing four values. This caused an array access overflow which resulted in imprecise values in some cases.<p>I&#x27;ve once wrote a function to parse the date format from log files that Go doesn&#x27;t natively support, and forgot to add November. I quit that job in April, so I never saw any issues. However when 1st of November came my ex-colleagues saw no logs for this day, and when they found out the reason they created a hash tag #nolognovember which you can probably find somewhere to this day :)
评论 #43704074 未加载
评论 #43703687 未加载
bestouffabout 1 month ago
&gt; the vast bulk of sanitizer complaints came from invoking undefined or implementation-defined behavior in harmless ways<p>This is patently false. <i>Any</i> Undefined Behavior is harmful because it allows the optimizer to insert totally random code, and this is not a purely theoretical behavior, it&#x27;s been repeatedly demonstrated happening. So even if your UB code isn&#x27;t called, the simple fact it exists may make some seemingly-unrelated code behave wrongly.
评论 #43706303 未加载
评论 #43707281 未加载
评论 #43706089 未加载
评论 #43705038 未加载
moefhabout 1 month ago
&gt; Passing pointers to the middle of a data structure. For example, free takes a pointer to the start of an allocation. The management structure appears just before that in memory; computing the address of which appears to be undefined behavior to the compiler.<p>To clarify, the undefined behavior here is that the sanitizer sees `free` trying to access memory outside the bounds of what was returned by `malloc`.<p>It&#x27;s perfectly valid to compute the address of a struct just before memory pointed to by a pointer you have, as long as the result points to valid memory:<p><pre><code> void not_free(void *p) { struct header *h = (struct header *) (((char *)p) - sizeof(struct header)); &#x2F;&#x2F; ... } </code></pre> In the case of `free`, that resulting pointer is technically &quot;invalid&quot; because it&#x27;s outside what was returned by `malloc`, even though the implementation of `malloc` presumably returned a pointer to memory just past the header.
评论 #43705931 未加载
juliangmpabout 1 month ago
&gt; [...] detect places where the program wanders into parts of the C language specification [...]<p>Small nitpick, the UB sanitizer also has some checks specific for C++ <a href="https:&#x2F;&#x2F;clang.llvm.org&#x2F;docs&#x2F;UndefinedBehaviorSanitizer.html" rel="nofollow">https:&#x2F;&#x2F;clang.llvm.org&#x2F;docs&#x2F;UndefinedBehaviorSanitizer.html</a>
musicaleabout 1 month ago
And don&#x27;t forget -fbounds-safety, which is in Apple&#x27;s clang&#x2F;llvm and perhaps other versions. <a href="https:&#x2F;&#x2F;clang.llvm.org&#x2F;docs&#x2F;BoundsSafety.html" rel="nofollow">https:&#x2F;&#x2F;clang.llvm.org&#x2F;docs&#x2F;BoundsSafety.html</a>
Arnavionabout 1 month ago
That arithmetic shift right implementation is also what I came up with for a video game fantasy architecture that only has logical shift right. (16-bit registers)<p><pre><code> ; asr rd, rs1, rs2 ; rd = signed(rs1) &gt;&gt; rs2 and rt, rs1, 0x8000 ; isolate sign bit lsr rt, rt, rs2 ; shift sign bit to final position neg rt, rt ; sign-extended part of final result lsr rd, rs1, rs2 ; base part of final result or rd, rd, rt ; combine both parts </code></pre> It might be easier to understand broken down this way for anyone who didn&#x27;t understand the article&#x27;s one-liner.