ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
In programming courses , we always to be taught to never to use goto statement in code.In many article about programming also indicate so.
I'm new to kernel module programming , but i see many goto statements in kernel module samples.
Why there are so much goto statements in kernel modules?
I'm calling so as to congratulate Sergei Steshenko on an exceptionally useful post which also took this thread out of the "zero replies threads" list. Keep up the good work!
I'm calling so as to congratulate Sergei Steshenko on an exceptionally useful post which also took this thread out of the "zero replies threads" list. Keep up the good work!
If the OP was taught not use goto's without being explained why, when and how, he was wrongly taught.
The main point is that "goto" is never necessary, and---in some standards---would not be allowed:
Quote:
The 1960s and 1970s saw computer scientists move away from GOTO statements in favor of the "structured programming" paradigm. Some programming style coding standards prohibit use of GOTO statements, particularly in view of the aforementioned structured program theorem. While it's a definite proof that GOTOs can be eliminated completely from the code, it doesn't prove in any way that the resulting programs would be easier to write and maintain than those using GOTO statements or that they would execute efficiently in every context.
My rule would be that the programmer can follow any standard they like, as long as the code meets the requirements of the customer.
Note that there are requirements that kernel developers are required to follow.
if we want to handle all our errors in one place, we'll need to add in a variable to track the return value of our function so we can return it:
Code:
int big_function()
{
int ret_val = [success];
/* do some work */
if([error])
{
ret_val = [error];
goto end;
}
/* do some more work */
if([error])
{
ret_val = [error];
goto end;
}
/* do some more work */
if([error])
{
ret_val = [error];
goto end;
}
/* do some more work */
if([error])
{
ret_val = [error];
goto end;
}
end:
/* clean up*/
return ret_val;
}
The benefit here is that your code following end has access to everything it will need to perform cleanup, and you've managed to reduce the number of change points considerably. Another benefit is that you've gone from having multiple exit points for your function to just one; there's no chance you'll accidentally return from the function without cleaning up.
Moreover, since goto is only being used to jump to a single point, it's not as though you're creating a mass of spaghetti code jumping back and forth in an attempt to simulate function calls. Rather, goto actually helps write more structured code.
There is one thing to be aware of: while your cleanup code should be able to free all of the memory you use, there may be times when you actually want to free that memory yourself and possibly reallocate it later. In these cases, if you do call free on a ptr and then have an if([error]) between that call to free and the subsequent call to malloc, you should definitely set the pointer to point to NULL! This will prevent your jumping to the cleanup code and then calling free on that pointer a second time, which can result in a security hole (the "double free" problem).
Goto should always be used sparingly, and as a last resort -- but there is a time and a place for it. The question should be not "do you have to use it" but "is it the best choice" to use it.
Yes, it's the second reason quoted above. But it specifically discusses kernel code. And one of the follow-ups has the block rewritten without goto so that you can compare them.
In simple English (I think I'm reiterating what's already been said) using goto for error handling, especially to get out from deep inner loop/scope, is quite good, clear and recommended.
In more general terms, goto to a label below the goto statement most likely is OK.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.