This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug c/81391] New: Use of parenthesis disables warning about incorrect size parameter
- From: "bugzilla at poradnik-webmastera dot com" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: Tue, 11 Jul 2017 09:29:21 +0000
- Subject: [Bug c/81391] New: Use of parenthesis disables warning about incorrect size parameter
- Auto-submitted: auto-generated
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81391
Bug ID: 81391
Summary: Use of parenthesis disables warning about incorrect
size parameter
Product: gcc
Version: 5.4.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c
Assignee: unassigned at gcc dot gnu.org
Reporter: bugzilla@poradnik-webmastera.com
Target Milestone: ---
When code below is compiled, gcc complains only about first call to strncpy.
2nd call has the same problem as the 1st one (sizeof pointer used as buffer
size), but there is no warning about it - parenthesis added around sizeof()
silenced this warning. It is recommended to use additional parenthesis around
params in macrofunctions, so some bugs there may went unnoticed.
Tested with gcc 4.8.5 (Linux) and gcc 5.4.0 (Cygwin).
#include <string.h>
int main()
{
char buf[10], *ptr = buf;
strncpy(ptr, "a", sizeof(ptr));
strncpy(ptr, "a", (sizeof(ptr)));
return 0;
}
$ gcc -o test test.c -Wall -O2
test.c: In function ‘main’:
test.c:6:29: warning: argument to ‘sizeof’ in ‘strncpy’ call is the same
expression as the destination; did you mean to provide an explicit length?
[-Wsizeof-pointer-memaccess]
strncpy(ptr, "a", sizeof(ptr));
^