Menu

#193 New -Wrestrict warning with GCC9

autogen
closed
nobody
None
1
2020-09-14
2019-02-19
No

Using latest GCC, one can see:

$ /bin/sh ../libtool  --tag=CC   --mode=compile gcc -DHAVE_CONFIG_H -I. -I..  -I.. -I../autoopts  -DPKGDATADIR='"/usr/local/share/autogen"' -g -O2 -Wno-format-contains-nul -fno-strict-aliasing -Wall -Werror -Wcast-align -Wmissing-prototypes -Wpointer-arith -Wshadow -Wstrict-prototypes -Wwrite-strings -Wstrict-aliasing=3 -Wextra -Wno-cast-qual -g -O2 -Wno-format-contains-nul -fno-strict-aliasing -c -o libopts_la-libopts.lo `test -f 'libopts.c' || echo './'`libopts.c
libtool: compile:  gcc -DHAVE_CONFIG_H -I. -I.. -I.. -I../autoopts -DPKGDATADIR=\"/usr/local/share/autogen\" -g -O2 -Wno-format-contains-nul -fno-strict-aliasing -Wall -Werror -Wcast-align -Wmissing-prototypes -Wpointer-arith -Wshadow -Wstrict-prototypes -Wwrite-strings -Wstrict-aliasing=3 -Wextra -Wno-cast-qual -g -O2 -Wno-format-contains-nul -fno-strict-aliasing -c libopts.c  -fPIC -DPIC -o .libs/libopts_la-libopts.o
In file included from autoopts.c:42,
                 from libopts.c:22:
In function ‘canonicalize_pathname’,
    inlined from ‘option_pathfind.constprop’ at ../compat/pathfind.c:88:32:
../compat/pathfind.c:214:17: error: ‘strcpy’ accessing 1 byte at offsets [0, 9223372036854775807] and [0, 9223372036854775807] may overlap 1 byte at offset 0 [-Werror=restrict]
  214 |                 strcpy( result + start + 1, result + i + 2 );
      |                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

It's explained and analyzed here:
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=88973

Related

Bugs: #193

