[PATCH xserver 6/8] xkb: Silence some compiler warnings

Adam Jackson ajax at redhat.com
Thu Apr 5 17:13:59 UTC 2018


Of the form:

../xkb/XKBGAlloc.c: In function ‘SrvXkbAddGeomKeyAlias’:
../xkb/XKBGAlloc.c:591:13: warning: ‘strncpy’ specified bound 4 equals destination size [-Wstringop-truncation]
             strncpy(alias->real, realStr, XkbKeyNameLength);
             ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

This is intentional; the code that reads from these fields never reads
more than 4 bytes anyway. Rephrase things in terms of memcpy so that's
clear. Obviously this is awful but in XKB awful is par.

Signed-off-by: Adam Jackson <ajax at redhat.com>
---
 xkb/XKBGAlloc.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)

diff --git a/xkb/XKBGAlloc.c b/xkb/XKBGAlloc.c
index e9f55fa434..8958b0c523 100644
--- a/xkb/XKBGAlloc.c
+++ b/xkb/XKBGAlloc.c
@@ -588,7 +588,8 @@ XkbAddGeomKeyAlias(XkbGeometryPtr geom, char *aliasStr, char *realStr)
          i++, alias++) {
         if (strncmp(alias->alias, aliasStr, XkbKeyNameLength) == 0) {
             memset(alias->real, 0, XkbKeyNameLength);
-            strncpy(alias->real, realStr, XkbKeyNameLength);
+            memcpy(alias->real, realStr,
+                   min(XkbKeyNameLength, strlen(realStr)));
             return alias;
         }
     }
@@ -598,8 +599,8 @@ XkbAddGeomKeyAlias(XkbGeometryPtr geom, char *aliasStr, char *realStr)
     }
     alias = &geom->key_aliases[geom->num_key_aliases];
     memset(alias, 0, sizeof(XkbKeyAliasRec));
-    strncpy(alias->alias, aliasStr, XkbKeyNameLength);
-    strncpy(alias->real, realStr, XkbKeyNameLength);
+    memcpy(alias->alias, aliasStr, min(XkbKeyNameLength, strlen(aliasStr)));
+    memcpy(alias->real, realStr, min(XkbKeyNameLength, strlen(realStr)));
     geom->num_key_aliases++;
     return alias;
 }
@@ -814,8 +815,8 @@ XkbAddGeomOverlayKey(XkbOverlayPtr overlay,
         (_XkbAllocOverlayKeys(row, 1) != Success))
         return NULL;
     key = &row->keys[row->num_keys];
-    strncpy(key->under.name, under, XkbKeyNameLength);
-    strncpy(key->over.name, over, XkbKeyNameLength);
+    memcpy(key->under.name, under, min(XkbKeyNameLength, strlen(under)));
+    memcpy(key->over.name, over, min(XkbKeyNameLength, strlen(over)));
     row->num_keys++;
     return key;
 }
-- 
2.16.2



More information about the xorg-devel mailing list