Whamcloud - gitweb
LU-15220 tests: avoid gcc-11 -Werror=stringop-overread warning 77/45777/7
authorJian Yu <yujian@whamcloud.com>
Thu, 20 Jan 2022 19:06:42 +0000 (11:06 -0800)
committerOleg Drokin <green@whamcloud.com>
Wed, 26 Jan 2022 05:17:08 +0000 (05:17 +0000)
GCC 11 warns about string and memory operations on fixed address:

In function 'memcpy', inlined from 'obd_uuid2str' at
lustre/include/uapi/linux/lustre/lustre_user.h:1222:3,
include/linux/fortify-string.h:20:33: error: '__builtin_memcpy'
reading 39 bytes from a region of size 0 [-Werror=stringop-overread]
   20 | #define __underlying_memcpy     __builtin_memcpy
      |                                 ^
include/linux/fortify-string.h:191:16: note:
in expansion of macro '__underlying_memcpy'
  191 |         return __underlying_memcpy(p, q, size);
      |                ^~~~~~~~~~~~~~~~~~~

The patch avoids the above warning by not using a fixed address.

badarea_io.c:47:14: error: 'write' reading 5 bytes from a region
of size 0 [-Werror=stringop-overread]
   47 |         rc = write(fd, (void *)0x4096000, 5);
      |              ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The patch avoids the above warning by making the pointer volatile
as suggested in:
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99578#c16

Change-Id: I90b936835c6236a0f47e744013e3e480442f682c
Signed-off-by: Jian Yu <yujian@whamcloud.com>
Reviewed-on: https://review.whamcloud.com/45777
Tested-by: jenkins <devops@whamcloud.com>
Reviewed-by: Arshad Hussain <arshad.hussain@aeoncomputing.com>
Tested-by: Maloo <maloo@whamcloud.com>
Reviewed-by: Patrick Farrell <pfarrell@whamcloud.com>
Reviewed-by: James Simmons <jsimmons@infradead.org>
Reviewed-by: Oleg Drokin <green@whamcloud.com>
lustre/include/uapi/linux/lustre/lustre_user.h
lustre/tests/badarea_io.c

index 8f746ec..e9be1a6 100644 (file)
@@ -1218,9 +1218,11 @@ static inline char *obd_uuid2str(const struct obd_uuid *uuid)
        if (uuid->uuid[sizeof(*uuid) - 1] != '\0') {
                /* Obviously not safe, but for printfs, no real harm done...
                   we're always null-terminated, even in a race. */
-               static char temp[sizeof(*uuid)];
-               memcpy(temp, uuid->uuid, sizeof(*uuid) - 1);
-               temp[sizeof(*uuid) - 1] = '\0';
+               static char temp[sizeof(*uuid->uuid)];
+
+               memcpy(temp, uuid->uuid, sizeof(*uuid->uuid) - 1);
+               temp[sizeof(*uuid->uuid) - 1] = '\0';
+
                return temp;
        }
        return (char *)(uuid->uuid);
index e7395b2..a46b598 100644 (file)
 int main(int argc, char **argv)
 {
        int rc;
-       int fd = open(argv[1], O_WRONLY);
+       int fd;
+       void *volatile buf = (void *)0x4096000;
+       void *volatile fd_ptr;
 
+       fd = open(argv[1], O_WRONLY);
        if (fd == -1) {
                perror(argv[1]);
                goto read;
@@ -44,27 +47,28 @@ int main(int argc, char **argv)
 
        /* We need rc because Sles11 compiler warns against unchecked
         * return value of read and write */
-       rc = write(fd, (void *)0x4096000, 5);
+       rc = write(fd, buf, 5);
        if (rc != 5)
                perror("write badarea (Should have failed)");
 
-       rc = write(fd, &fd, 0);
+       fd_ptr = (void *)&fd;
+       rc = write(fd, fd_ptr, 0);
        if (rc != 0)
                perror("write zero bytes");
 
-       rc = write(fd, &fd, 1);
+       rc = write(fd, fd_ptr, 1);
        if (rc != 1)
                perror("write one byte");
 
-       rc = write(fd, &fd, 2UL*1024*1024);
+       rc = write(fd, fd_ptr, 2UL*1024*1024);
        if (rc != 2UL*1024*1024)
                perror("write 2M");
 
-       rc = write(fd, &fd, 2UL*1024*1024*1024);
+       rc = write(fd, fd_ptr, 2UL*1024*1024*1024);
        if (rc != 2UL*1024*1024*1024)
                perror("write 2G");
 
-       rc = write(fd, &fd, -2);
+       rc = write(fd, fd_ptr, -2);
        if (rc != -2)
                perror("write -2");
 
@@ -74,7 +78,7 @@ read:
        fd = open(argv[1], O_RDONLY);
        if (fd == -1)
                return 0;
-       rc = read(fd, (void *)0x4096000, 5);
+       rc = read(fd, buf, 5);
        perror("read");
 
        close(fd);