Ancestors

Written by Christian Brauner 🦊🐺 on 2024-09-09 at 14:21

Why on earth do they want me to pay CHF 216 for the C standard https://www.iso.org/standard/74528.html

Is there a free version of this? And isn't this weird that a lot of OSS is written in C but the standard is behind a big fat paywall?

I just have question about unions...

=> More informations about this toot | More toots from brauner@mastodon.social

Written by Christian Brauner 🦊🐺 on 2024-09-09 at 14:37

Basically my questions comes down to whether in an anoymous union like:

struct foo {
        union {
                struct {
                        struct bar b[5]; /* 60 bytes */
                        u32 nr_entries;
                };
                struct {
                        void *p;
                        void *q;
                };
        };
};

I can use nr_entries to index p/q and b.

=> More informations about this toot | More toots from brauner@mastodon.social

Written by Christian Brauner 🦊🐺 on 2024-09-09 at 14:38

IOW, if I did:

struct foo meh = {

   .nr_entries = 1234,

   .p = some_array;

   .q = some_other_array;

};

can I rely on that no being undefined behavior. I think C11 guarantees this type punning.

=> More informations about this toot | More toots from brauner@mastodon.social

Toot

Written by Christian Brauner 🦊🐺 on 2024-09-09 at 14:46

https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2310.pdf

"99) If the member used to read the contents of a union object is not the same as the member last used to store a value in the

object, the appropriate part of the object representation of the value is reinterpreted as an object representation in the new

type as described in 6.2.6 (a process sometimes called "type punning"). This might be a trap representation."

=> More informations about this toot | More toots from brauner@mastodon.social

Descendants

Written by Jann Horn on 2024-09-09 at 17:04

@brauner

waaait... the C standard says union members do type punning?

how the heck can they possibly guarantee that when type-based anti-aliasing is used?

like, this clang example shows why that can't really work:

https://godbolt.org/z/c38K61Th5

struct bar { long c; };

long blah(struct foo *f, struct bar *b) {

f->a = 1;

long l = b->c;

f->a = 2;

return l;

}

union u {

struct foo foo;

struct bar bar;

};

long bar_union(union u *u) {

return blah(&u->foo, &u->bar);

}

output:

    mov     rax, qword ptr [rdi]

    mov     dword ptr [rdi], 2

    ret

How does the standard handle this - is there some separate rule in the standard that forbids passing pointers to two different union members to the same function or what?

(sidenote: The Linux kernel disables strict aliasing rules in the compiler, so strict aliasing concerns don't really apply in the kernel.)

=> More informations about this toot | More toots from jann@infosec.exchange

Written by Joshua J. Drake on 2024-09-09 at 19:58

@jann @brauner C, especially with unions, is YOLO

=> More informations about this toot | More toots from jduck@infosec.exchange

Written by Siguza on 2024-09-09 at 20:21

@jann @brauner last I checked, the standard didn't handle this at all, and it was subject to debates because it means that strict aliasing is void, but compiler people don't like that.

=> More informations about this toot | More toots from siguza@infosec.space

Proxy Information
Original URL
gemini://mastogem.picasoft.net/thread/113108134931849877
Status Code
Success (20)
Meta
text/gemini
Capsule Response Time
287.630296 milliseconds
Gemini-to-HTML Time
2.021837 milliseconds

This content has been proxied by September (ba2dc).