Re: gcc warns for memmove (0, b, 0)

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 16/11/2016 12:45, Prathamesh Kulkarni wrote:

> For the following test-case:
> 
> void f(char *b)
> {
>   __builtin_memmove (0, b, 0);
> }
> 
> gcc emits the warning:
> t2.c: In function ‘f’:
> t2.c:3:3: warning: null argument where non-null required (argument 1)
> [-Wnonnull]
>    __builtin_memmove (0, b, 0);
>    ^~~~~~~~~~~~~~~~~
> 
> I was wondering if the warning is correct in this case, since we are
> passing 3rd argument as 0,
> and IIUC, no copying should take place from source to dest and hence
> shouldn't matter
> if dest is NULL ?
> 
> n1570 7.24.2.2 says:
> "The memmove function copies n characters from the object pointed to
> by s2 into the
> object pointed to by s1. Copying takes place as if the n characters
> from the object
> pointed to by s2 are first copied into a temporary array of n
> characters that does not
> overlap the objects pointed to by s1 and s2, and then the n characters from the
> temporary array are copied into the object pointed to by s1."
> 
> I am not sure though how to interpret this in context of above example.

I'm using the n1256 (C99) draft, but your document should have it too;
you missed this footnote:

> Where an argument declared as size_t n specifies the length of the
> array for a function, n can have the value zero on a call to that
> function. Unless explicitly stated otherwise in the description of a
> particular function in this sub-clause, pointer arguments on such a
> call shall still have valid values, as described in 7.1.4.
       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Note that a very similar question came up recently on comp.lang.c

"UB of memcpy(NULL, NULL, 0)"
https://groups.google.com/forum/#!topic/comp.lang.c/3dDP5kbWfbE
(A real news server is definitely better than this interface though)

https://www.imperialviolet.org/2016/06/26/nonnull.html

Regards.




[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux