Commit e564235b436159f23a77270752ffdee1867f27c8

Daniel Stone 2010-06-15T19:43:14

xkbcomp: Don't malloc() and free() most scanned tokens Use a constant buffer instead. Sigh. Signed-off-by: Daniel Stone <daniel@fooishbar.org>

diff --git a/src/xkbcomp/parseutils.h b/src/xkbcomp/parseutils.h
index ea04240..0fecb42 100644
--- a/src/xkbcomp/parseutils.h
+++ b/src/xkbcomp/parseutils.h
@@ -33,7 +33,7 @@
 
 #include "xkbcomp.h"
 
-extern char *scanStr;
+extern char scanBuf[1024];
 extern int scanInt;
 extern int lineNum;
 
diff --git a/src/xkbcomp/xkbparse.y b/src/xkbcomp/xkbparse.y
index 68e261f..1c05356 100644
--- a/src/xkbcomp/xkbparse.y
+++ b/src/xkbcomp/xkbparse.y
@@ -313,10 +313,10 @@ Decl		:	OptMergeMode VarDecl
 			{
 			    if ($1==MergeAltForm) {
 				yyerror("cannot use 'alternate' to include other maps");
-				$$= &IncludeCreate(scanStr,MergeDefault)->common;
+				$$= &IncludeCreate(scanBuf,MergeDefault)->common;
 			    }
 			    else {
-				$$= &IncludeCreate(scanStr,$1)->common;
+				$$= &IncludeCreate(scanBuf,$1)->common;
 			    }
                         }
 		;
@@ -723,7 +723,7 @@ KeySymList	:	KeySymList COMMA KeySym
 			{ $$= CreateKeysymList($1); }
 		;
 
-KeySym		:	IDENT	{ $$= scanStr; scanStr= NULL; }
+KeySym		:	IDENT	{ $$= strdup(scanBuf); }
 		|	SECTION	{ $$= strdup("section"); }
 		|	Integer		
 			{
@@ -753,21 +753,21 @@ Float		:	FLOAT		{ $$= scanInt; }
 Integer		:	INTEGER		{ $$= scanInt; }
 		;
 
-KeyName		:	KEYNAME		{ $$= scanStr; scanStr= NULL; }
+KeyName		:	KEYNAME		{ $$= strdup(scanBuf); }
 		;
 
-Ident		:	IDENT	{ $$= XkbcInternAtom(scanStr,False); }
+Ident		:	IDENT	{ $$= XkbcInternAtom(scanBuf,False); }
 		|	DEFAULT { $$= XkbcInternAtom("default",False); }
 		;
 
-String		:	STRING	{ $$= XkbcInternAtom(scanStr,False); }
+String		:	STRING	{ $$= XkbcInternAtom(scanBuf,False); }
 		;
 
 OptMapName	:	MapName	{ $$= $1; }
 		|		{ $$= NULL; }
 		;
 
-MapName		:	STRING 	{ $$= scanStr; scanStr= NULL; }
+MapName		:	STRING 	{ $$= strdup(scanBuf); }
 		;
 %%
 void
@@ -776,8 +776,8 @@ yyerror(const char *s)
     if (warningLevel>0) {
 	(void)fprintf(stderr,"%s: line %d of %s\n",s,lineNum,
 					(scanFile?scanFile:"(unknown)"));
-	if ((scanStr)&&(warningLevel>3))
-	    (void)fprintf(stderr,"last scanned symbol is: %s\n",scanStr);
+	if ((warningLevel>3))
+	    (void)fprintf(stderr,"last scanned symbol is: %s\n",scanBuf);
     }
     return;
 }
diff --git a/src/xkbcomp/xkbscan.c b/src/xkbcomp/xkbscan.c
index 1bf363d..e03813c 100644
--- a/src/xkbcomp/xkbscan.c
+++ b/src/xkbcomp/xkbscan.c
@@ -43,11 +43,10 @@ int lineNum = 0;
 
 int scanInt;
 
-char *scanStr = NULL;
+char scanBuf[1024];
 static int scanStrLine = 0;
 
 #define	BUFSIZE	4096
