From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 116689 invoked by alias); 13 Dec 2019 00:20:55 -0000 Mailing-List: contact gcc-help-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-help-owner@gcc.gnu.org Received: (qmail 116677 invoked by uid 89); 13 Dec 2019 00:20:54 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-7.4 required=5.0 tests=AWL,BAYES_00,FREEMAIL_FROM,RCVD_IN_DNSWL_NONE,SPF_PASS autolearn=ham version=3.3.1 spammy= X-HELO: mail-qk1-f182.google.com Received: from mail-qk1-f182.google.com (HELO mail-qk1-f182.google.com) (209.85.222.182) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Fri, 13 Dec 2019 00:20:53 +0000 Received: by mail-qk1-f182.google.com with SMTP id l124so348270qkf.8 for ; Thu, 12 Dec 2019 16:20:53 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-language:content-transfer-encoding; bh=MLmGCm2AoahonXgSVgEVKWpG7xp/mlgG6SjCShDGVc4=; b=bbfODd6/ZqHLl1lEQylZ64jYkPoi8W4hqezyzruEIBb0MWMA4seZvqFWsSXfZMOcM3 UgR8wDDxnLwtESjTTL0iby2EIx7xPyAWdx6mpePodI2SuqJon+zKhjlMHodIAnySzXoP RLimS/ZSZw6oSxnY4RjuuhxZQU5sV7NUK4V1CiRq97IBvXAvMBazbnhJNZmWBvoa2xct 8CmnRV46Ey0GL9yeAIDXpeCr5qB3FkwRRa8hhkwEluevr+vrOyHZxdp/IfKKm6VAjBg7 krCcZrJGJjXF3F8XraKurm57XAgNiExS6o7+8BKB+UyIzCDruPjEEE5o92lUnaB/5dNH FUrg== Return-Path: Received: from [192.168.0.41] (75-166-99-91.hlrn.qwest.net. [75.166.99.91]) by smtp.gmail.com with ESMTPSA id i8sm2740527qtr.24.2019.12.12.16.20.50 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 12 Dec 2019 16:20:50 -0800 (PST) Subject: Re: gcc-9.2.0 and spurious warnings To: Josef Wolf , gcc-help@gcc.gnu.org References: <20191212081043.GK2074@raven.inka.de> From: Martin Sebor Message-ID: <5e1c3ed5-db98-0079-8723-401a1fef42e0@gmail.com> Date: Fri, 13 Dec 2019 00:20:00 -0000 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.6.1 MIME-Version: 1.0 In-Reply-To: <20191212081043.GK2074@raven.inka.de> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-IsSubscribed: yes X-SW-Source: 2019-12/txt/msg00045.txt.bz2 On 12/12/19 1:10 AM, Josef Wolf wrote: > Hello, > > I upgraded from gcc-8.2.0 to gcc-9.2.0. > > With gcc-9.2.0, I am getting new warnings for old code, which I have not seen > with gcc-8.2.0 and earlier gcc versions. > > What's more is, that those warnings disappear when OTHER (totally unrelated) > parts of the code is removed. > > > Here is one example (some helper functions for parsing). With the code > attached below, I get this warning: > > $ LANG= PATH=$PATH:/usr/local/crossgcc/bin m68k-unknown-elf-gcc -ansi -pedantic -Wall -Wcast-align -Wstrict-prototypes -Wmissing-prototypes -std=c89 -Wnull-dereference -g -O2 -fno-toplevel-reorder -mcpu32 -c -o t.o t.c > t.c: In function 'get_word': > t.c:53:5: warning: 'strncpy' destination unchanged after copying no bytes > [-Wstringop-truncation] > 53 | strncpy (*r, p, q-p); /* copy */ > | ^~~~~~~~~~~~~~~~~~~~ > > I don't understand this warning at all. The freshly allocated memory is big > enough to hold the copied word including the trailing NUL character. And the > NUL character is appended just behind the copied word. > > What am I supposed to do to get rid of the warning? > > When I remove the next_word() function, which is TOTALLY UNRELATED to the code > in question, the warning disappears! Jeff already explained what the warning means. I don't see it with fresh trunk, either natively or with an m68k-unknown-elf cross, so I can't really say why GCC 9 thinks the two pointers are equal. You can tell by looking at the optimization dumps (use -ftree-dump-all and grep the output for strncpy with a zero argument). What I can say is that strncpy isn't really meant to be used this way: to copy exactly as many bytes as the third argument says. That's what memcpy is for. The warning was added because strncpy is frequently misused and a common source of bugs. The warning tries to avoid triggering for the safe uses when it can detect they are safe, but it's imperfect and prone to false alarms. To avoid it I would suggest using memcpy instead. It will likely also be faster. Alternatively, you can try calling the function only when the difference is non-zero. Martin > > Here is the code: > > #include > #include > #include > > char *skip_spaces (char *p); > char *skip_word (char *p); > char *next_word (char *p); > char *get_word (char *p, char **r); > void halt_system (int restart, char *fmt, ...) __attribute__((noreturn)); > > /* skip spaces and tabs */ > char *skip_spaces (char *p) > { > if (!p) return NULL; > > while (((*p==' ') || (*p=='\t'))) > p++; > > return (p); > } > > /* skip word (separated by spaces/tabs) */ > char *skip_word (char *p) > { > while (p && *p && (*p!=' ') && (*p!='\t')) > p++; > > return (p); > } > > /* serch next word (separated by spaces/tabs) */ > char *next_word (char *p) > { > return (skip_spaces (skip_word (p))); > } > > /* This function will extract a word and store it in freshly allocated memory */ > char *get_word (char *p, char **r) > { > char *q; > > if (!p) return NULL; > > p = skip_spaces (p); /* look for start of word */ > q = skip_word (p); /* look for end of word */ > > if (!(*r=malloc (q-p+1))) /* allocate memory */ > halt_system (1, "Out of memory in get_word()"); > > /* !!!!!! The warning at the next line is: > 'strncpy' destination unchanged after copying no bytes [-Wstringop-truncation] > This warning disappears when the next_word() function is removed. > */ > strncpy (*r, p, q-p); /* copy */ > (*r)[q-p] = 0; /* mark the end */ > > return (skip_spaces (q)); /* return pointer to next word */ > } >