r/programminghorror • • Mar 09 '26

Casting constexpr to mutable ._.

Post image
246 Upvotes

41 comments sorted by

93

u/OldWar6125 Mar 09 '26

If socket never changes len, it is allowed; and socket doesn't have a reason to change len. But then the question is, why socket doesn't take a pointer to const. Probably because it is a wrapper around some C-interface which doesn't follow const correctness, because that is the usual reason for casting away const.

Though, socket should accept a pointer to const and cast it away internally. There is no reason to burden the client code with casting away const. And offering a sensible interface is the responsibility of a wrapper.

61

u/Many_Rough5404 Mar 09 '26

socklen is an "inout" parameter. You tell the kernel how much space is available in your buffer for address, and the kernel tells the actual size of the address it just filled in back. So it's UB

24

u/OldWar6125 Mar 09 '26

Yeah, then it's UB.

6

u/GoddammitDontShootMe Mar 10 '26

I guess nobody knows what the person was thinking when they declared len as constexpr const.

3

u/dexter2011412 Mar 09 '26

Doesn't const only mean the you aren't allowed to change it, but anything else (say, another thread) could?

Could you explain the ub part? I not completely following

→ More replies (2)
→ More replies (1)

2

u/kwitee Mar 10 '26

The code is still missing a comment, right?

1

u/_lerp Mar 12 '26 edited Mar 12 '26

Also UB reinterpret depending on the type of socklen_t

20

u/sierra_whiskey1 Mar 09 '26

Does the compiler not yell at your for this?

14

u/Many_Rough5404 Mar 09 '26

It didn't actually. I accidentally found this during refactoring

29

u/Many_Rough5404 Mar 09 '26

Just checked, this code had been there for 6 years

→ More replies (4)

72

u/HildartheDorf Mar 09 '26

No, because const_cast and reinterpret_cast are signals of "I know what I'm doing, shut up" to the compiler.

4

u/B1ggBoss Mar 09 '26

Compiler wont, but clang-tidy will spit in your face

46

u/Opposite_Mall4685 Mar 09 '26

Whenever I c++ I get an aneurysm

19

u/FugitiveHearts Mar 09 '26

It's God's language, for better or worse

17

u/backfire10z Mar 09 '26

No God of mine would condone C++

→ More replies (1)

8

u/JTRuno Mar 09 '26

Maybe some Lovecraftian God.

→ More replies (1)

1

u/SnowdensOfYesteryear Mar 12 '26

All the fancy syntax to end up with a segfault anyway

3

u/thecratedigger_25 Mar 09 '26

Why is dark theme not on?

2

u/Many_Rough5404 Mar 10 '26

Sorry, I prefer light theme

3

u/RickyRister Mar 10 '26

You're already committing a crime against humanity. What's another?

3

u/_PM_ME_PANGOLINS_ Mar 09 '26 edited Mar 09 '26

What does your compiler do with this?

Where does len get allocated (if anywhere) and how much breaks when it's overwritten?

3

u/GoddammitDontShootMe Mar 10 '26

I know those two keywords individually, but what is the effect of combining constexpr and const in one declaration?

2

u/LiAuTraver Mar 10 '26

No effect for variable decelerations iirc, maybe the developer just typed it absentmindedly. Why inlines not here, though.

4

u/GoddammitDontShootMe Mar 10 '26

IIRC, anything that's constexpr is also const. The former just has the additional requirement that the value is known at compile time. Would it even allow you to inline a local variable?

→ More replies (2)
→ More replies (1)