7 ms·
Is it considered harmful, and if so, why?
by Simran-B 4y ago
Is it considered harmful, and if so, why?
- deleted 4y ago[deleted]
- pm215 4y agoMostly for the usual "don't reinvent a wheel the language standard library already provides" reasons, I think. For instance glibc's x86setjmp/sigsetjmp have been updated to support shadow stacks, but if you'd rolled your own you'd have to do that yourself: https://elixir.bootlin.com/glibc/glibc-2.35/source/sysdeps/i386/setjmp.S https://elixir.bootlin.com/glibc/glibc-2.35/source/sysdeps/i...
- j16sdiz 4y agoYes, but the author was trying to avoid using libc. > Yesterday I wrote that setjmp is handy and that it would be nice to have without linking the C standard library. Related article from the same author. https://nullprogram.com/blog/2023/02/11/ https://nullprogram.com/blog/2023/02/11/
- pm215 4y agoPersonally I think that's throwing the baby out with the bathwater for most use cases. You could rephrase my comment as "if you're already committed to reinventing half of libc's wheels, this one is not really any harder or more awkward than most, but if you're not aiming for that overall goal then reinventing just this one wheel is a bad plan" if you like.
- adrian_b 4y ago"setjmp" and "longjmp" are the mechanism for implementing exceptions in C, i.e. they are just another form of writing "catch" and "throw". The implementation of exceptions, i.e. of jumps over multiple levels of nested functions and blocks, is much simpler in C than in C++, because there are no destructors that must be called when unwinding the stack, so it is enough to restore the CPU registers to the values correct for the program point where the exception must be caught. This is needed because at the point where the exception is thrown it is not known whether any of the nested functions that must be skipped has modified any of the registers that a function is expected to preserve and where the original values of the registers have been saved. Any kind of exceptions can be easily misused, which is why it is recommended to be careful with the use of "setjmp" and "longjmp", but they are not more harmful than the use of exceptions in any other language. The only additional problem of C is that since there are no implicitly called destructors, like in C++ and similar languages, in C the programmer must do what the compiler would do in C++. This means that if the nested functions that are skipped by a "longjmp" have allocated heap memory, opened files or sockets etc., such resources must be freed in the exception handler marked by a "setjmp". Therefore the C programmer must keep track of the resources that might have been allocated in the nested functions, e.g. by recording the allocations in some global table.
- ur-whale 4y ago> "setjmp" and "longjmp" are the mechanism for implementing exceptions in C Correct, but reductive. They can be used for many other things.
- adrian_b 4y agoThe more general POV is that "setjmp" defines a continuation and "longjmp" invokes it. While the most frequent use of explicit continuations is for implementing exceptions, you are right that there are many other programming techniques based on explicit continuations (for instance coroutines).
- someweirdperson 4y ago> They can be used for many other things. The underlying primitive offers even more flexibility, but exceptions (in the sense of programming languages) can also be used for many other things than exceptions (in the sense of natural language, to handle exceptional cases).
- bogomipz 4y ago>"... but exceptions (in the sense of programming languages) can also be used for many other things than exceptions ..." Can you elaborate? What are the other ways exceptions are used when not used to handle exceptional situations?
- randomNumber7 4y agoIt's like goto, but you can jump over function boundaries. Checking out the other comments here its not considered harmful...
- kazinator 4y agolongjmp is like return, which can jump out of a function, to the immediate caller. However, longjmp can return directly to a grandparent, great-grandparent, ... this happens without any unwind support; you have to invent your own conventions for that and implement them. Only carefully-written code which knows about the conventions of your particular longjmp wrapper library will get the unwinding.