Post B6tXpso9Uzw3VDIxjE by lilyyllyyllyly@mastodon.gamedev.place
(DIR) More posts by lilyyllyyllyly@mastodon.gamedev.place
(DIR) Post #B6tRGakU3EhjdHNT9c by linear@nya.social
0 likes, 1 repeats
this is your irregular reminder that it is not safe to treat C++ as a superset of C because C++ adds a whole lot of new and potentially surprising places where undefined behavior can be found, and there exists lots of "safe" and acceptable C code that it is an extremely bad idea to compile as C++https://godbolt.org/z/neYY9rbGc
(DIR) Post #B6tXN6Q1oacJJmIVRg by linear@nya.social
0 likes, 0 repeats
ignore the warning about the string constant, i accidentally left a line in that doesn't affect the end result.as proof, test.c being copied to test.cpp before compiling (which i meant to do in the first place), and getting the same result, minus that warning.
(DIR) Post #B6tXpso9Uzw3VDIxjE by lilyyllyyllyly@mastodon.gamedev.place
0 likes, 0 repeats
@linear I'm so confused, what's going on here? Where does the line in the warning come from? How is the evil function called?
(DIR) Post #B6tY4kz0sESXZtTefA by linear@nya.social
0 likes, 0 repeats
@lilyyllyyllyly@mastodon.gamedev.place sorry, see the updated image, i accidentally left a line in, i meant to cp test.c test.cpp in between. the warning about the string constant does not affect the result.https://nya.social/notes/819e84041e289d5f6bae05d9the other warning, however, is a clue. the key to why the evil function is called has to do with undefined behavior, and the fact that the good function is declared int but does not ever return a value.C says this is fine, because the value is never used. only upon using the return value of the function do you enter the world of undefined behavior.C++, however, says that this is undefined behavior even if the value is never used, and your compiler will happily exercise the wiggle room you've given it and leave out the ret instruction at the end of the function.so, when the good function is called, it never returns, instead falling through to what is below it.RE: https://nya.social/notes/819e84041e289d5f6bae05d9
(DIR) Post #B6tYXc9UmmpwaT8msS by lilyyllyyllyly@mastodon.gamedev.place
0 likes, 0 repeats
@linear oh wow! I was thinking it must've been about the return type, but I couldn't think of how that'd possibly cause it to call that function... it just not returning and continuing execution is crazy behaviour! Thanks for the explaination!
(DIR) Post #B6tqvsVXQUnLRDAdIe by beepster4096@furry.engineer
0 likes, 0 repeats
@linear it baffles me that cpp compilers don't emit ud2 on unreachable paths by default and just let them dangle
(DIR) Post #B6tqvshEj09W1UdyXA by linear@nya.social
0 likes, 0 repeats
@beepster4096@furry.engineer if you remove the "-O1" from the compile args on the godbolt page, you'll notice that it does exactly that.
(DIR) Post #B6u28BCopeayOqU12m by beepster4096@furry.engineer
0 likes, 0 repeats
@linear Yeah that makes sense, tho I meant with optimizations too. I can't imagine not putting those in is that much of a binary size benefit, especially in comparison to the dangers of runaway code execution
(DIR) Post #B6vTYj8sAint6KTr1M by zardoru@dragon.style
0 likes, 0 repeats
@linear casts to and from void* have gotta be my favorite ub
(DIR) Post #B6vThdPcqQC1w8Z1lo by linear@nya.social
0 likes, 0 repeats
@zardoru@dragon.style at least memcpy is always† safe...†: terms and conditions may apply