[lokisetup] [PATCH] wrong use of strncat

Stephane Peter megastep at megastep.org
Tue Dec 12 15:06:30 EST 2006


Thanks, Ludwig. I've just committed your patch to CVS.

Ludwig Nussel wrote:

>Hi,
>
>gcc warns about some wrong uses of strncat, attached patch fixes
>them in loki_setupdb. strncpy is not used correctly either but
>well...
>
>cu
>Ludwig
>
>  
>
>------------------------------------------------------------------------
>
>Index: loki_setupdb/convert.c
>===================================================================
>--- loki_setupdb.orig/convert.c
>+++ loki_setupdb/convert.c
>@@ -36,8 +36,8 @@ const char *get_path(const char *path)
>         strncpy(buf, path, sizeof(buf));
>     } else {
>         getcwd(buf, sizeof(buf));
>-        strncat(buf, "/", sizeof(buf));
>-        strncat(buf, path, sizeof(buf));
>+        strncat(buf, "/", sizeof(buf)-strlen(buf)-1);
>+        strncat(buf, path, sizeof(buf)-strlen(buf)-1);
>     }
>     ptr = strrchr(buf, '/');
>     if ( ptr ) {
>Index: loki_setupdb/setupdb.c
>===================================================================
>--- loki_setupdb.orig/setupdb.c
>+++ loki_setupdb/setupdb.c
>@@ -646,11 +646,11 @@ product_t *loki_create_product(const cha
> 	snprintf(homefile, sizeof(homefile), "%s/" LOKI_DIRNAME, detect_home());
> 	mkdir(homefile, 0700);
> 
>-	strncat(homefile, "/installed", sizeof(homefile)-strlen(homefile));
>+	strncat(homefile, "/installed", sizeof(homefile)-strlen(homefile)-1);
> 	mkdir(homefile, 0700);
> 
>-	strncat(homefile, "/", sizeof(homefile)-strlen(homefile));
>-	strncat(homefile, get_xml_base(), sizeof(homefile)-strlen(homefile));
>+	strncat(homefile, "/", sizeof(homefile)-strlen(homefile)-1);
>+	strncat(homefile, get_xml_base(), sizeof(homefile)-strlen(homefile)-1);
> 	mkdir(homefile, 0700);
> 
> 	/* Clean up the root - it can't have a trailing slash */
>@@ -667,13 +667,13 @@ product_t *loki_create_product(const cha
>         return NULL;
>     }
> 
>-	strncat(homefile, "/", sizeof(homefile)-strlen(homefile));
>-	strncat(homefile, name, sizeof(homefile)-strlen(homefile));
>-	strncat(homefile, ".xml", sizeof(homefile)-strlen(homefile));
>-
>-	strncat(manifest, "/", sizeof(manifest)-strlen(manifest));
>-	strncat(manifest, name, sizeof(manifest)-strlen(manifest));
>-	strncat(manifest, ".xml", sizeof(manifest)-strlen(manifest));
>+	strncat(homefile, "/", sizeof(homefile)-strlen(homefile)-1);
>+	strncat(homefile, name, sizeof(homefile)-strlen(homefile)-1);
>+	strncat(homefile, ".xml", sizeof(homefile)-strlen(homefile)-1);
>+
>+	strncat(manifest, "/", sizeof(manifest)-strlen(manifest)-1);
>+	strncat(manifest, name, sizeof(manifest)-strlen(manifest)-1);
>+	strncat(manifest, ".xml", sizeof(manifest)-strlen(manifest)-1);
> 
> 
>     /* Symlink the file in the 'installed' per-user directory */
>@@ -2258,15 +2258,15 @@ int loki_upgrade_uninstall(product_t *pr
>     snprintf(binpath, sizeof(binpath), "%s/" LOKI_DIRNAME "/installed/bin", detect_home());
>     mkdir(binpath, 0755);
> 
>-    strncat(binpath, "/", sizeof(binpath));
>-    strncat(binpath, os_name, sizeof(binpath));
>+    strncat(binpath, "/", sizeof(binpath)-strlen(binpath)-1);
>+    strncat(binpath, os_name, sizeof(binpath)-strlen(binpath)-1);
>     mkdir(binpath, 0755);
> 
>-    strncat(binpath, "/", sizeof(binpath));
>-    strncat(binpath, detect_arch(), sizeof(binpath));
>+    strncat(binpath, "/", sizeof(binpath)-strlen(binpath)-1);
>+    strncat(binpath, detect_arch(), sizeof(binpath)-strlen(binpath)-1);
>     mkdir(binpath, 0755);
> 
>-    strncat(binpath, "/uninstall", sizeof(binpath));
>+    strncat(binpath, "/uninstall", sizeof(binpath)-strlen(binpath)-1);
> 
>     if ( !access(binpath, X_OK) && !access(src_bins, R_OK) ) {
>         char cmd[PATH_MAX];
>@@ -2369,7 +2369,7 @@ int loki_upgrade_uninstall(product_t *pr
> 						 lang);
> 				mkdir(binpath, 0755);
> 				
>-				strncat(binpath, "/LC_MESSAGES", sizeof(binpath));
>+				strncat(binpath, "/LC_MESSAGES", sizeof(binpath)-strlen(binpath)-1);
> 				mkdir(binpath, 0755);
> 
> 				snprintf(binpath, sizeof(binpath),
>  
>


-- 
Stephane Peter
megastep at megastep.org

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://icculus.org/pipermail/lokisetup/attachments/20061212/4c4967ef/attachment.htm>


More information about the Lokisetup mailing list