Crashing threads with *(int*)NULL = 1; problematic?

Deadly 提交于 2019-12-06 03:53:42

问题


I found this in a multi-threaded c application. The authors commented that it's used to make a thread crash in a custom assert function. GCC is fine with it, but clang issues the following warning:

note: consider using __builtin_trap() or qualifying pointer with 'volatile'

and also issues one of those, for each usage of the assert function:

warning: indirection of non-volatile null pointer will be deleted, not trap

What is going on here? Is __builtin_trap specific to clang? Should I use it?


回答1:


Writing to NULL address is not guaranteed to crash your program reliably, so GCC introduced __builtin_trap for that.

It looks like clang decided to go further, and eliminate such writes altogether, almost forcing you into using __builtin_trap. Their other option of casting NULL to volatile pointer does not look attractive compared to __builtin_trap, because it's "merely" an undefined behavior.




回答2:


The statement provoques undefined behavior. In particular the compiler is not obliged to try to store something at address 0 and may optimize this out. This is what the compilers are telling you.

Use exit() or abort() or some of the derivatives to terminate the whole process execution. This is portable. (C11 has exit, _Exit, quick_exit and abort)



来源:https://stackoverflow.com/questions/10153246/crashing-threads-with-intnull-1-problematic

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!