Nicolas George: > Andreas Rheinhardt (12020-06-20): >> Unfortunately all pointer arithmetic involving NULL is undefined >> behaviour in C, even NULL + 0. > > I am pretty sure the UB is not there, and that NULL+0 is valid. Do you > have the reference? > C99, 6.5.6 (the part about additive operators) says that pointer arithmetic is only defined in two cases: First, if it involves a pointer to an array (an object that is not an element of an array is treated as an array with exactly one element for the purposes of pointer arithmetic) and an offset so small so that it contains an element offset places away from the pointer. And secondly, if the pointer points to the last element of an array and the offset is 1. (For C90, this stuff is contained in 6.3.6.)
A nullpointer does not point to any object, hence it is undefined. Notice that UBSan in Clang 10 started warning about this (see ticket 8592 or see https://github.com/llvm/llvm-project/commit/536b0ee40ab97f2878dc124a321cf9108ee3d233). > Anyway, even if it is valid, it is not good style, I agree on that. While it would be very bad style to write NULL + 0 or something like that when it is known at compile-time that this is the only case that will happen, I don't consider an expression that might lead to NULL + 0 to be bad style. I actually think that this is one of the insanities of the C standard (C++ btw explicitly allows nullptr + 0). - Andreas _______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org https://ffmpeg.org/mailman/listinfo/ffmpeg-devel To unsubscribe, visit link above, or email ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".