toad.social is one of the many independent Mastodon servers you can use to participate in the fediverse.
Mastodon server operated by David Troy, a tech pioneer and investigative journalist addressing threats to democracy. Thoughtful participation and discussion welcome.

Administered by:

Server stats:

368
active users

#include

4 posts2 participants0 posts today
Karl Baron<p>It looks like CW by default auto-includes any Mac headers you need, but at the same time stdlib stuff like "strlen" isn't working even with a manual <a href="https://bitbang.social/tags/include" class="mention hashtag" rel="nofollow noopener noreferrer" target="_blank">#<span>include</span></a> &lt;strings.h&gt; which is the point where I'm starting to look at "maybe I should read a book instead of just poking at things randomly!" (2/2)</p>
Jann Horn<p><span class="h-card" translate="no"><a href="https://infosec.exchange/@jduck" class="u-url mention" rel="nofollow noopener noreferrer" target="_blank">@<span>jduck</span></a></span> I mean, if it convinces you...</p><pre><code>$ cat blub.c<br>#include &lt;stdlib.h&gt;<br>#include &lt;stdio.h&gt;<br>struct foo {<br> long a;<br> int b;<br> int arr[];<br>};<br>int main(void) {<br> struct foo *a = malloc(sizeof(struct foo) + sizeof(int)*3);<br> struct foo *b = malloc(sizeof(struct foo) + sizeof(int)*3);<br> if (!a || !b)<br> return 1;<br><br> a-&gt;a = 1;<br> a-&gt;b = 2;<br> for (int i=0; i&lt;3; i++)<br> a-&gt;arr[i] = i + 123456;<br><br> b-&gt;a = 101;<br> b-&gt;b = 102;<br> for (int i=0; i&lt;3; i++)<br> b-&gt;arr[i] = i + 100;<br><br> *b = *a;<br><br> for (int i=0; i&lt;3; i++)<br> printf("%d ", b-&gt;arr[i]);<br> printf("\n");<br>}<br>$ gcc -o blub blub.c<br>$ ./blub<br>123456 101 102<br></code></pre>
Jann Horn<p>Why does the C standard not mention any UB in its definition of offsetof()? Is it implicit somewhere else that doing <code>offsetof(struct foo, arr[SIZE_MAX])</code> on a struct containing a flexible array member is UB?</p><p>Both GCC and clang don't diagnose anything here and compile it to a "return 0":</p><pre><code>#include &lt;stddef.h&gt;<br>#include &lt;inttypes.h&gt;<br>struct foo {<br> int a;<br> int arr[];<br>};<br>unsigned long func(void) {<br> return offsetof(struct foo, arr[SIZE_MAX]);<br>}<br></code></pre><p><a href="https://godbolt.org/z/rx7rqvh7j" rel="nofollow noopener noreferrer" translate="no" target="_blank"><span class="invisible">https://</span><span class="">godbolt.org/z/rx7rqvh7j</span><span class="invisible"></span></a></p>
Jann Horn<p>Funny standard C behavior - assigning a struct with flexible array member can copy <em>parts of</em> the flexible array member:</p><pre><code>$ cat vla_assign.c<br>#include &lt;stdlib.h&gt;<br>#include &lt;stdio.h&gt;<br>struct foo {<br> long a;<br> int b;<br> int arr[];<br>};<br>int main(void) {<br> struct foo *a = malloc(sizeof(struct foo) + sizeof(int)*3);<br> struct foo *b = malloc(sizeof(struct foo) + sizeof(int)*3);<br> if (!a || !b)<br> return 1;<br><br> a-&gt;a = 1;<br> a-&gt;b = 2;<br> for (int i=0; i&lt;3; i++)<br> a-&gt;arr[i] = i;<br><br> b-&gt;a = 101;<br> b-&gt;b = 102;<br> for (int i=0; i&lt;3; i++)<br> b-&gt;arr[i] = i + 100;<br><br> *b = *a;<br><br> for (int i=0; i&lt;3; i++)<br> printf("%d ", b-&gt;arr[i]);<br> printf("\n");<br>}<br>$ gcc -Wall -Wextra -pedantic -o vla_assign vla_assign.c<br>$ ./vla_assign<br>0 101 102<br></code></pre>
russell<p><span class="h-card" translate="no"><a href="https://mastodon.social/@whitequark" class="u-url mention" rel="nofollow noopener noreferrer" target="_blank">@<span>whitequark</span></a></span> <span class="h-card" translate="no"><a href="https://chaos.social/@ronya" class="u-url mention" rel="nofollow noopener noreferrer" target="_blank">@<span>ronya</span></a></span> That is wild because the whole reason for that separate parser is to be able to reindex individual files without paying attention to the <a href="https://mastodon.social/tags/include" class="mention hashtag" rel="nofollow noopener noreferrer" target="_blank">#<span>include</span></a> graph or anything.</p><p>It doesn't do this for me, but that's using "full" VS, which shares the parser(s) but not the layer that orchestrates them. (But also both VS and Code are prone to behaving in similarly broken ways.)</p>
Simon Zerafa<p><span class="h-card" translate="no"><a href="https://mastodon.social/@Wileymiller" class="u-url mention" rel="nofollow noopener noreferrer" target="_blank">@<span>Wileymiller</span></a></span> </p><p><a href="https://infosec.exchange/tags/Include" class="mention hashtag" rel="nofollow noopener noreferrer" target="_blank">#<span>Include</span></a> </p>
Shafik Yaghmour<p>Retro C++ quiz #26</p><p><a href="https://hachyderm.io/tags/include" class="mention hashtag" rel="nofollow noopener noreferrer" target="_blank">#<span>include</span></a> &lt;cmath&gt;</p><p>auto f(unsigned int x) {<br> return std::abs(x); // Well-formed?<br>}</p><p>Without checking:</p><p>A. Yes<br>B. No<br>C: Show answers</p><p><a href="https://hachyderm.io/tags/cplusplus" class="mention hashtag" rel="nofollow noopener noreferrer" target="_blank">#<span>cplusplus</span></a><br><a href="https://hachyderm.io/tags/Cpppolls" class="mention hashtag" rel="nofollow noopener noreferrer" target="_blank">#<span>Cpppolls</span></a></p>
Jann Horn<p>fun Linux fact: because <code>MAP_SHARED|MAP_ANONYMOUS</code> is actually a file-backed mapping under the hood, unmapping part of such a mapping does not discard the data stored in that part:</p><pre><code>$ cat mremap.c<br>#define _GNU_SOURCE<br>#include &lt;err.h&gt;<br>#include &lt;stdio.h&gt;<br>#include &lt;sys/mman.h&gt;<br>int main(void) {<br> char *p = mmap(NULL, 0x2000, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANONYMOUS, -1, 0);<br> if (p == MAP_FAILED) err(1, "mmap");<br> p[0x1000] = 'X';<br> if (munmap(p+0x1000, 0x1000)) err(1, "munmap");<br> // that 'X' we just wrote... is it gone?<br> // nope, let's bring it back!<br> p = mremap(p, 0x1000, 0x2000, MREMAP_MAYMOVE);<br> if (p == MAP_FAILED) err(1, "mremap");<br> printf("p[0x1000]='%c'\n", p[0x1000]);<br>}<br>$ gcc -o mremap mremap.c<br>$ ./mremap<br>p[0x1000]='X'<br>$<br></code></pre>
prozacchiwawa<p>ok i hit this weekend's goal:</p><p><a href="https://functional.cafe/tags/include" class="mention hashtag" rel="nofollow noopener noreferrer" target="_blank">#<span>include</span></a> &lt;windows.h&gt;</p><p>int main(int argc, char **argv) {<br> char computer_name_buf[1024];<br> DWORD buf_size = sizeof(computer_name_buf);<br> GetComputerNameA(computer_name_buf, &amp;buf_size);<br> printf("hi from main: computer name %s\n", computer_name_buf);<br> return 0;<br>}</p>
Björkus "No time_t to Die" Dorkus<p>Time to get printf to the SIZE_MAX off the ground. Especially since it's not the type that matters for the precision specifier for the string size, it's the actual value.</p><pre><code>#include &lt;stdio.h&gt;<br>#include &lt;stdlib.h&gt;<br>#include &lt;limits.h&gt;<br>#include &lt;assert.h&gt;<br><br>int main() {<br> enum { COUNT = 10, BYTESIZE = INT_MAX / COUNT };<br> char* str = (char*)malloc(BYTESIZE + 1);<br> for (size_t i = 0; i &lt; BYTESIZE; ++i) {<br> str[i] = 'a';<br> }<br> str[BYTESIZE] = '\0';<br> FILE* f = fopen("/dev/null", "w+");<br> [[maybe_unused]] int write_value = fprintf(f, "%.*s", BYTESIZE, str);<br> [[maybe_unused]] int large_write_value = fprintf(f, "%.*s %*.s %*.s %*.s %*.s %*.s %*.s %*.s %*.s %*.s %*.s",<br> BYTESIZE, str, BYTESIZE, str, BYTESIZE, str, BYTESIZE, str, BYTESIZE, str, BYTESIZE, str, BYTESIZE, str,<br> BYTESIZE, str, BYTESIZE, str, BYTESIZE, str, BYTESIZE, str);<br> free(str);<br> assert(write_value == BYTESIZE); // Well.<br> assert(large_write_value == -1); // ... Okay.<br> // this should let the other thing work out nicely, then.<br> return 0;<br>}<br></code></pre>
Gareth Lloyd (He/him)<p>Did you extract anything into a function?<br>Did you give it a good name?</p><p>Have a look in `<a href="https://fosstodon.org/tags/include" class="mention hashtag" rel="nofollow noopener noreferrer" target="_blank">#<span>include</span></a> &lt;algorithm&gt;` does it already exist?</p><p><a href="https://en.cppreference.com/w/cpp/algorithm" rel="nofollow noopener noreferrer" translate="no" target="_blank"><span class="invisible">https://</span><span class="ellipsis">en.cppreference.com/w/cpp/algo</span><span class="invisible">rithm</span></a></p>
Jann Horn<p>Wow, <a href="https://clang.llvm.org/docs/LanguageExtensions.html#builtin-dump-struct" rel="nofollow noopener noreferrer" target="_blank"><code>__builtin_dump_struct</code></a> is an amazing clang feature, how did I never hear about this before?</p><pre><code>$ cat test.c<br>#include &lt;stdio.h&gt;<br><br>struct nested {<br> int n;<br>};<br>struct foo {<br> int member_a;<br> unsigned long member_b;<br> char *str;<br> void *ptr;<br> struct nested nested;<br>};<br><br>int main(void) {<br> struct foo f = {<br> .member_a = 123,<br> .member_b = 0x4141414141414141,<br> .str = "foobar",<br> .ptr = &amp;f,<br> .nested = {.n = 42}<br> };<br> __builtin_dump_struct(&amp;f, printf);<br>}<br>$ clang -o test test.c &amp;&amp; ./test<br>struct foo {<br> int member_a = 123<br> unsigned long member_b = 4702111234474983745<br> char * str = "foobar"<br> void * ptr = 0x7fff1df41b78<br> struct nested nested = {<br> int n = 42<br> }<br>}<br></code></pre><p>The <a href="https://github.com/llvm/llvm-project/commit/0652534131b269eec336a2ed3545642cc01a1300" rel="nofollow noopener noreferrer" target="_blank">original version</a> of this feature was introduced back in 2018 (though it was <a href="https://github.com/llvm/llvm-project/commit/c4f95ef86a224fe730d2219aab90e88a0e7b03d2" rel="nofollow noopener noreferrer" target="_blank">reimplemented since</a> in 2022).</p>
Karsten Johansson<p><span class="h-card" translate="no"><a href="https://mastodon.sdf.org/@screwtape" class="u-url mention" rel="nofollow noopener noreferrer" target="_blank">@<span>screwtape</span></a></span> <span class="h-card" translate="no"><a href="https://mstdn.social/@praetor" class="u-url mention" rel="nofollow noopener noreferrer" target="_blank">@<span>praetor</span></a></span> <span class="h-card" translate="no"><a href="https://infosec.exchange/@untakenusername" class="u-url mention" rel="nofollow noopener noreferrer" target="_blank">@<span>untakenusername</span></a></span> <span class="h-card" translate="no"><a href="https://infosec.exchange/@bughuntercat" class="u-url mention" rel="nofollow noopener noreferrer" target="_blank">@<span>bughuntercat</span></a></span> Just to beat the hell out of a dead horse (this one took longer):</p><p>C:<br><a href="https://infosec.exchange/tags/include" class="mention hashtag" rel="nofollow noopener noreferrer" target="_blank">#<span>include</span></a> <br><a href="https://infosec.exchange/tags/include" class="mention hashtag" rel="nofollow noopener noreferrer" target="_blank">#<span>include</span></a> <br>int main() {<br> int* arr = malloc(5 * sizeof(int));<br> if (!arr) return 1; // Check allocation success<br> for (int i = 0; i &lt; 5; i++) {<br> arr[i] = i * 10;<br> }<br> for (int i = 0; i &lt; 5; i++) {<br> printf("%d ", arr[i]);<br> }<br> free(arr);<br> return 0;<br>}</p><p>Common Lisp:<br>(loop for i from 0 below 5<br> collect (* i 10) into arr<br> finally (print arr))</p><p>Which language does low level things easier? Clearly Common Lisp does... (even if this is high level representation of low level things...) :ablobcatrave:</p>
Karsten Johansson<p><span class="h-card" translate="no"><a href="https://mastodon.sdf.org/@screwtape" class="u-url mention" rel="nofollow noopener noreferrer" target="_blank">@<span>screwtape</span></a></span> <span class="h-card" translate="no"><a href="https://mstdn.social/@praetor" class="u-url mention" rel="nofollow noopener noreferrer" target="_blank">@<span>praetor</span></a></span> <span class="h-card" translate="no"><a href="https://infosec.exchange/@untakenusername" class="u-url mention" rel="nofollow noopener noreferrer" target="_blank">@<span>untakenusername</span></a></span> <span class="h-card" translate="no"><a href="https://infosec.exchange/@bughuntercat" class="u-url mention" rel="nofollow noopener noreferrer" target="_blank">@<span>bughuntercat</span></a></span> This deserves an example.</p><p>C:<br><a href="https://infosec.exchange/tags/include" class="mention hashtag" rel="nofollow noopener noreferrer" target="_blank">#<span>include</span></a> <br><a href="https://infosec.exchange/tags/include" class="mention hashtag" rel="nofollow noopener noreferrer" target="_blank">#<span>include</span></a> <br>int main() {<br> uint8_t x = 0xA5;<br> // Extract the high nibble: shift right by 4 bits then mask<br> uint8_t high_nibble = (x &gt;&gt; 4) &amp; 0x0F;<br> printf("High nibble: %X\n", high_nibble);<br> return 0;<br>}</p><p>Common Lisp:<br>(let ((x <a href="https://infosec.exchange/tags/xA5" class="mention hashtag" rel="nofollow noopener noreferrer" target="_blank">#<span>xA5</span></a>))<br> (format t "High nibble: ~X~%" (ldb (byte 4 4) x)))</p><p>This is how I started using <a href="https://infosec.exchange/tags/lisp" class="mention hashtag" rel="nofollow noopener noreferrer" target="_blank">#<span>lisp</span></a> from the very early days. So I see it as an easier way to do low level things. The rest (OOP and all) are things pretty much all modern languages do easily.</p><p>I recently found one of my first <em>very</em> low level Lisp programs. I'll post it one day. It is ethically questionable though, because it outputs DOS-based viruses lol.</p>
mcc<p>If you want me to hate you forever, a good way to do this is to share sample code as a isolated function in a blockquote, which produces a dozen 'symbol not found' errors when punched into actual code because you *didn't* include a blockquote containing the half-dozen <a href="https://mastodon.social/tags/include" class="mention hashtag" rel="nofollow noopener noreferrer" target="_blank">#<span>include</span></a> or `using` or `import` statements that would be needed to bring the various API calls and types you reference into scope</p>
phase separation anxiolyte<p><span class="h-card" translate="no"><a href="https://infosec.exchange/@atax1a" class="u-url mention" rel="nofollow noopener noreferrer" target="_blank">@<span>atax1a</span></a></span> right.<br><a href="https://mastodon.mit.edu/tags/include" class="mention hashtag" rel="nofollow noopener noreferrer" target="_blank">#<span>include</span></a> &lt;stdio.h&gt; probably 2</p>
Taxo Rubio:wq<p><span class="h-card" translate="no"><a href="https://chaos.social/@root42" class="u-url mention" rel="nofollow noopener noreferrer" target="_blank">@<span>root42</span></a></span> <span class="h-card" translate="no"><a href="https://social.tchncs.de/@cgudrian" class="u-url mention" rel="nofollow noopener noreferrer" target="_blank">@<span>cgudrian</span></a></span> Checking it out now, I just got bitten by integer promotion rules again. Rip.</p><p>The i/j example still yields 255:</p><p>(gdb) l<br>1 <a href="https://fosstodon.org/tags/include" class="mention hashtag" rel="nofollow noopener noreferrer" target="_blank">#<span>include</span></a> &lt;cstdint&gt;<br>2 <br>3 int main () {<br>4 uint8_t i = 0;<br>5 uint8_t j = (i-1) % 128;<br>6 }<br>(gdb) br 6<br>Breakpoint 1 at 0x113c: file a.cpp, line 6.<br>(gdb) r<br>Starting program: /home/groctel/a.out<br>Breakpoint 1, main () at a.cpp:6<br>6 }<br>(gdb) p i<br>$1 = 0 '\000'<br>(gdb) p j<br>$2 = 255 '\377'</p>
root42<p><span class="h-card" translate="no"><a href="https://social.tchncs.de/@cgudrian" class="u-url mention" rel="nofollow noopener noreferrer" target="_blank">@<span>cgudrian</span></a></span> Casts seem to handle correctly. I tried this one:<br><a href="https://chaos.social/tags/include" class="mention hashtag" rel="nofollow noopener noreferrer" target="_blank">#<span>include</span></a> &lt;stdio.h&gt;<br><a href="https://chaos.social/tags/include" class="mention hashtag" rel="nofollow noopener noreferrer" target="_blank">#<span>include</span></a> &lt;stdint.h&gt;</p><p>int main(void)<br>{<br> unsigned int i = 65535;<br> uint8_t j = (uint8_t)i;<br> printf("%u %u\n", i, j);<br> i--;<br> j = (uint8_t)i;<br> printf("%u %u\n", i, j);<br> return 0;<br>}</p><p>Which leads the correct results, just as clang</p>
root42<p><span class="h-card" translate="no"><a href="https://social.tchncs.de/@cgudrian" class="u-url mention" rel="nofollow noopener noreferrer" target="_blank">@<span>cgudrian</span></a></span> My test program looks like this now:<br><a href="https://chaos.social/tags/include" class="mention hashtag" rel="nofollow noopener noreferrer" target="_blank">#<span>include</span></a> &lt;stdint.h&gt;<br><a href="https://chaos.social/tags/include" class="mention hashtag" rel="nofollow noopener noreferrer" target="_blank">#<span>include</span></a> &lt;stdio.h&gt;</p><p>int main(void)<br>{<br> for(int i = 0; i &lt; 3; i++) {<br> uint8_t c1 = (i - 1) &amp; 255;<br> uint8_t c2 = i - 1;<br> printf("%u %u\n", c1, c2);<br> }<br> return 0;<br>}<br>And it produces the "correct" result with bit masking, and is faulty without.</p>
root42<p>To my <a href="https://chaos.social/tags/c" class="mention hashtag" rel="nofollow noopener noreferrer" target="_blank">#<span>c</span></a> and <a href="https://chaos.social/tags/cxx" class="mention hashtag" rel="nofollow noopener noreferrer" target="_blank">#<span>cxx</span></a> <a href="https://chaos.social/tags/cpp" class="mention hashtag" rel="nofollow noopener noreferrer" target="_blank">#<span>cpp</span></a> bubble: Why does a uint8_t get promoted to uint when doing -1 on it? Example:</p><p><a href="https://chaos.social/tags/include" class="mention hashtag" rel="nofollow noopener noreferrer" target="_blank">#<span>include</span></a> &lt;stdio.h&gt;<br><a href="https://chaos.social/tags/include" class="mention hashtag" rel="nofollow noopener noreferrer" target="_blank">#<span>include</span></a> &lt;stdint.h&gt;</p><p>int main(void)<br>{<br> uint8_t c1 = 0;<br> printf("%u\n", c-1);<br> return 0;<br>}</p><p>This will print (uint8_t)(c1-1) if you don't cast: (uint8_t)(c1-1)</p>