On Thu, Sep 22, 2016 at 11:04 AM, <u...@netbeisser.de> wrote: > > Hi, > > how does noreturn attribute work? Trying to get this [0] to work, I > error on noreturn. > > /* Note how buffer overruns are undefined behavior and the compilers tend to > optimize these checks away if you wrote them yourself, this only works > robustly because the compiler did it itself. */ > extern uintptr_t __stack_chk_guard; > noreturn void __stack_chk_fail(void); > void foo(const char* str) > { > uintptr_t canary = __stack_chk_guard; > char buffer[16]; > strcpy(buffer, str); > if ( (canary = canary ^ __stack_chk_guard) != 0 ) > __stack_chk_fail(); > } > > > > main.c:7:10: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before > ‘void’ > noreturn void __stack_chk_fail(void); > > tried > void __stack_chk_fail(void) __attribute ((noreturn)); > instead. > > > [0] http://wiki.osdev.org/Stack_Smashing_Protector >
`noreturn` is usually used in exception code, kernel code, libc [0], or other systems code. It signifies to the compiler, whether there is a return type or void, that it is possible that the callee function will not return to the caller function. Often you will see a `while( true );` or `for(;;);` at the end of the function to make it more obvious for the next developer. Adding a `noreturn` will keep the compiler from complaining/ warning/ erroring when it encounters this function. For your case, you may need to add that infinite loop at the end of the function. [0] https://git.musl-libc.org/cgit/musl/tree/src/exit/_Exit.c?id=e738b8cbe64b6dd3ed9f47b6d4cd7eb2c422b38d