[PATCH 2/5] Convert strncpy/strncat to strlcpy/strlcat

Alan Coopersmith alan.coopersmith at oracle.com
Fri Oct 28 21:41:50 PDT 2011


As long as we're carrying around a compatibility copy in os/strl*.c,
might as well use them.

Signed-off-by: Alan Coopersmith <alan.coopersmith at oracle.com>
---
 Xext/xvmc.c                    |    6 ++----
 hw/dmx/config/dmxcompat.c      |    3 +--
 hw/dmx/config/scanner.l        |    2 +-
 hw/dmx/dmxinit.c               |    2 +-
 hw/xfree86/common/xf86Config.c |    3 +--
 hw/xfree86/common/xf86Xinput.c |    5 ++---
 hw/xfree86/common/xf86pciBus.c |   12 +++++-------
 hw/xfree86/parser/scan.c       |    6 ++++--
 hw/xquartz/xpr/dri.c           |    3 +--
 os/access.c                    |    6 ++----
 xkb/ddxLoad.c                  |    6 ++----
 xkb/maprules.c                 |    3 +--
 xkb/xkbtext.c                  |    3 +--
 13 files changed, 24 insertions(+), 36 deletions(-)

diff --git a/Xext/xvmc.c b/Xext/xvmc.c
index bc78b55..47b9f47 100644
--- a/Xext/xvmc.c
+++ b/Xext/xvmc.c
@@ -777,14 +777,12 @@ xf86XvMCRegisterDRInfo(ScreenPtr pScreen, char *name,
 		       int patchLevel)
 {
     XvMCScreenPtr pScreenPriv = XVMC_GET_PRIVATE(pScreen);
-    strncpy(pScreenPriv->clientDriverName, name,
+    strlcpy(pScreenPriv->clientDriverName, name,
 	    DR_CLIENT_DRIVER_NAME_SIZE);
-    strncpy(pScreenPriv->busID, busID, DR_BUSID_SIZE);
+    strlcpy(pScreenPriv->busID, busID, DR_BUSID_SIZE);
     pScreenPriv->major = major;
     pScreenPriv->minor = minor;
     pScreenPriv->patchLevel = patchLevel;
-    pScreenPriv->clientDriverName[DR_CLIENT_DRIVER_NAME_SIZE-1] = 0;
-    pScreenPriv->busID[DR_BUSID_SIZE-1] = 0;
     return Success;
 }
 
diff --git a/hw/dmx/config/dmxcompat.c b/hw/dmx/config/dmxcompat.c
index b4190ff..98c52eb 100644
--- a/hw/dmx/config/dmxcompat.c
+++ b/hw/dmx/config/dmxcompat.c
@@ -94,8 +94,7 @@ static void dmxVDLDisplayEntry(const char *buf,
     char       *end;
 
     pt   = strchr(buf, ' ');
-    strncpy(name, buf, pt-buf);
-    name[pt-buf] = '\0';
+    strlcpy(name, buf, 1+pt-buf);
     *len  = strlen(name);
     
     *x     = strtol(pt, &end, 10);
diff --git a/hw/dmx/config/scanner.l b/hw/dmx/config/scanner.l
index cef99d0..5ce268d 100644
--- a/hw/dmx/config/scanner.l
+++ b/hw/dmx/config/scanner.l
@@ -152,7 +152,7 @@ static int getdimension(int token, const char *text, int leng)
     char *tmp = dmxConfigAlloc(leng+1);
     int  x, y;
 
-    strncpy(tmp, text, leng);
+    strlcpy(tmp, text, leng+1);
     x = strtol(tmp, &endptr, 10);
     while (*endptr && !isdigit(*endptr)) ++endptr;
     y = strtol(endptr, NULL, 10);
diff --git a/hw/dmx/dmxinit.c b/hw/dmx/dmxinit.c
index 165476c..b950c50 100644
--- a/hw/dmx/dmxinit.c
+++ b/hw/dmx/dmxinit.c
@@ -138,7 +138,7 @@ static int dmxErrorHandler(Display *dpy, XErrorEvent *ev)
         for (ext = dpy->ext_procs;
              ext && ext->codes.major_opcode != ev->request_code;
              ext = ext->next);
-        if (ext) strncpy(buf, ext->name, sizeof(buf));
+        if (ext) strlcpy(buf, ext->name, sizeof(buf));
         else     buf[0] = '\0';
     }
     dmxLog(dmxWarning, "                 Major opcode: %d (%s)\n",
diff --git a/hw/xfree86/common/xf86Config.c b/hw/xfree86/common/xf86Config.c
index cb4be42..cc79973 100644
--- a/hw/xfree86/common/xf86Config.c
+++ b/hw/xfree86/common/xf86Config.c
@@ -195,8 +195,7 @@ xf86ValidateFontPath(char *path)
 	dirlen = p1 - path_elem;
       else
 	dirlen = strlen(path_elem);
-      strncpy(dir_elem, path_elem, dirlen);
-      dir_elem[dirlen] = '\0';
+      strlcpy(dir_elem, path_elem, dirlen + 1);
       flag = stat(dir_elem, &stat_buf);
       if (flag == 0)
 	if (!S_ISDIR(stat_buf.st_mode))
diff --git a/hw/xfree86/common/xf86Xinput.c b/hw/xfree86/common/xf86Xinput.c
index 425b359..63f0039 100644
--- a/hw/xfree86/common/xf86Xinput.c
+++ b/hw/xfree86/common/xf86Xinput.c
@@ -460,10 +460,9 @@ HostOS(void)
 
     if (*host_os == '\0') {
         if (uname(&name) >= 0)
-            strcpy(host_os, name.sysname);
+            strlcpy(host_os, name.sysname, sizeof(host_os));
         else {
-            strncpy(host_os, "unknown", sizeof(host_os));
-            host_os[sizeof(host_os)-1] = '\0';
+            strlcpy(host_os, "unknown", sizeof(host_os));
         }
     }
     return host_os;
diff --git a/hw/xfree86/common/xf86pciBus.c b/hw/xfree86/common/xf86pciBus.c
index eb5323c..c0591a1 100644
--- a/hw/xfree86/common/xf86pciBus.c
+++ b/hw/xfree86/common/xf86pciBus.c
@@ -1210,9 +1210,9 @@ matchDriverFromFiles (char** matches, uint16_t match_vendor, uint16_t match_chip
         /* A tiny bit of sanity checking. We should probably do better */
         if (strncmp(&(direntry->d_name[len-4]), ".ids", 4) == 0) {
             /* We need the full path name to open the file */
-            strncpy(path_name, PCI_TXT_IDS_PATH, 256);
-            strncat(path_name, "/", 1);
-            strncat(path_name, direntry->d_name, (256 - strlen(path_name) - 1));
+            strlcpy(path_name, PCI_TXT_IDS_PATH, sizeof(path_name));
+            strlcat(path_name, "/", sizeof(path_name));
+            strlcat(path_name, direntry->d_name, sizeof(path_name));
             fp = fopen(path_name, "r");
             if (fp == NULL) {
                 xf86Msg(X_ERROR, "Could not open %s for reading. Exiting.\n", path_name);
@@ -1226,8 +1226,7 @@ matchDriverFromFiles (char** matches, uint16_t match_vendor, uint16_t match_chip
 #endif /* __GLIBC __ */
                 xchomp(line);
                 if (isdigit(line[0])) {
-                    strncpy(vendor_str, line, 4);
-                    vendor_str[4] = '\0';
+                    strlcpy(vendor_str, line, sizeof(vendor_str));
                     vendor = (int)strtol(vendor_str, NULL, 16);
                     if ((strlen(&line[4])) == 0) {
                         chip_str[0] = '\0';
@@ -1239,8 +1238,7 @@ matchDriverFromFiles (char** matches, uint16_t match_vendor, uint16_t match_chip
                             chip = -1;
                         } else {
                             /* Ok, it's a real ID */
-                            strncpy(chip_str, &line[4], 4);
-                            chip_str[4] = '\0';
+                            strlcpy(chip_str, &line[4], sizeof(chip_str));
                             chip = (int)strtol(chip_str, NULL, 16);
                         }
                     }
diff --git a/hw/xfree86/parser/scan.c b/hw/xfree86/parser/scan.c
index 1cff3bc..8d2855f 100644
--- a/hw/xfree86/parser/scan.c
+++ b/hw/xfree86/parser/scan.c
@@ -281,8 +281,10 @@ again:
 				if (builtinConfig[builtinIndex] == NULL)
 					ret = NULL;
 				else {
-					ret = strncpy(configBuf, builtinConfig[builtinIndex],
-							CONFIG_BUF_LEN);
+					strlcpy(configBuf,
+						builtinConfig[builtinIndex],
+						CONFIG_BUF_LEN);
+					ret = configBuf;
 					builtinIndex++;
 				}
 			}
diff --git a/hw/xquartz/xpr/dri.c b/hw/xquartz/xpr/dri.c
index 8bae6b0..a58f2c7 100644
--- a/hw/xquartz/xpr/dri.c
+++ b/hw/xquartz/xpr/dri.c
@@ -813,8 +813,7 @@ Bool DRICreatePixmap(ScreenPtr pScreen, Drawable id,
 	return FALSE;
     }
     
-    strncpy(path, shared->shmPath, pathmax);
-    path[pathmax - 1] = '\0';
+    strlcpy(path, shared->shmPath, pathmax);
     
     dixSetPrivate(&pPix->devPrivates, DRIPixmapBufferPrivKey, shared);
 
diff --git a/os/access.c b/os/access.c
index f31626d..1598940 100644
--- a/os/access.c
+++ b/os/access.c
@@ -1756,8 +1756,7 @@ siHostnameAddrMatch(int family, pointer addr, int len,
 	if (siAddrLen >= sizeof(hostname)) 
 	    return FALSE;
 
-	strncpy(hostname, siAddr, siAddrLen);
-	hostname[siAddrLen] = '\0';
+	strlcpy(hostname, siAddr, siAddrLen + 1);
 
 	if (getaddrinfo(hostname, NULL, NULL, &addresses) == 0) {
 	    for (a = addresses ; a != NULL ; a = a->ai_next) {
@@ -1786,8 +1785,7 @@ siHostnameAddrMatch(int family, pointer addr, int len,
 	if (siAddrLen >= sizeof(hostname)) 
 	    return FALSE;
 
-	strncpy(hostname, siAddr, siAddrLen);
-	hostname[siAddrLen] = '\0';
+	strlcpy(hostname, siAddr, siAddrLen + 1);
 
 	if ((hp = _XGethostbyname(hostname, hparams)) != NULL) {
 #ifdef h_addr				/* new 4.3bsd version of gethostent */
diff --git a/xkb/ddxLoad.c b/xkb/ddxLoad.c
index e102035..ff32fc3 100644
--- a/xkb/ddxLoad.c
+++ b/xkb/ddxLoad.c
@@ -263,8 +263,7 @@ XkbDDXCompileKeymapByNames(	XkbDescPtr		xkb,
             if (xkbDebugFlags)
                 DebugF("[xkb] xkb executes: %s\n",buf);
 	    if (nameRtrn) {
-		strncpy(nameRtrn,keymap,nameRtrnLen);
-		nameRtrn[nameRtrnLen-1]= '\0';
+		strlcpy(nameRtrn,keymap,nameRtrnLen);
 	    }
             free(buf);
 	    return TRUE;
@@ -319,8 +318,7 @@ FILE *	file;
     }
     else file= NULL;
     if ((fileNameRtrn!=NULL)&&(fileNameRtrnLen>0)) {
-	strncpy(fileNameRtrn,buf,fileNameRtrnLen);
-	buf[fileNameRtrnLen-1]= '\0';
+	strlcpy(fileNameRtrn,buf,fileNameRtrnLen);
     }
     return file;
 }
diff --git a/xkb/maprules.c b/xkb/maprules.c
index f940899..a14fbbb 100644
--- a/xkb/maprules.c
+++ b/xkb/maprules.c
@@ -250,8 +250,7 @@ get_index(char *str, int *ndx)
        *ndx = -1;
        return end + 1;
    }
-   strncpy(ndx_buf, str, end - str);
-   ndx_buf[end - str] = '\0';
+   strlcpy(ndx_buf, str, 1 + end - str);
    *ndx = atoi(ndx_buf);
    return end + 1;
 }
diff --git a/xkb/xkbtext.c b/xkb/xkbtext.c
index 9f49d59..1ba10a1 100644
--- a/xkb/xkbtext.c
+++ b/xkb/xkbtext.c
@@ -81,8 +81,7 @@ char	*rtrn,*tmp;
 	if (len>BUFFER_SIZE)
 	    len= BUFFER_SIZE-2;
 	rtrn= tbGetBuffer(len);
-	strncpy(rtrn,atmstr,len);
-	rtrn[len]= '\0';
+	strlcpy(rtrn,atmstr,len);
     }
     else {
 	rtrn= tbGetBuffer(1);
-- 
1.7.3.2



More information about the xorg-devel mailing list