2 ms·
`s` may be null, and so the strlen may seg fault.
by WalterBright 11mo ago
`s` may be null, and so the strlen may seg fault.
- pianom4n 11mo agoBut that's undefined behavior, so the compiler is free to ignore that possibility.
- WalterBright 11mo ago> so the compiler is free to ignore that possibility And that's what is wrong. This is the most unfriendly behavior towards the programmer.
- flqn 11mo agoSince the optimiser is allowed to assume you're not invoking UB, and strlen of null is UB, I don't believe that it would consider that case when optimising this function.
- WalterBright 11mo agoI understand that, but I don't agree that such optimizer behavior is worth it and I won't put it in my compilers.
- kragen 11mo agoI appreciate that greatly.
- WalterBright 11mo agoThe notion that because it is undefined behavior means that the compiler is free to replace it with anything up to and including "launch nuclear missiles". This is just nuts. If I program it to cause a null pointer seg fault, I expect a null pointer seg fault. If I program it to cause a twos complement overflow, I want a twos complement overflow.
- kragen 11mo agoYeah, I feel the same way. It's refreshing to hear that that's not just because I'm insane. I think C compiler teams are sort of forced into this stupid shit because they don't have new CPU architectures to port to anymore, so, unless they want to go find new jobs, they're forced to waste their time and everyone else's by "improving" the compilers by increasing performance in riskier and riskier ways.
- deleted 11mo ago[deleted]