Having looked at a few different C systems codebases (notably the kernels of BSD and Linux), I notice a liberal use of goto
, even when it would be trivial to use a higher-level flow control construct like a loop or function call instead. Here's one example, from mm/mmap.c in Linux:
munmap_back:
if (find_vma_links(mm, addr, addr + len, &prev, &rb_link, &rb_parent)) {
if (do_munmap(mm, addr, len))
return -ENOMEM;
goto munmap_back;
}
munmap_back
is not referenced anywhere else, so this code is exactly equivalent to
while (find_vma_links(mm, addr, addr + len, &prev, &rb_link, &rb_parent)) {
if (do_munmap(mm, addr, len))
return -ENOMEM;
}
Since Linux and BSD were developed by very competent people, I assume there are some advantages or reasons for this style. What are they?