Open post none gender with left politics :cross_fox: @vikxin@vulpine.club · 43mo ago Replying to @slomo@toot.cat <p>PSA: Did you know that function argument evaluation order in C is undefined, and gcc / clang are actually doing the opposite of each other?</p><p>I did, but that knowledge didn't make today's debugging session much shorter and less puzzling.</p><p>The starting point was that the same code worked fine on Linux but doesn't work on macOS. Nothing system-specific anywhere near the error.</p><p>As it turns out, somewhere deep down there was some code relying on a specific argument evaluation order. Something in the shape of</p><pre><code>copy(reader_read_data(r, reader_get_remaining(r)), reader_get_remaining(r));<br></code></pre><p>With gcc this copied the remaining amount of data, with clang this silently copied nothing. gcc is evaluating arguments right-to-left, clang left-to-right. So clang was first advancing the cursor to the end, and then the remaining amount of data was actually 0.</p><p>Should you write such code? No. Does it make sense for the code to behave differently with different, well-behaving compilers? Also no.</p><p>The code in question did not cause any compiler warnings, and no static code analyser I tried (including coverity) detected it either.</p><p>Fortunately another thing Rust got right: argument evaluation is explicitly defined as left-to-right.</p><p>And this is just one of the parts of C with undefined, unspecified or implementation-defined behaviour. Far too many footguns to keep in mind to realistically avoid having one or another showing up somewhere sooner or later.</p><p><a href="https://toot.cat/tags/C" class="mention hashtag" rel="tag">#<span>C</span></a> <a href="https://toot.cat/tags/Programming" class="mention hashtag" rel="tag">#<span>Programming</span></a> <a href="https://toot.cat/tags/Bug" class="mention hashtag" rel="tag">#<span>Bug</span></a> <a href="https://toot.cat/tags/FootGun" class="mention hashtag" rel="tag">#<span>FootGun</span></a> <a href="https://toot.cat/tags/RustLang" class="mention hashtag" rel="tag">#<span>RustLang</span></a></p> @slomo@toot.cat @KitRedgrave@vulpine.club oh god, sequence points in C are such a mess