Discussion

  • Bruce Korb

    Bruce Korb - 2019-02-19

    I didn't write this code. The person who did is no longer able to help (employer restriction). I haven't been writing code for 5 years, so a patch would be welcome. :)

    Otherwise, I'll take a gander when I get back from an out-of-country wedding this April.

     
  • Martin Liška

    Martin Liška - 2019-02-20

    I can come up with a patch candidate.

     
  • Martin Liška

    Martin Liška - 2019-02-20

    Patch candiate:

    diff --git a/compat/pathfind.c b/compat/pathfind.c
    index 5c477ca..6a4eeb5 100644
    --- a/compat/pathfind.c
    +++ b/compat/pathfind.c
    @@ -136,6 +136,18 @@ make_absolute( char const * string, char const * dot_path )
         return result;
     }
    
    +/*
    
    + * Proccess strcpy for overlapping memory locations.
    + */
    +static char*
    +strcpy_overlapping ( char *d, const char *s)
    +{
    +  unsigned n = strlen ( s );
    +  memmove ( d, s, n + 1 );
    +  return d;
    +}
    +
    +
     /*
      * Canonicalize PATH, and return a  new path.  The new path differs from
      * PATH in that:
    @@ -182,7 +194,7 @@ canonicalize_pathname( char *path )
             if ((start + 1) != i && (start != 0 || i != 2))
     #endif /* apollo */
             {
    -            strcpy( result + start + 1, result + i );
    +            strcpy_overlapping( result + start + 1, result + i );
                 i = start + 1;
             }
    
    @@ -201,7 +213,7 @@ canonicalize_pathname( char *path )
             if (result[i] == '.') {
                 /* Handle `./'. */
                 if (result[i + 1] == '/') {
    
    -                strcpy( result + i, result + i + 1 );
    +                strcpy_overlapping( result + i, result + i + 1 );
                     i = (start < 0) ? 0 : start;
                     continue;
                 }
    @@ -211,7 +223,7 @@ canonicalize_pathname( char *path )
                     (result[i + 2] == '/' || !result[i + 2])) {
                     while (--start > -1 && result[start] != '/')
                         ;
    -                strcpy( result + start + 1, result + i + 2 );
    +                strcpy_overlapping( result + start + 1, result + i + 2 );
                     i = (start < 0) ? 0 : start;
                     continue;
                 }
    
     
  • Martin Liška

    Martin Liška - 2019-02-26

    Any opinion about the patch? Thanks.

     
    • Bruce Korb

      Bruce Korb - 2019-02-26

      Looks good to me, but strcpy should have been a void procedure from the get-go.
      Since we're fixing a warning triggered by a "we must warn about every
      conceivable
      issue", the return value from strcpy/strcpy_overlapping is ignored. May as well
      make this warning-go-away fix be scrupulous, too:

      static void
      strcpy_overlapping(char d, const char s)
      {
      memmove(d, s, strlen(s) + 1);
      }

      I'll be back from a wedding the end of March.

      On Tue, Feb 26, 2019 at 2:19 AM "Martin Liška"
      marxin@users.sourceforge.net wrote:

      Any opinion about the patch? Thanks.


      [bugs:#193] New -Wrestrict warning with GCC9

      Status: open
      Group: autogen
      Created: Tue Feb 19, 2019 09:34 AM UTC by Martin Liška
      Last Updated: Wed Feb 20, 2019 11:52 AM UTC
      Owner: nobody

      Using latest GCC, one can see:

      $ /bin/sh ../libtool --tag=CC --mode=compile gcc -DHAVE_CONFIG_H -I. -I.. -I.. -I../autoopts -DPKGDATADIR='"/usr/local/share/autogen"' -g -O2 -Wno-format-contains-nul -fno-strict-aliasing -Wall -Werror -Wcast-align -Wmissing-prototypes -Wpointer-arith -Wshadow -Wstrict-prototypes -Wwrite-strings -Wstrict-aliasing=3 -Wextra -Wno-cast-qual -g -O2 -Wno-format-contains-nul -fno-strict-aliasing -c -o libopts_la-libopts.lo test -f 'libopts.c' || echo './'libopts.c
      libtool: compile: gcc -DHAVE_CONFIG_H -I. -I.. -I.. -I../autoopts -DPKGDATADIR=\"/usr/local/share/autogen\" -g -O2 -Wno-format-contains-nul -fno-strict-aliasing -Wall -Werror -Wcast-align -Wmissing-prototypes -Wpointer-arith -Wshadow -Wstrict-prototypes -Wwrite-strings -Wstrict-aliasing=3 -Wextra -Wno-cast-qual -g -O2 -Wno-format-contains-nul -fno-strict-aliasing -c libopts.c -fPIC -DPIC -o .libs/libopts_la-libopts.o
      In file included from autoopts.c:42,
      from libopts.c:22:
      In function ‘canonicalize_pathname’,
      inlined from ‘option_pathfind.constprop’ at ../compat/pathfind.c:88:32:
      ../compat/pathfind.c:214:17: error: ‘strcpy’ accessing 1 byte at offsets [0, 9223372036854775807] and [0, 9223372036854775807] may overlap 1 byte at offset 0 [-Werror=restrict]
      214 | strcpy( result + start + 1, result + i + 2 );
      | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

      It's explained and analyzed here:
      https://gcc.gnu.org/bugzilla/show_bug.cgi?id=88973


      Sent from sourceforge.net because you indicated interest in https://sourceforge.net/p/autogen/bugs/193/

      To unsubscribe from further messages, please visit https://sourceforge.net/auth/subscriptions/

       

      Related

      Bugs: #193

  • Bruce Korb

    Bruce Korb - 2020-09-14
    • status: open --> closed
     
  • Bruce Korb

    Bruce Korb - 2020-09-14

    strcpy-s were replaced with memmove directly.
    fixed.

     

Log in to post a comment.

MongoDB Logo MongoDB