Re:Linux Local Privilege Escalation via SUID /proc/pid/mem W...
by zx2c4 on Monday January 23, 2012 @12:18AM
(#3499198)
Goto is common for error out code in C. The code that uses that mentioned in the blog code is from the linux kernel, not any exploit code.
But please, for the love of god, can we not make this discussion thread an extended conversation on the merits or horrors of goto? Pretty please?
Re:Linux Local Privilege Escalation via SUID /proc/pid/mem W...
by yew on Monday January 23, 2012 @12:56AM
(#3499283)
Goto is actually relatively common in C code. As in this case, it's usually used for error handling in much the same manner as exceptions might be. The document "/usr/src/linux/Documentation/CodingStyle" actually recommends such usage.
Re:Linux Local Privilege Escalation via SUID /proc/pid/mem W...
by chalst on Monday January 23, 2012 @05:12AM
(#3499801)
The reason why Linus Torvalds and others prefer to handle errors using goto is that it avoids the main branch of the code receiving an additional indentation, as it would if it were wrapped in a conditional. Not to do so would make the code less readable.
Note that these gotos are always local jumps (that is, they are in the same code block), at the same indentation level, and they are forward jumps (so the control flow is much the same as for a conditional). They are easy to understand and audit, and not similar to the goto examples that Dijkstra criticised for making code hard to reason about.
Re:Linux Local Privilege Escalation via SUID /proc/pid/mem W...
by arnnr on Monday January 23, 2012 @03:30PM
(#3501997)
This.
Also python's execeptions can be treated as limited goto's:
http://docs.python.org/faq/design.html#why-is-there-no-goto
Re:Linux Local Privilege Escalation via SUID /proc/pid/mem W...
by zokier on Monday January 23, 2012 @02:33AM
(#3499474)
I addition what others have said, I'd emphasise that goto for error handling is fine in C, but in C++ it is usually unnecessary because C++ has exceptions and RAII.
Re:Linux Local Privilege Escalation via SUID /proc/pid/mem W...
by fhars on Monday January 23, 2012 @02:20AM
(#3499445)
Read the classics http://scholar.google.com/scholar?cluster=171471433276813964...
Re:Linux Local Privilege Escalation via SUID /proc/pid/mem W...
by VMG on Monday January 23, 2012 @04:18AM
(#3499667)
meta: I don't see why it is necessary to downvote newbie questions on HN
Re:Linux Local Privilege Escalation via SUID /proc/pid/mem W...
by exDM69 on Monday January 23, 2012 @05:04AM
(#3499777)
Gotos are common in C for error handling because the other alternative of doing an if/then/else with all the error handling is so verbose. It's used to work around the lack of exceptions in C.
You still should not use gotos for regular control flow. And jumping backwards with goto is bad practice in most cases.
Appearing in the Linux kernel makes me reconsider my avoidance of it[2]. A quick google throws up a discussion with Linus in 2003[3] which can be summarised as: don't blame the tool, blame the programmer and as a tool goto has a place.
[1] XKCD comic on the subject http://xkcd.com/292/
[2] On my infrequent visits to C++ land, I generally live in Python land where goto doesn't exist.
[3] http://kerneltrap.org/node/553/2131