[PATCH 1/4] xkb: Use snprintf to measure string lengths instead of manual strlen math

Alan Coopersmith alan.coopersmith at oracle.com
Sun Feb 13 21:36:02 PST 2011


Signed-off-by: Alan Coopersmith <alan.coopersmith at oracle.com>
---
 xkb/ddxLoad.c  |   21 +++++++++++----------
 xkb/maprules.c |    3 +--
 2 files changed, 12 insertions(+), 12 deletions(-)

diff --git a/xkb/ddxLoad.c b/xkb/ddxLoad.c
index 51b5777..9686ea8 100644
--- a/xkb/ddxLoad.c
+++ b/xkb/ddxLoad.c
@@ -303,15 +303,16 @@ FILE *	file;
                 &&(!isalpha(xkm_output_dir[0]) || xkm_output_dir[1]!=':')
 #endif
                 ) {
-	    if (strlen(XkbBaseDirectory)+strlen(xkm_output_dir)
-		     +strlen(mapName)+6 <= PATH_MAX)
-	    {
-	        sprintf(buf,"%s/%s%s.xkm",XkbBaseDirectory,
-					xkm_output_dir,mapName);
-	    }
+            if (snprintf(buf, PATH_MAX, "%s/%s%s.xkm", XkbBaseDirectory,
+                         xkm_output_dir, mapName) >= PATH_MAX)
+                buf[0] = '\0';
+	}
+	else
+	{
+            if (snprintf(buf, PATH_MAX, "%s%s.xkm", xkm_output_dir, mapName)
+                >= PATH_MAX)
+                buf[0] = '\0';
 	}
-	else if (strlen(xkm_output_dir)+strlen(mapName)+5 <= PATH_MAX)
-	    sprintf(buf,"%s%s.xkm",xkm_output_dir,mapName);
 	if (buf[0] != '\0')
 	    file= fopen(buf,"rb");
 	else file= NULL;
@@ -388,11 +389,11 @@ XkbRF_RulesPtr	rules;
     if (!rules_name)
 	return FALSE;
 
-    if (strlen(XkbBaseDirectory) + strlen(rules_name) + 8 > PATH_MAX) {
+    if (snprintf(buf, PATH_MAX, "%s/rules/%s", XkbBaseDirectory, rules_name)
+        >= PATH_MAX) {
         LogMessage(X_ERROR, "XKB: Rules name is too long\n");
         return FALSE;
     }
-    sprintf(buf,"%s/rules/%s", XkbBaseDirectory, rules_name);
 
     file = fopen(buf, "r");
     if (!file) {
diff --git a/xkb/maprules.c b/xkb/maprules.c
index c683c0d..0c91cdc 100644
--- a/xkb/maprules.c
+++ b/xkb/maprules.c
@@ -945,9 +945,8 @@ Bool		ok;
     if ((!base)||(!rules))
 	return FALSE;
     if (locale) {
-	if (strlen(base)+strlen(locale)+2 > PATH_MAX)
+	if (snprintf(buf, PATH_MAX, "%s-%s", base, locale) >= PATH_MAX)
 	    return FALSE;
-	sprintf(buf,"%s-%s", base, locale);
     }
     else {
 	if (strlen(base)+1 > PATH_MAX)
-- 
1.7.3.2



More information about the xorg-devel mailing list