-static int nInBuf = 0;
 static char readBuf[BUFSIZE];
 static int readBufPos = 0;
 static int readBufLen = 0;
@@ -218,7 +217,7 @@ tokText(int tok)
         break;
 
     case STRING:
-        snprintf(buf, sizeof(buf), "STRING (%s)", scanStr);
+        snprintf(buf, sizeof(buf), "STRING (%s)", scanBuf);
         break;
     case INTEGER:
         snprintf(buf, sizeof(buf), "INTEGER (0x%x)", scanInt);
@@ -228,10 +227,10 @@ tokText(int tok)
                 scanInt / XkbGeomPtsPerMM, scanInt % XkbGeomPtsPerMM);
         break;
     case IDENT:
-        snprintf(buf, sizeof(buf), "IDENT (%s)", scanStr);
+        snprintf(buf, sizeof(buf), "IDENT (%s)", scanBuf);
         break;
     case KEYNAME:
-        snprintf(buf, sizeof(buf), "KEYNAME (%s)", scanStr);
+        snprintf(buf, sizeof(buf), "KEYNAME (%s)", scanBuf);
         break;
 
     case PARTIAL:
@@ -308,7 +307,6 @@ static int
 yyGetString(void)
 {
     int ch, i;
-    char buf[1024];
 
     i = 0;
     while (((ch = scanchar()) != EOF) && (ch != '"'))
@@ -376,15 +374,12 @@ yyGetString(void)
             else
                 return ERROR_TOK;
         }
-        if (i < sizeof(buf) - 1)
-            buf[i++] = ch;
+        if (i < sizeof(scanBuf) - 1)
+            scanBuf[i++] = ch;
     }
     if (ch == '"')
     {
-        buf[i++] = '\0';
-        if (scanStr)
-            uFree(scanStr);
-        scanStr = strdup(buf);
+        scanBuf[i++] = '\0';
         scanStrLine = lineNum;
         return STRING;
     }
@@ -395,7 +390,6 @@ static int
 yyGetKeyName(void)
 {
     int ch, i;
-    char buf[1024];
 
     i = 0;
     while (((ch = scanchar()) != EOF) && (ch != '>'))
@@ -458,15 +452,12 @@ yyGetKeyName(void)
                 return ERROR_TOK;
         }
 
-        if (i < sizeof(buf) - 1)
-            buf[i++] = ch;
+        if (i < sizeof(scanBuf) - 1)
+            scanBuf[i++] = ch;
     }
     if ((ch == '>') && (i < 5))
     {
-        buf[i++] = '\0';
-        if (scanStr)
-            uFree(scanStr);
-        scanStr = strdup(buf);
+        scanBuf[i++] = '\0';
         scanStrLine = lineNum;
         return KEYNAME;
     }
@@ -577,21 +568,20 @@ yyGetIdent(int first)
 {
     int ch, i, j, found;
     int rtrn = IDENT;
-    char buf[1024];
 
-    buf[0] = first;
+    scanBuf[0] = first;
     j = 1;
     while (((ch = scanchar()) != EOF) && (isalnum(ch) || (ch == '_')))
     {
-        if (j < sizeof(buf) - 1)
-            buf[j++] = ch;
+        if (j < sizeof(scanBuf) - 1)
+            scanBuf[j++] = ch;
     }
-    buf[j++] = '\0';
+    scanBuf[j++] = '\0';
     found = 0;
 
     for (i = 0; (!found) && (i < numKeywords); i++)
     {
-        if (uStrCaseCmp(buf, keywords[i].keyword) == 0)
+        if (uStrCaseCmp(scanBuf, keywords[i].keyword) == 0)
         {
             rtrn = keywords[i].token;
             found = 1;
@@ -599,9 +589,6 @@ yyGetIdent(int first)
     }
     if (!found)
     {
-        if (scanStr)
-            uFree(scanStr);
-        scanStr = strdup(buf);
         scanStrLine = lineNum;
         rtrn = IDENT;
     }
@@ -619,6 +606,7 @@ yyGetNumber(int ch)
 {
     int isFloat = 0;
     char buf[1024];
+    int nInBuf = 0;
 
     buf[0] = ch;
     nInBuf = 1;