bug-gnulib@gnu.org mirror (unofficial)
 help / color / mirror / Atom feed
From: "Tim Rühsen" <tim.ruehsen@gmx.de>
To: Bruno Haible <bruno@clisp.org>, Paul Eggert <eggert@cs.ucla.edu>
Cc: Mats Erik Andersson <mats.andersson@gisladisker.se>, bug-gnulib@gnu.org
Subject: Re: restrict
Date: Mon, 17 Feb 2020 10:51:34 +0100	[thread overview]
Message-ID: <82caf012-4998-2d98-52b6-8eac1ca4af72@gmx.de> (raw)
In-Reply-To: <2029541.RzEzKTdQOE@omega>


[-- Attachment #1.1: Type: text/plain, Size: 1536 bytes --]

On 2/17/20 4:53 AM, Bruno Haible wrote:
> Paul Eggert wrote:
>> if GCC generated warnings for that sort of thing, the warnings would be false
>> alarms.
> 
> Yes, and this in turn means that the ability to produce useful warnings via
> 'restrict' is limited. In this example:
> ===================================================================
> #include <string.h>
> extern void memmcpy (void *restrict, const void *restrict, size_t);
> 
> void shuffle (char array[10])
> {
>   memmcpy (array + 2, array, 8);
>   memcpy (array + 2, array, 8);
> }
> ===================================================================
> gcc gives no warning about 'memmcpy' - because it does not know
> how many elements the function will access. gcc does give a warning
> about 'memcpy' - apparently due to custom logic in the compiler.

The following gives you a warning, with -O2 / -O3 and -Wall:

===================================================================
#include <string.h>
void memmcpy (void *restrict d, void *restrict s, size_t n)
{
  memcpy(d, s, n);
}

void shuffle ()
{
  char array[] = "abcdefg", *array2 = array + 2;

  memmcpy (array, array2 - 2, 8);
}
===================================================================

$ gcc-8 -O2 -Wall x.c
In function ‘memmcpy’,
    inlined from ‘shuffle’ at x.c:11:3:
x.c:4:3: warning: ‘memcpy’ accessing 8 bytes at offsets 0 and 0 overlaps
8 bytes at offset 0 [-Wrestrict]
   memcpy(d, s, n);
   ^~~~~~~~~~~~~~~

Regards, Tim


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

  reply	other threads:[~2020-02-17  9:52 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-02-09 14:44 Possible testing case of snprintf Mats Erik Andersson
2020-02-09 15:24 ` Tim Rühsen
2020-02-10  3:02   ` restrict Bruno Haible
2020-02-10  8:39     ` restrict Jeffrey Walton
2020-02-10 10:11     ` restrict Tim Rühsen
2020-02-16 21:44     ` restrict Paul Eggert
2020-02-16 23:49       ` restrict Bruno Haible
2020-02-17  0:14         ` restrict Bruno Haible
2020-02-17  2:46           ` restrict Paul Eggert
2020-02-17  3:53             ` restrict Bruno Haible
2020-02-17  9:51               ` Tim Rühsen [this message]
2020-02-17 21:03                 ` restrict Bruno Haible
2020-02-17 18:03         ` restrict Paul Eggert
2020-02-17 20:55           ` restrict Bruno Haible
2020-02-17 22:28             ` restrict Paul Eggert
2020-02-17 21:19     ` restrict - summary Bruno Haible
2020-02-22 22:51       ` Bruno Haible
2020-02-24 19:17         ` Eric Blake
2020-02-23 12:39       ` Bruno Haible
2020-02-23 13:30       ` Bruno Haible

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://lists.gnu.org/mailman/listinfo/bug-gnulib

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=82caf012-4998-2d98-52b6-8eac1ca4af72@gmx.de \
    --to=tim.ruehsen@gmx.de \
    --cc=bruno@clisp.org \
    --cc=bug-gnulib@gnu.org \
    --cc=eggert@cs.ucla.edu \
    --cc=mats.andersson@gisladisker.se \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).