Live data from Hacker News

GOTOphobia considered harmful in C

blog.joren.ga

11–20 of 319 posts

Re: GOTOphobia considered harmful in C

#11
Manual goto cleanup is such a busywork, adding nothing of value, only places for potential leaks and UAFs.

I know for C it’s unthinkable to standardize such a luxury like defer or destructors, so we’re going to relive arguments from 1968 for as long as C is used.

Re: GOTOphobia considered harmful in C

#12
post #10

> Bad code is the product of bad programmers This person is living in a pretend bubble that isn't grounded in the reality of large projects, multiple team members, deadlines, changing requirements, etc. No programmer is perfect. And when your tool can cut your arm off, you should be careful or route around the dangerous bits when possible.

It's right up there with being told you shouldn't need any access to production if you programmed it correctly the first time.

Re: GOTOphobia considered harmful in C

#13
post #9

There have been a bunch of major security vulnerabilities due to mistakes involving GOTO in C, being used as suggested by the article. Here’s a memorable one: https://www.imperialviolet.org/2014/02/22/applebug.html

I wondered then if this couldn't have been a merge error.

Re: GOTOphobia considered harmful in C

#14
post #9

There have been a bunch of major security vulnerabilities due to mistakes involving GOTO in C, being used as suggested by the article. Here’s a memorable one: https://www.imperialviolet.org/2014/02/22/applebug.html

that one in particular was not really caused by goto, but rather by braceless if statements, it'd be a vulnerability all the same if the line was a "fail" function that was called instead of a goto.

Re: GOTOphobia considered harmful in C

#15
post #9

There have been a bunch of major security vulnerabilities due to mistakes involving GOTO in C, being used as suggested by the article. Here’s a memorable one: https://www.imperialviolet.org/2014/02/22/applebug.html

I’d say this was more due to the case not being properly enclosed in brackets.

Many people seem to really dislike using the brackets but this is what that gets you.

Re: GOTOphobia considered harmful in C

#16
post #9

There have been a bunch of major security vulnerabilities due to mistakes involving GOTO in C, being used as suggested by the article. Here’s a memorable one: https://www.imperialviolet.org/2014/02/22/applebug.html

This seems to be more an issue with if statements without curly braces.

Re: GOTOphobia considered harmful in C

#18
post #10

> Bad code is the product of bad programmers This person is living in a pretend bubble that isn't grounded in the reality of large projects, multiple team members, deadlines, changing requirements, etc. No programmer is perfect. And when your tool can cut your arm off, you should be careful or route around the dangerous bits when possible.

Yeah, I don't know how people say stuff like this without looking over their shoulder or knocking on wood.

Re: GOTOphobia considered harmful in C

#19
In RAII languages[0], you obviously don't need unrestricted gotos.

However, I always find myself missing it when writing nested loops. Labeled break and continue[1] ought to be considered standard structure programming primitives. These are restricted gotos and allowing them to break or continue a parent loop doesn't unrestrict them much. But it does significantly improve the expressive power of your looping constructs.

[0] C++, Rust, Go, or anything else with automatic memory management and destructors

[1] I've also heard of numbered break/continue. Personally I think this isn't good enough: what if I need to move loops around? That will change the meaning of a `break 2`. With a `break OUTER` the compiler will yell at me if I remove the outer loop without changing all the code that breaks out of it to target a different one.

Re: GOTOphobia considered harmful in C

#20
If you’ve ever actually written more than toy C, you’ll know gotos are essential for resource cleanup and error handling. Even the famous goto fail was not a goto error, it was a block error (named because a cleanup statement `goto fail;` always executed because of a brace-less if). goto can be abused like any other language construct, but it’s uniquely useful and makes code more simple and easy to reason about when used correctly. You just shouldn’t be using C any more unless under duress.
Post reply on HN