Avoid some GCC 10.X warnings in HotSpot

Koichi Sakata sakatakui at oss.nttdata.com
Wed Jun 17 22:46:14 UTC 2020


Hi all,

I tried to build OpenJDK fastdebug with GCC 10.1 on Ubuntu 18.04, but I 
saw some compiler warnings as follows:

In file included from 
/home/jyukutyo/code/jdk/src/hotspot/share/classfile/systemDictionary.hpp:31,
                  from 
/home/jyukutyo/code/jdk/src/hotspot/share/classfile/javaClasses.hpp:28,
                  from 
/home/jyukutyo/code/jdk/src/hotspot/share/precompiled/precompiled.hpp:35:
In member function 'void Symbol::byte_at_put(int, u1)',
     inlined from 'Symbol::Symbol(const u1*, int, int)' at 
/home/jyukutyo/code/jdk/src/hotspot/share/oops/symbol.cpp:55:16:
/home/jyukutyo/code/jdk/src/hotspot/share/oops/symbol.hpp:130:18: error: 
writing 1 byte into a region of size 0 [-Werror=stringop-overflow=]
   130 |     _body[index] = value;
       |     ~~~~~~~~~~~~~^~~~~~~

/home/jyukutyo/code/jdk/src/java.base/share/native/libfdlibm/k_standard.c: 
In function '__j__kernel_standard':
/home/jyukutyo/code/jdk/src/java.base/share/native/libfdlibm/k_standard.c:743:19: 
error: 'exc.retval' may be used uninitialized in this function 
[-Werror=maybe-uninitialized]
   743 |         return exc.retval;
       |                ~~~^~~~~~~

In file included from /usr/include/string.h:494,
                  from 
/home/jyukutyo/code/jdk/src/java.base/unix/native/libnet/NetworkInterface.c:30:
In function 'strncpy',
     inlined from 'getFlags' at 
/home/jyukutyo/code/jdk/src/java.base/unix/native/libnet/NetworkInterface.c:1362:5,
     inlined from 'addif' at 
/home/jyukutyo/code/jdk/src/java.base/unix/native/libnet/NetworkInterface.c:974:13:
/usr/include/x86_64-linux-gnu/bits/string_fortified.h:106:10: error: 
'__builtin_strncpy' output may be truncated copying 15 bytes from a 
string of length 15 [-Werror=stringop-truncation]
   106 |   return __builtin___strncpy_chk (__dest, __src, __len, __bos 
(__dest));
       | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

I can resolve them with the following patch. I believe it fixes those 
potential bugs, so I'd like to contribute it.
(Our company has signed OCA.)

Thanks,
Koichi

===== PATCH =====
diff -r 20d92fe3ac52 src/hotspot/share/oops/symbol.cpp
--- a/src/hotspot/share/oops/symbol.cpp	Tue Jun 16 03:16:41 2020 +0000
+++ b/src/hotspot/share/oops/symbol.cpp	Thu Jun 18 07:08:50 2020 +0900
@@ -50,9 +50,10 @@
  Symbol::Symbol(const u1* name, int length, int refcount) {
    _hash_and_refcount =  pack_hash_and_refcount((short)os::random(), 
refcount);
    _length = length;
-  _body[0] = 0;  // in case length == 0
-  for (int i = 0; i < length; i++) {
-    byte_at_put(i, name[i]);
+  if (length == 0) {
+    _body[0] = 0;
+  } else {
+    memcpy(_body, name, length);
    }
  }

diff -r 20d92fe3ac52 src/hotspot/share/oops/symbol.hpp
--- a/src/hotspot/share/oops/symbol.hpp	Tue Jun 16 03:16:41 2020 +0000
+++ b/src/hotspot/share/oops/symbol.hpp	Thu Jun 18 07:08:50 2020 +0900
@@ -125,11 +125,6 @@
      return (int)heap_word_size(byte_size(length));
    }

-  void byte_at_put(int index, u1 value) {
-    assert(index >=0 && index < length(), "symbol index overflow");
-    _body[index] = value;
-  }
-
    Symbol(const u1* name, int length, int refcount);
    void* operator new(size_t size, int len) throw();
    void* operator new(size_t size, int len, Arena* arena) throw();
diff -r 20d92fe3ac52 src/java.base/share/native/libfdlibm/k_standard.c
--- a/src/java.base/share/native/libfdlibm/k_standard.c	Tue Jun 16 
03:16:41 2020 +0000
+++ b/src/java.base/share/native/libfdlibm/k_standard.c	Thu Jun 18 
07:08:50 2020 +0900
@@ -739,6 +739,10 @@
                          errno = EDOM;
                  }
                  break;
+            default:
+                exc.retval = zero;
+                errno = EINVAL;
+                break;
          }
          return exc.retval;
  }
diff -r 20d92fe3ac52 src/java.base/unix/native/libnet/NetworkInterface.c
--- a/src/java.base/unix/native/libnet/NetworkInterface.c	Tue Jun 16 
03:16:41 2020 +0000
+++ b/src/java.base/unix/native/libnet/NetworkInterface.c	Thu Jun 18 
07:08:50 2020 +0900
@@ -1296,7 +1296,10 @@
  static int getIndex(int sock, const char *name) {
      struct ifreq if2;
      memset((char *)&if2, 0, sizeof(if2));
-    strncpy(if2.ifr_name, name, sizeof(if2.ifr_name) - 1);
+    if (sizeof(if2.ifr_name) < sizeof(name)) {
+        return -1;
+    }
+    strcpy(if2.ifr_name, name);

      if (ioctl(sock, SIOCGIFINDEX, (char *)&if2) < 0) {
          return -1;
@@ -1359,7 +1362,10 @@
  static int getFlags(int sock, const char *ifname, int *flags) {
      struct ifreq if2;
      memset((char *)&if2, 0, sizeof(if2));
-    strncpy(if2.ifr_name, ifname, sizeof(if2.ifr_name) - 1);
+    if (sizeof(if2.ifr_name) < sizeof(ifname)) {
+        return -1;
+    }
+    strcpy(if2.ifr_name, ifname);

      if (ioctl(sock, SIOCGIFFLAGS, (char *)&if2) < 0) {
          return -1;


More information about the core-libs-dev mailing list