Re:Curl is C (2017)
by 0xTJ on Thursday February 18, 2021 @09:43AM
(#26180288)
The code you're compiling is flat-out wrong though. This is exactly a nasal demons situation. Since `Do` is static, the compiler can guarantee that the only possible place it could be set in this file is in `NeverCalled`. Since calling `Do` is only ever valid after that line in `NeverCalled`, it can safely assume that you'd only ever call `Do` after it's been set to `EraseAll`. Anything else is 100% undefined behaviour. You should have a check on the value of `Do` or have a default value.
[ Parent
| on HN ]
Re:Curl is C (2017)
by kevinmgranger on Thursday February 18, 2021 @12:05PM
(#26182425)
This boils down to "just don't do undefined behavior". It's 100% correct and misses the point entirely.[1]
1: https://nibblestew.blogspot.com/2020/04/your-statement-is-10...
I don't think OP is arguing that this is a violation of the language spec; they're arguing that the spec (as implemented) is stupid.
Re:Curl is C (2017)
by dale_glass on Thursday February 18, 2021 @09:51AM
(#26180413)
Right, and that's one of the things that makes C a bad language for security sensitive issues -- not just that it'll let you use memory after free(), but also that compilers have a liking for doing creative interpretations.
Re:Curl is C (2017)
by bobbylarrybobby on Thursday February 18, 2021 @02:19PM
(#26184238)
What's wrong with a compiler error? "You are calling a function (Do) that is only defined by calling a function (NeverCalled) that we can't guarantee you ever call"