From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 17346 invoked by alias); 3 Jun 2014 13:11:38 -0000 Mailing-List: contact gcc-bugs-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-bugs-owner@gcc.gnu.org Received: (qmail 17306 invoked by uid 48); 3 Jun 2014 13:11:32 -0000 From: "jason at gcc dot gnu.org" To: gcc-bugs@gcc.gnu.org Subject: [Bug middle-end/61294] [4.9 Regression] erroneous memset used with constant zero length parameter warning Date: Tue, 03 Jun 2014 13:11:00 -0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: middle-end X-Bugzilla-Version: 4.9.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: jason at gcc dot gnu.org X-Bugzilla-Status: NEW X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: 4.9.1 X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: bug_status cc component Message-ID: In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 7bit X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 X-SW-Source: 2014-06/txt/msg00172.txt.bz2 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=61294 Jason Merrill changed: What |Removed |Added ---------------------------------------------------------------------------- Status|WAITING |NEW CC| |jason at gcc dot gnu.org Component|c++ |middle-end --- Comment #4 from Jason Merrill --- It's clearly a false positive; the warning is intended to catch calls where the user wrote a 0 directly in the argument list for memset (which suggests accidentally transposed arguments), not cases where some execution path might result in a 0 argument (which works fine). The only question is what there is to fix either in GCC or GLIBC to avoid this false positive. A simple way to work around this is to guard the memset with if (npoints > 0). The false positive seems to come up fairly often: https://sourceware.org/ml/binutils/2012-02/msg00073.html https://bugzilla.redhat.com/show_bug.cgi?id=452219 https://www.nsnam.org/bugzilla/show_bug.cgi?id=1165 Changing component to middle-end. Reduced C testcase: typedef __SIZE_TYPE__ size_t; extern void *malloc (size_t __size) __attribute__ ((__malloc__)) __attribute__ ((__warn_unused_result__)); extern void *memset (void *__s, int __c, size_t __n) __attribute__ ((__nonnull__ (1))); extern void __warn_memset_zero_len (void) __attribute__((__warning__ ("memset used with constant zero length parameter; this could be due to transposed parameters"))); extern __inline __attribute__((__always_inline__)) __attribute__((__artificial__)) void * memset (void *__dest, int __ch, size_t __len) { if (__builtin_constant_p (__len) && __len == 0 && (!__builtin_constant_p (__ch) || __ch != 0)) { __warn_memset_zero_len (); return __dest; } return __builtin___memset_chk (__dest, __ch, __len, __builtin_object_size (__dest, 0)); } int i; inline int f() { if (i) return i; else return 0; } void g(unsigned char val) { int len = f(); void *p = malloc (len); memset (p, val, len); }