More strcpy() strcat() sprintf() squashing
authorkvigor <kvigor@4a8a32a2-be11-0410-ad9d-d568d2c75423>
Fri, 21 Feb 2003 23:38:38 +0000 (23:38 +0000)
committerkvigor <kvigor@4a8a32a2-be11-0410-ad9d-d568d2c75423>
Fri, 21 Feb 2003 23:38:38 +0000 (23:38 +0000)
git-svn-id: https://sdcc.svn.sourceforge.net/svnroot/sdcc/trunk/sdcc@2287 4a8a32a2-be11-0410-ad9d-d568d2c75423

14 files changed:
src/SDCC.y
src/SDCCBBlock.c
src/SDCCast.c
src/SDCCglue.c
src/SDCCicode.c
src/SDCCmacro.c
src/SDCCmain.c
src/SDCCmem.c
src/SDCCpeeph.c
src/SDCCsymt.c
src/SDCCutil.c
src/SDCCval.c
src/altlex.c
src/ds390/ralloc.c

index 52da8209ca517216749ef392e389b42e50db9d9a..ab27ae5aef46627737cd0b49f172fb09241e318f 100644 (file)
@@ -34,6 +34,7 @@
 #include "port.h"
 #include "newalloc.h"
 #include "SDCCerr.h"
+#include "SDCCutil.h"
 
 extern int yyerror (char *);
 extern FILE    *yyin;
@@ -868,11 +869,13 @@ opt_assign_expr
                            }                           
    |                       {                              
                               if (cenum)  {
-                                 sprintf(lbuff,"%d",(int) floatFromVal(cenum)+1);
+                                 SNPRINTF(lbuff, sizeof(lbuff), 
+                                         "%d",(int) floatFromVal(cenum)+1);
                                  $$ = cenum = constVal(lbuff);
                               }
                               else {
-                                 sprintf(lbuff,"%d",0);
+                                 SNPRINTF(lbuff, sizeof(lbuff), 
+                                         "%d",0);
                                  $$ = cenum = constVal(lbuff);
                               }   
                            }
@@ -1292,7 +1295,8 @@ selection_statement
                               ex->values.switchVals.swNum = swLabel ;
                                  
                               /* now create the label */
-                              sprintf(lbuff,"_swBrk_%d",swLabel++);
+                              SNPRINTF(lbuff, sizeof(lbuff), 
+                                      "_swBrk_%d",swLabel++);
                               $<sym>$  =  newSymbol(lbuff,NestLevel);
                               /* put label in the break stack  */
                               STACK_PUSH(breakStack,$<sym>$);   
@@ -1308,13 +1312,13 @@ selection_statement
 while : WHILE  {  /* create and push the continue , break & body labels */
                   static int Lblnum = 0 ;
                  /* continue */
-                  sprintf (lbuff,"_whilecontinue_%d",Lblnum);
+                  SNPRINTF (lbuff, sizeof(lbuff), "_whilecontinue_%d",Lblnum);
                  STACK_PUSH(continueStack,newSymbol(lbuff,NestLevel));
                  /* break */
-                 sprintf (lbuff,"_whilebreak_%d",Lblnum);
+                 SNPRINTF (lbuff, sizeof(lbuff), "_whilebreak_%d",Lblnum);
                  STACK_PUSH(breakStack,newSymbol(lbuff,NestLevel));
                  /* body */
-                 sprintf (lbuff,"_whilebody_%d",Lblnum++);
+                 SNPRINTF (lbuff, sizeof(lbuff), "_whilebody_%d",Lblnum++);
                  $$ = newSymbol(lbuff,NestLevel);
                }
    ;
@@ -1323,13 +1327,13 @@ do : DO {  /* create and push the continue , break & body Labels */
            static int Lblnum = 0 ;
 
           /* continue */
-          sprintf(lbuff,"_docontinue_%d",Lblnum);
+          SNPRINTF(lbuff, sizeof(lbuff), "_docontinue_%d",Lblnum);
           STACK_PUSH(continueStack,newSymbol(lbuff,NestLevel));
           /* break */
-          sprintf (lbuff,"_dobreak_%d",Lblnum);
+          SNPRINTF(lbuff, sizeof(lbuff), "_dobreak_%d",Lblnum);
           STACK_PUSH(breakStack,newSymbol(lbuff,NestLevel));
           /* do body */
-          sprintf (lbuff,"_dobody_%d",Lblnum++);
+          SNPRINTF(lbuff, sizeof(lbuff), "_dobody_%d",Lblnum++);
           $$ = newSymbol (lbuff,NestLevel);       
         }
    ;
@@ -1338,16 +1342,16 @@ for : FOR { /* create & push continue, break & body labels */
             static int Lblnum = 0 ;
          
             /* continue */
-           sprintf (lbuff,"_forcontinue_%d",Lblnum);
+           SNPRINTF(lbuff, sizeof(lbuff), "_forcontinue_%d",Lblnum);
            STACK_PUSH(continueStack,newSymbol(lbuff,NestLevel));
            /* break    */
-           sprintf (lbuff,"_forbreak_%d",Lblnum);
+           SNPRINTF(lbuff, sizeof(lbuff), "_forbreak_%d",Lblnum);
            STACK_PUSH(breakStack,newSymbol(lbuff,NestLevel));
            /* body */
-           sprintf (lbuff,"_forbody_%d",Lblnum);
+           SNPRINTF(lbuff, sizeof(lbuff), "_forbody_%d",Lblnum);
            $$ = newSymbol(lbuff,NestLevel);
            /* condition */
-           sprintf (lbuff,"_forcond_%d",Lblnum++);
+           SNPRINTF(lbuff, sizeof(lbuff), "_forcond_%d",Lblnum++);
            STACK_PUSH(forStack,newSymbol(lbuff,NestLevel));
           }
    ;
index d1f1ebef447ccde2645340818449e73dd16f8b95..559d7341844401eaf57732d9388f162d6d8444fa 100644 (file)
@@ -226,7 +226,7 @@ iCode2eBBlock (iCode * ic)
     ebb->entryLabel = ic->argLabel.label;
   else
     {
-      sprintf (buffer, "_eBBlock%d", eBBNum++);
+      SNPRINTF (buffer, sizeof(buffer), "_eBBlock%d", eBBNum++);
       ebb->entryLabel = newSymbol (buffer, 1);
       ebb->entryLabel->key = labelKey++;
     }
index 2a19a3df00167691e44bf012d2c2c759e7bf1642..fe878f9f29cda6b71acf43f0f1b849cdb0bb9496 100644 (file)
@@ -1167,7 +1167,7 @@ stringToSymbol (value * val)
     }
   }
 
-  sprintf (name, "_str_%d", charLbl++);
+  SNPRINTF (name, sizeof(name), "_str_%d", charLbl++);
   sym = newSymbol (name, 0);   /* make it @ level 0 */
   strncpyz (sym->rname, name, SDCC_NAME_MAX);
 
@@ -2960,7 +2960,7 @@ decorateType (ast * tree)
     case SIZEOF:               /* evaluate wihout code generation */
       /* change the type to a integer */
       tree->type = EX_VALUE;
-      sprintf (buffer, "%d", (getSize (tree->right->ftype)));
+      SNPRINTF(buffer, sizeof(buffer), "%d", (getSize (tree->right->ftype)));
       tree->opval.val = constVal (buffer);
       tree->right = tree->left = NULL;
       TETYPE (tree) = getSpec (TTYPE (tree) =
@@ -3033,7 +3033,7 @@ decorateType (ast * tree)
                    break;
                }
            }
-           sprintf (buffer, "%d", typeofv);
+           SNPRINTF (buffer, sizeof(buffer), "%d", typeofv);
            tree->opval.val = constVal (buffer);
            tree->right = tree->left = NULL;
            TETYPE (tree) = getSpec (TTYPE (tree) =
@@ -3413,7 +3413,7 @@ sizeofOp (sym_link * type)
   checkTypeSanity(type, "(sizeof)");
 
   /* get the size and convert it to character  */
-  sprintf (buff, "%d", getSize (type));
+  SNPRINTF (buff, sizeof(buff), "%d", getSize (type));
 
   /* now convert into value  */
   return constVal (buff);
@@ -3447,7 +3447,7 @@ backPatchLabels (ast * tree, symbol * trueLabel, symbol * falseLabel)
       static int localLbl = 0;
       symbol *localLabel;
 
-      sprintf (buffer, "_and_%d", localLbl++);
+      SNPRINTF(buffer, sizeof(buffer), "_and_%d", localLbl++);
       localLabel = newSymbol (buffer, NestLevel);
 
       tree->left = backPatchLabels (tree->left, localLabel, falseLabel);
@@ -3473,7 +3473,7 @@ backPatchLabels (ast * tree, symbol * trueLabel, symbol * falseLabel)
       static int localLbl = 0;
       symbol *localLabel;
 
-      sprintf (buffer, "_or_%d", localLbl++);
+      SNPRINTF(buffer, sizeof(buffer), "_or_%d", localLbl++);
       localLabel = newSymbol (buffer, NestLevel);
 
       tree->left = backPatchLabels (tree->left, trueLabel, localLabel);
@@ -3565,7 +3565,7 @@ createLabel (symbol * label, ast * stmnt)
     label = newSymbol (label->name, label->level);
 
   /* change the name before putting it in add _ */
-  sprintf (name, "%s", label->name);
+  SNPRINTF(name, sizeof(name), "%s", label->name);
 
   /* put the label in the LabelSymbol table    */
   /* but first check if a label of the same    */
@@ -3653,7 +3653,8 @@ createCase (ast * swStat, ast * caseVal, ast * stmnt)
     }
 
   /* create the case label   */
-  sprintf (caseLbl, "_case_%d_%d",
+  SNPRINTF(caseLbl, sizeof(caseLbl), 
+          "_case_%d_%d",
           swStat->values.switchVals.swNum,
           (int) floatFromVal (caseVal->opval.val));
 
@@ -3682,7 +3683,8 @@ createDefault (ast * swStat, ast * stmnt)
   swStat->values.switchVals.swDefault = 1;
 
   /* create the label  */
-  sprintf (defLbl, "_default_%d", swStat->values.switchVals.swNum);
+  SNPRINTF (defLbl, sizeof(defLbl),
+           "_default_%d", swStat->values.switchVals.swNum);
   return createLabel (newSymbol (defLbl, 0), stmnt);
 }
 
@@ -3705,18 +3707,18 @@ createIf (ast * condAst, ast * ifBody, ast * elseBody)
   }
 
   /* create the labels */
-  sprintf (buffer, "_iffalse_%d", Lblnum);
+  SNPRINTF (buffer, sizeof(buffer), "_iffalse_%d", Lblnum);
   ifFalse = newSymbol (buffer, NestLevel);
   /* if no else body then end == false */
   if (!elseBody)
     ifEnd = ifFalse;
   else
     {
-      sprintf (buffer, "_ifend_%d", Lblnum);
+      SNPRINTF(buffer, sizeof(buffer), "_ifend_%d", Lblnum);
       ifEnd = newSymbol (buffer, NestLevel);
     }
 
-  sprintf (buffer, "_iftrue_%d", Lblnum);
+  SNPRINTF (buffer, sizeof(buffer), "_iftrue_%d", Lblnum);
   ifTrue = newSymbol (buffer, NestLevel);
 
   Lblnum++;
@@ -4348,7 +4350,7 @@ createFunction (symbol * name, ast * body)
     name->stack = SPEC_STAK (fetype) = stack;
 
   /* name needs to be mangled */
-  sprintf (name->rname, "%s%s", port->fun_prefix, name->name);
+  SNPRINTF (name->rname, sizeof(name->rname), "%s%s", port->fun_prefix, name->name);
 
   body = resolveSymbols (body);        /* resolve the symbols */
   body = decorateType (body);  /* propagateType & do semantic checks */
index 14cae90251fede641590535c57a82199d24d6830..c4ef6f2f5e4ef2f02c53992e1dcbb2b561904220 100644 (file)
@@ -253,8 +253,8 @@ emitRegularMap (memmap * map, bool addPublics, bool arFlag)
          // create a new "XINIT (CODE)" symbol, that will be emitted later
          newSym=copySymbol (sym);
          SPEC_OCLS(newSym->etype)=xinit;
-         sprintf (newSym->name, "__xinit_%s", sym->name);
-         sprintf (newSym->rname,"__xinit_%s", sym->rname);
+         SNPRINTF (newSym->name, sizeof(newSym->name), "__xinit_%s", sym->name);
+         SNPRINTF (newSym->rname, sizeof(newSym->rname), "__xinit_%s", sym->rname);
          SPEC_CONST(newSym->etype)=1;
          SPEC_STAT(newSym->etype)=1;
          resolveIvalSym(newSym->ival);
index b90d09adf10c5d7881197656cf6109d30a8ed419..62f2ceadfde566e87d1fb8cbff4a5bf56e26a99f 100644 (file)
@@ -200,7 +200,7 @@ void checkConstantRange(sym_link *ltype, value *val, char *msg,
 
 #if 0 // temporary disabled, leaving the warning as a reminder
   if (warnings) {
-    sprintf (message, "for %s %s in %s", 
+    SNPRINTF (message, sizeof(message), "for %s %s in %s", 
             SPEC_USIGN(ltype) ? "unsigned" : "signed",
             nounName(ltype), msg);
     werror (W_CONST_RANGE, message);
@@ -613,9 +613,14 @@ newiTemp (char *s)
   symbol *itmp;
 
   if (s)
-    sprintf (buffer, "%s", s);
+  {
+      SNPRINTF (buffer, sizeof(buffer), "%s", s);
+  }
   else
-    sprintf (buffer, "iTemp%d", iTempNum++);
+  {
+      SNPRINTF (buffer, sizeof(buffer), "iTemp%d", iTempNum++);
+  }
+    
   itmp = newSymbol (buffer, 1);
   strncpyz (itmp->rname, itmp->name, SDCC_NAME_MAX);
   itmp->isitmp = 1;
@@ -636,10 +641,12 @@ newiTempLabel (char *s)
     return itmplbl;
 
   if (s)
-    itmplbl = newSymbol (s, 1);
+    {
+       itmplbl = newSymbol (s, 1);
+    }
   else
     {
-      sprintf (buffer, "iTempLbl%d", iTempLblNum++);
+      SNPRINTF (buffer, sizeof(buffer), "iTempLbl%d", iTempLblNum++);
       itmplbl = newSymbol (buffer, 1);
     }
 
@@ -658,7 +665,7 @@ newiTempPreheaderLabel ()
 {
   symbol *itmplbl;
 
-  sprintf (buffer, "preHeaderLbl%d", iTempLblNum++);
+  SNPRINTF (buffer, sizeof(buffer), "preHeaderLbl%d", iTempLblNum++);
   itmplbl = newSymbol (buffer, 1);
 
   itmplbl->isitmp = 1;
@@ -3148,7 +3155,8 @@ geniCodeJumpTable (operand * cond, value * caseVals, ast * tree)
   /* all integer numbers between the maximum & minimum must */
   /* be present , the maximum value should not exceed 255 */
   min = max = (int) floatFromVal (vch = caseVals);
-  sprintf (buffer, "_case_%d_%d",
+  SNPRINTF (buffer, sizeof(buffer), 
+           "_case_%d_%d",
           tree->values.switchVals.swNum,
           min);
   addSet (&labels, newiTempLabel (buffer));
@@ -3161,7 +3169,8 @@ geniCodeJumpTable (operand * cond, value * caseVals, ast * tree)
     {
       if (((t = (int) floatFromVal (vch)) - max) != 1)
        return 0;
-      sprintf (buffer, "_case_%d_%d",
+      SNPRINTF (buffer, sizeof(buffer), 
+               "_case_%d_%d",
               tree->values.switchVals.swNum,
               t);
       addSet (&labels, newiTempLabel (buffer));
@@ -3177,9 +3186,14 @@ geniCodeJumpTable (operand * cond, value * caseVals, ast * tree)
     return 0;
 
   if (tree->values.switchVals.swDefault)
-    sprintf (buffer, "_default_%d", tree->values.switchVals.swNum);
+    {
+       SNPRINTF (buffer, sizeof(buffer), "_default_%d", tree->values.switchVals.swNum);
+    }
   else
-    sprintf (buffer, "_swBrk_%d", tree->values.switchVals.swNum);
+    {
+       SNPRINTF (buffer, sizeof(buffer), "_swBrk_%d", tree->values.switchVals.swNum);
+    }
+    
 
   falseLabel = newiTempLabel (buffer);
 
@@ -3242,7 +3256,7 @@ geniCodeSwitch (ast * tree,int lvl)
                                        operandFromValue (caseVals),
                                        EQ_OP);
 
-      sprintf (buffer, "_case_%d_%d",
+      SNPRINTF (buffer, sizeof(buffer), "_case_%d_%d",
               tree->values.switchVals.swNum,
               (int) floatFromVal (caseVals));
       trueLabel = newiTempLabel (buffer);
@@ -3256,9 +3270,13 @@ geniCodeSwitch (ast * tree,int lvl)
 
   /* if default is present then goto break else break */
   if (tree->values.switchVals.swDefault)
-    sprintf (buffer, "_default_%d", tree->values.switchVals.swNum);
+    {
+       SNPRINTF (buffer, sizeof(buffer), "_default_%d", tree->values.switchVals.swNum);
+    }
   else
-    sprintf (buffer, "_swBrk_%d", tree->values.switchVals.swNum);
+    {
+       SNPRINTF (buffer, sizeof(buffer), "_swBrk_%d", tree->values.switchVals.swNum);
+    }
 
   falseLabel = newiTempLabel (buffer);
   geniCodeGoto (falseLabel);
index ec961f99adf1b95d5eef55e45356d00e5846a289..9691c0bdce6d5f5f1d879feabddd69f6fc0a3d06 100644 (file)
@@ -116,7 +116,24 @@ mvsprintf(hTab *pvals, const char *pformat, va_list ap)
   /* Recursivly evaluate all the macros in the string */
   _evalMacros(ainto, pvals, pformat, MAX_STRING_LENGTH);
   /* Evaluate all the arguments */
-  vsprintf(atmp, ainto, ap);
+#if defined(HAVE_VSNPRINTF)
+    if (vsnprintf(atmp, MAX_STRING_LENGTH, ainto, ap) >= MAX_STRING_LENGTH)
+    {
+       fprintf(stderr, "Internal error: mvsprintf output truncated.\n");
+    }
+#else    
+    {  
+       int wlen; 
+       
+       wlen = vsprintf(atmp, ainto, ap);
+       
+       if (wlen < 0 || wlen >= MAX_STRING_LENGTH)
+       {
+           wassertl(0, "mvsprintf overflowed.");
+       }
+    }
+#endif    
+    
   /* Recursivly evaluate any macros that were used as arguments */
   _evalMacros(ainto, pvals, atmp, MAX_STRING_LENGTH);
 
index 502c753ad47e9e8c850e981ec1401e43d4359e22..8b41cce77ba3cd4279f6950910878a654f610540 100644 (file)
@@ -695,7 +695,10 @@ tryHandleUnsupportedOpt(char **argv, int *pi)
                         (longOpt && unsupportedOptTable[i].longOpt && !strcmp(unsupportedOptTable[i].longOpt, longOpt))) {
                         // Found an unsupported opt.
                         char buffer[100];
-                        sprintf(buffer, "%s%c%c", longOpt ? longOpt : "", shortOpt ? '-' : ' ', shortOpt ? shortOpt : ' ');
+                        SNPRINTF(buffer, sizeof(buffer), 
+                                "%s%c%c", 
+                                longOpt ? longOpt : "", 
+                                shortOpt ? '-' : ' ', shortOpt ? shortOpt : ' ');
                         werror (W_UNSUPP_OPTION, buffer, unsupportedOptTable[i].message);
                         return 1;
                     }
@@ -1135,7 +1138,7 @@ parseCmdLine (int argc, char **argv)
                if (sOpt == 'Y')
                  sOpt = 'I';
 
-               sprintf (buffer, "-%c%s", sOpt, rest);
+               SNPRINTF (buffer, sizeof(buffer), "-%c%s", sOpt, rest);
                addToList (preArgv, buffer);
              }
              break;
@@ -1238,7 +1241,8 @@ parseCmdLine (int argc, char **argv)
   /* if debug option is set the open the cdbFile */
   if (options.debug && fullSrcFileName)
     {
-      sprintf (scratchFileName, "%s.adb", dstFileName); //JCF: Nov 30, 2002
+      SNPRINTF (scratchFileName, sizeof(scratchFileName), 
+               "%s.adb", dstFileName); //JCF: Nov 30, 2002
       if ((cdbFile = fopen (scratchFileName, "w")) == NULL)
        werror (E_FILE_OPEN_ERR, scratchFileName);
       else
@@ -1261,7 +1265,8 @@ linkEdit (char **envp)
   int i, system_ret;
 
   /* first we need to create the <filename>.lnk file */
-  sprintf (scratchFileName, "%s.lnk", dstFileName);
+  SNPRINTF (scratchFileName, sizeof(scratchFileName), 
+           "%s.lnk", dstFileName);
   if (!(lnkfile = fopen (scratchFileName, "w")))
     {
       werror (E_FILE_OPEN_ERR, scratchFileName);
@@ -1393,22 +1398,24 @@ linkEdit (char **envp)
   /* -o option overrides default name? */
   if (fullDstFileName)
     {
-      strncpyz (scratchFileName, fullDstFileName, PATH_MAX);
+      strncpyz (scratchFileName, fullDstFileName, sizeof(scratchFileName));
     }
   else
     {
       /* the linked file gets the name of the first modul */
       if (fullSrcFileName)
         {
-          strncpyz (scratchFileName, dstFileName, PATH_MAX);
+          strncpyz (scratchFileName, dstFileName, sizeof(scratchFileName));
         }
       else
         {
-          strncpyz (scratchFileName, relFiles[0], PATH_MAX);
+          strncpyz (scratchFileName, relFiles[0], sizeof(scratchFileName));
           /* strip ".rel" extension */
           *strrchr (scratchFileName, '.') = '\0';
         }
-      strncatz (scratchFileName, options.out_fmt ? ".S19" : ".ihx", PATH_MAX);
+      strncatz (scratchFileName, 
+               options.out_fmt ? ".S19" : ".ihx",
+               sizeof(scratchFileName));
     }
 
   if (port->linker.cmd)
@@ -1431,12 +1438,12 @@ linkEdit (char **envp)
       /* the linked file gets the name of the first modul */
       if (fullSrcFileName)
         {
-          strncpyz (scratchFileName, dstFileName, PATH_MAX);
+          strncpyz (scratchFileName, dstFileName, sizeof(scratchFileName));
           p = strlen (scratchFileName) + scratchFileName;
         }
       else
         {
-          strncpyz (scratchFileName, relFiles[0], PATH_MAX);
+          strncpyz (scratchFileName, relFiles[0], sizeof(scratchFileName));
           /* strip "rel" extension */
           p = strrchr (scratchFileName, '.');
          if (p)
@@ -1446,7 +1453,9 @@ linkEdit (char **envp)
            }
            
         }
-      strncatz (scratchFileName, options.out_fmt ? "S19" : "ihx", PATH_MAX);
+      strncatz (scratchFileName,
+               options.out_fmt ? "S19" : "ihx",
+               sizeof(scratchFileName));
       rename (scratchFileName, fullDstFileName);
 
       q = strrchr (fullDstFileName, '.');
@@ -1463,14 +1472,14 @@ linkEdit (char **envp)
         }
                
       *p = 0;  
-      strncatz (scratchFileName, "map", PATH_MAX);
+      strncatz (scratchFileName, "map", sizeof(scratchFileName));
       *q = 0;
-      strncatz(fullDstFileName, "map", PATH_MAX);
+      strncatz(fullDstFileName, "map", fullDstFileNameLen);
       rename (scratchFileName, fullDstFileName);
       *p = 0;  
-      strncatz (scratchFileName, "mem", PATH_MAX);
+      strncatz (scratchFileName, "mem", sizeof(scratchFileName));
       *q = 0;
-      strncatz(fullDstFileName, "mem", PATH_MAX);      
+      strncatz(fullDstFileName, "mem", fullDstFileNameLen);    
       rename (scratchFileName, fullDstFileName);
     }
   if (system_ret)
@@ -1489,7 +1498,7 @@ assemble (char **envp)
 
     /* -o option overrides default name? */
     if (options.cc_only && fullDstFileName) {
-        strncpyz (scratchFileName, fullDstFileName, PATH_MAX);
+        strncpyz (scratchFileName, fullDstFileName, sizeof(scratchFileName));
     } else {
         /* the assembled file gets the name of the first modul */
         strncpyz (scratchFileName, dstFileName, PATH_MAX);
@@ -1504,7 +1513,7 @@ assemble (char **envp)
                      options.debug ? port->assembler.debug_opts : port->assembler.plain_opts,
                      asmOptions);
     } else {
-       buildCmdLine2 (buffer, port->assembler.mcmd, PATH_MAX);
+       buildCmdLine2 (buffer, port->assembler.mcmd, sizeof(buffer));
     }
 
     if (my_system (buffer)) {
@@ -1516,8 +1525,10 @@ assemble (char **envp)
     /* TODO: most assembler don't have a -o parameter */
     /* -o option overrides default name? */
     if (options.cc_only && fullDstFileName) {
-        strncpyz (scratchFileName, dstFileName, PATH_MAX);
-        strncatz (scratchFileName, port->linker.rel_ext, PATH_MAX);
+        strncpyz (scratchFileName, dstFileName, sizeof(scratchFileName));
+        strncatz (scratchFileName, 
+                 port->linker.rel_ext,
+                 sizeof(scratchFileName));
         rename (scratchFileName, fullDstFileName);
     }
 }
@@ -1635,8 +1646,8 @@ _setPaths (const char *pprefix)
       where expected.  If so, set.
   */
   getPathDifference (buffer, PREFIX, SDCC_INCLUDE_DIR);
-  strncpyz (scratchFileName, pprefix, PATH_MAX);
-  strncatz (scratchFileName, buffer, PATH_MAX);
+  strncpyz (scratchFileName, pprefix, sizeof(scratchFileName));
+  strncatz (scratchFileName, buffer, sizeof(scratchFileName));
 
   if (pathExists (scratchFileName))
     {
@@ -1648,8 +1659,8 @@ _setPaths (const char *pprefix)
     }
 
   getPathDifference (buffer, PREFIX, SDCC_LIB_DIR);
-  strncpyz (scratchFileName, pprefix, PATH_MAX);
-  strncatz (scratchFileName, buffer, PATH_MAX);
+  strncpyz (scratchFileName, pprefix, sizeof(scratchFileName));
+  strncatz (scratchFileName, buffer, sizeof(scratchFileName));
 
   if (pathExists (scratchFileName))
     {
@@ -1693,7 +1704,7 @@ _discoverPaths (const char *argv0)
    */
   if (strchr (argv0, DIR_SEPARATOR_CHAR))
     {
-      strncpyz (scratchFileName, argv0, PATH_MAX);
+      strncpyz (scratchFileName, argv0, sizeof(scratchFileName));
       *strrchr (scratchFileName, DIR_SEPARATOR_CHAR) = '\0';
       setMainValue ("bindir", scratchFileName);
       ExePathList[0] = Safe_strdup (scratchFileName);
@@ -1701,7 +1712,7 @@ _discoverPaths (const char *argv0)
   else if (getenv (SDCCDIR_NAME) != NULL)
     {
       getPathDifference (buffer, PREFIX, BINDIR);
-      strncpyz (scratchFileName, getenv (SDCCDIR_NAME), PATH_MAX);
+      strncpyz (scratchFileName, getenv (SDCCDIR_NAME), sizeof(scratchFileName));
       strncatz (scratchFileName, buffer, PATH_MAX);
       setMainValue ("bindir", scratchFileName);
       ExePathList[0] = Safe_strdup (scratchFileName);
index 36edd0f8f262693f22d07754325ddbd9301ee560..bacebcfb2ffef64635c1e99b3db5db393ce1a006 100644 (file)
@@ -303,7 +303,8 @@ allocGlobal (symbol * sym)
 
   /* symbol name is internal name  */
   if (!sym->level)             /* local statics can come here */
-    sprintf (sym->rname, "%s%s", port->fun_prefix, sym->name);
+    SNPRINTF (sym->rname, sizeof(sym->rname), 
+             "%s%s", port->fun_prefix, sym->name);
 
   /* add it to the operandKey reset */
   addSet (&operKeyReset, sym);
@@ -518,7 +519,8 @@ allocParms (value * val)
       else
        { /* allocate them in the automatic space */
          /* generate a unique name  */
-         sprintf (lval->sym->rname, "%s%s_PARM_%d", port->fun_prefix, currFunc->name, pNum);
+         SNPRINTF (lval->sym->rname, sizeof(lval->sym->rname),
+                   "%s%s_PARM_%d", port->fun_prefix, currFunc->name, pNum);
          strncpyz (lval->name, lval->sym->rname, sizeof(lval->name));
          
          /* if declared in external storage */
@@ -600,9 +602,10 @@ allocLocal (symbol * sym)
 {
 
   /* generate an unique name */
-  sprintf (sym->rname, "%s%s_%s_%d_%d",
-          port->fun_prefix,
-          currFunc->name, sym->name, sym->level, sym->block);
+  SNPRINTF (sym->rname, sizeof(sym->rname), 
+           "%s%s_%s_%d_%d",
+           port->fun_prefix,
+           currFunc->name, sym->name, sym->level, sym->block);
 
   sym->islocal = 1;
   sym->localof = currFunc;
index 17ac0d121115b9e28e45e6939ad289f7c4e39eb5..a928f3a0e1084f1b0b0a1a5add4c7d6660b5f40a 100644 (file)
@@ -66,7 +66,7 @@ pcDistance (lineNode * cpos, char *lbl, bool back)
   char buff[MAX_PATTERN_LEN];
   int dist = 0;
 
-  sprintf (buff, "%s:", lbl);
+  SNPRINTF (buff, sizeof(buff), "%s:", lbl);
   while (pl)
     {
 
@@ -815,8 +815,7 @@ bindVar (int key, char **s, hTab ** vtab)
   *s = vvx;
   *vv = '\0';
   /* got value */
-  vvx = traceAlloc (&_G.values, Safe_alloc(strlen (vval) + 1));
-  strcpy (vvx, vval);
+  vvx = traceAlloc (&_G.values, Safe_strdup(vval));
 
   hTabAddItem (vtab, key, vvx);
 }
@@ -1310,7 +1309,7 @@ readFileIntoBuffer (char *fname)
          if (rs)
            {
              rs = Safe_realloc (rs, strlen (rs) + strlen (lb) + 1);
-             strcat (rs, lb);
+             strncatz (rs, lb,  strlen (rs) + strlen (lb) + 1);
            }
          else
            {
@@ -1328,7 +1327,7 @@ readFileIntoBuffer (char *fname)
       if (rs)
        {
          rs = Safe_realloc (rs, strlen (rs) + strlen (lb) + 1);
-         strcat (rs, lb);
+         strncatz (rs, lb, strlen (rs) + strlen (lb) + 1);
        }
       else
        {
index ed9c4fb5bfef072be5a6be12c3b4123861881872..588935e69a086b233399593129bd9e0eac07c9d0 100644 (file)
@@ -138,7 +138,7 @@ addSym (bucket ** stab,
   bp->sym = sym;               /* update the symbol pointer  */
   bp->level = level;           /* update the nest level      */
   bp->block = block;
-  strcpy (bp->name, sname);    /* copy the name into place */
+  strncpyz (bp->name, sname, sizeof(bp->name));        /* copy the name into place */
 
   /* if this is the first entry */
   if (stab[i] == NULL)
@@ -287,7 +287,7 @@ newSymbol (char *name, int scope)
 
   sym = Safe_alloc ( sizeof (symbol));
 
-  strcpy (sym->name, name);    /* copy the name    */
+  strncpyz (sym->name, name, sizeof(sym->name));       /* copy the name */
   sym->level = scope;          /* set the level    */
   sym->block = currBlockno;
   sym->lineDef = yylineno;     /* set the line number */
@@ -317,7 +317,7 @@ newStruct (char *tag)
 
   s = Safe_alloc ( sizeof (structdef));
 
-  strcpy (s->tag, tag);                /* copy the tag            */
+  strncpyz (s->tag, tag, sizeof(s->tag));              /* copy the tag */
   return s;
 }
 
@@ -671,7 +671,7 @@ genSymName (int level)
   static int gCount = 0;
   static char gname[SDCC_NAME_MAX + 1];
 
-  sprintf (gname, "__%04d%04d", level, gCount++);
+  SNPRINTF (gname, sizeof(gname), "__%04d%04d", level, gCount++);
   return gname;
 }
 
@@ -1086,7 +1086,7 @@ compStructSize (int su, structdef * sdef)
     while (loop) {
 
        /* create the internal name for this variable */
-       sprintf (loop->rname, "_%s", loop->name);
+       SNPRINTF (loop->rname, sizeof(loop->rname), "_%s", loop->name);
        loop->offset = (su == UNION ? sum = 0 : sum);
        SPEC_VOLATILE (loop->etype) |= (su == UNION ? 1 : 0);
 
@@ -1708,13 +1708,13 @@ checkFunction (symbol * sym, symbol *csym)
       // this can happen for reentrant functions
       werror(E_PARAM_NAME_OMITTED, sym->name, argCnt);
       // the show must go on: synthesize a name and symbol
-      sprintf (acargs->name, "_%s_PARM_%d", sym->name, argCnt);
+      SNPRINTF (acargs->name, sizeof(acargs->name), "_%s_PARM_%d", sym->name, argCnt);
       acargs->sym = newSymbol (acargs->name, 1);
       SPEC_OCLS (acargs->etype) = istack;
       acargs->sym->type = copyLinkChain (acargs->type);
       acargs->sym->etype = getSpec (acargs->sym->type);
       acargs->sym->_isparm = 1;
-      strcpy (acargs->sym->rname, acargs->name);
+      strncpyz (acargs->sym->rname, acargs->name, sizeof(acargs->sym->rname));
     } else if (strcmp(acargs->sym->name, acargs->sym->rname)==0) { 
       // synthesized name
       werror(E_PARAM_NAME_OMITTED, sym->name, argCnt);
@@ -1897,13 +1897,14 @@ processFuncArgs (symbol * func)
       /* synthesize a variable name */
       if (!val->sym)
        {
-         sprintf (val->name, "_%s_PARM_%d", func->name, pNum++);
+         SNPRINTF (val->name, sizeof(val->name), 
+                   "_%s_PARM_%d", func->name, pNum++);
          val->sym = newSymbol (val->name, 1);
          SPEC_OCLS (val->etype) = port->mem.default_local_map;
          val->sym->type = copyLinkChain (val->type);
          val->sym->etype = getSpec (val->sym->type);
          val->sym->_isparm = 1;
-         strcpy (val->sym->rname, val->name);
+         strncpyz (val->sym->rname, val->name, sizeof(val->sym->rname));
          SPEC_STAT (val->etype) = SPEC_STAT (val->sym->etype) =
            SPEC_STAT (func->etype);
          addSymChain (val->sym);
@@ -1912,8 +1913,8 @@ processFuncArgs (symbol * func)
       else                     /* symbol name given create synth name */
        {
 
-         sprintf (val->name, "_%s_PARM_%d", func->name, pNum++);
-         strcpy (val->sym->rname, val->name);
+         SNPRINTF (val->name, sizeof(val->name), "_%s_PARM_%d", func->name, pNum++);
+         strncpyz (val->sym->rname, val->name, sizeof(val->sym->rname));
          val->sym->_isparm = 1;
          SPEC_OCLS (val->etype) = SPEC_OCLS (val->sym->etype) =
            (options.model != MODEL_SMALL ? xdata : data);
@@ -2551,12 +2552,12 @@ initCSupport ()
            {
              if (tofrom)
                {
-                 sprintf (buffer, "__fs2%s%s", ssu[su], sbwd[bwd]);
+                 SNPRINTF (buffer, sizeof(buffer), "__fs2%s%s", ssu[su], sbwd[bwd]);
                  __conv[tofrom][bwd][su] = funcOfType (buffer, __multypes[bwd][su], floatType, 1, options.float_rent);
                }
              else
                {
-                 sprintf (buffer, "__%s%s2fs", ssu[su], sbwd[bwd]);
+                 SNPRINTF (buffer, sizeof(buffer), "__%s%s2fs", ssu[su], sbwd[bwd]);
                  __conv[tofrom][bwd][su] = funcOfType (buffer, floatType, __multypes[bwd][su], 1, options.float_rent);
                }
            }
@@ -2569,7 +2570,8 @@ initCSupport ()
        {
          for (su = 0; su < 2; su++)
            {
-             sprintf (buffer, "_%s%s%s",
+             SNPRINTF (buffer, sizeof(buffer), 
+                       "_%s%s%s",
                       smuldivmod[muldivmod],
                       ssu[su],
                       sbwd[bwd]);
@@ -2585,7 +2587,8 @@ initCSupport ()
        {
          for (su = 0; su < 2; su++)
            {
-             sprintf (buffer, "_%s%s%s",
+             SNPRINTF (buffer, sizeof(buffer), 
+                       "_%s%s%s",
                       srlrr[rlrr],
                       ssu[su],
                       sbwd[bwd]);
index 065d2834c2693d0fcc01c3f4fad25e201454744c..dd42706c221bce10e16725b0a5224b49f0c8623a 100644 (file)
@@ -382,6 +382,7 @@ size_t SDCCsnprintf(char *dst, size_t n, const char *fmt, ...)
     {
        fprintf(stderr, "internal error: sprintf truncated.\n");
     }
+    
     return len;
 }
 
index 67bb1a44fa7c7c83d99e4359f779eaffa52cf46b..b6988f5e3ad716114c0cc04bd5dad6c7513a2e57 100644 (file)
@@ -304,10 +304,13 @@ symbolVal (symbol * sym)
     }
 
   if (*sym->rname)
-    sprintf (val->name, "%s", sym->rname);
+    {
+       SNPRINTF (val->name, sizeof(val->name), "%s", sym->rname);
+    }
   else
-    sprintf (val->name, "_%s", sym->name);
-
+    {
+       SNPRINTF (val->name, sizeof(val->name), "_%s", sym->name);
+    }
 
   return val;
 }
@@ -377,11 +380,11 @@ valueFromLit (double lit)
 
   if ((((long) lit) - lit) == 0)
     {
-      sprintf (buffer, "%ld", (long) lit);
+      SNPRINTF (buffer, sizeof(buffer), "%ld", (long) lit);
       return constVal (buffer);
     }
 
-  sprintf (buffer, "%f", lit);
+  SNPRINTF (buffer, sizeof(buffer), "%f", lit);
   return constFloatVal (buffer);
 }
 
@@ -1528,11 +1531,15 @@ valForArray (ast * arrExpr)
 
   val = newValue ();
   if (!lval)
-    sprintf (buffer, "%s", AST_SYMBOL (arrExpr->left)->rname);
+    {
+       SNPRINTF (buffer, sizeof(buffer), "%s", AST_SYMBOL (arrExpr->left)->rname);
+    }
   else
-    sprintf (buffer, "%s", lval->name);
+    {
+       SNPRINTF (buffer, sizeof(buffer), "%s", lval->name);
+    }
 
-  sprintf (val->name, "(%s + %d)", buffer,
+  SNPRINTF (val->name, sizeof(val->name), "(%s + %d)", buffer,
           (int) AST_LIT_VALUE (arrExpr->right) * size);
 
   val->type = newLink ();
@@ -1595,11 +1602,15 @@ valForStructElem (ast * structT, ast * elemT)
 
   val = newValue ();
   if (!lval)
-    sprintf (buffer, "%s", AST_SYMBOL (structT)->rname);
+    {
+       SNPRINTF(buffer, sizeof(buffer), "%s", AST_SYMBOL (structT)->rname);
+    }
   else
-    sprintf (buffer, "%s", lval->name);
+    {
+       SNPRINTF (buffer, sizeof(buffer), "%s", lval->name);
+    }
 
-  sprintf (val->name, "(%s + %d)", buffer,
+  SNPRINTF (val->name, sizeof(val->name), "(%s + %d)", buffer,
           (int) sym->offset);
 
   val->type = newLink ();
@@ -1639,7 +1650,7 @@ valForCastAggr (ast * aexpr, sym_link * type, ast * cnst, int op)
 
   val = newValue ();
 
-  sprintf (val->name, "(%s %c %d)",
+  SNPRINTF (val->name, sizeof(val->name), "(%s %c %d)",
           AST_SYMBOL (aexpr)->rname, op,
           getSize (type->next) * (int) AST_LIT_VALUE (cnst));
 
@@ -1662,7 +1673,7 @@ valForCastArr (ast * aexpr, sym_link * type)
 
   val = newValue ();
 
-  sprintf (val->name, "(%s)",
+  SNPRINTF (val->name, sizeof(val->name), "(%s)",
           AST_SYMBOL (aexpr)->rname);
 
   val->type = type;
index 5b84d12d17ca6df790442a26cfd0a376f572c25b..5de195461cc1f424ddec2d0c42eba71a9ca918fe 100644 (file)
@@ -234,12 +234,12 @@ check_token (const char *sz)
   /* check if it is in the typedef table */
   if (findSym (TypedefTab, NULL, sz))
     {
-      strcpy (yylval.yychar, sz);
+      strncpyz (yylval.yychar, sz, sizeof(yylval.yychar));
       return TYPE_NAME;
     }
   else
     {
-      strcpy (yylval.yychar, sz);
+      strncpyz (yylval.yychar, sz, sizeof(yylval.yychar));
       return IDENTIFIER;
     }
 }
@@ -916,7 +916,7 @@ int
 altlex_testparse (const char *input)
 {
   /* Fiddle with the read-ahead buffer to insert ourselves */
-  strcpy (linebuf, input);
+  strncpyz (linebuf, input, sizeof(linebuf));
   linelen = strlen (linebuf) + 1;
   linepos = 0;
 
index 0d1779994b8127903de51ec5b3df99e945a93163..2c3d52c31f24f81a0859afedf8c8885161eb5ce3 100644 (file)
@@ -484,7 +484,8 @@ createStackSpil (symbol * sym)
      we need to allocate this on the stack : this is really a
      hack!! but cannot think of anything better at this time */
 
-  if (sprintf (slocBuffer, "sloc%d", _G.slocNum++) >= sizeof (slocBuffer))
+  if (SNPRINTF (slocBuffer, sizeof(slocBuffer), 
+               "sloc%d", _G.slocNum++) >= sizeof (slocBuffer))
     {
       fprintf (stderr, "***Internal error: slocBuffer overflowed: %s:%d\n",
               __FILE__, __LINE__);
@@ -643,9 +644,10 @@ selectSpil (iCode * ic, eBBlock * ebp, symbol * forSym)
   if ((selectS = liveRangesWith (lrcs, directSpilLoc, ebp, ic)))
     {
       sym = leastUsedLR (selectS);
-      strcpy (sym->rname, (sym->usl.spillLoc->rname[0] ?
-                          sym->usl.spillLoc->rname :
-                          sym->usl.spillLoc->name));
+      strncpyz (sym->rname,
+               sym->usl.spillLoc->rname[0] ?
+                  sym->usl.spillLoc->rname : sym->usl.spillLoc->name,
+               sizeof(sym->rname));
       sym->spildir = 1;
       /* mark it as allocation required */
       sym->usl.spillLoc->allocreq++;
@@ -1683,14 +1685,17 @@ rematStr (symbol * sym)
   char *s = buffer;
   iCode *ic = sym->rematiCode;
 
+  *s = 0;
+    
   while (1)
     {
 
       /* if plus or minus print the right hand side */
       if (ic->op == '+' || ic->op == '-')
        {
-         sprintf (s, "0x%04x %c ", (int) operandLitValue (IC_RIGHT (ic)),
-                  ic->op);
+         SNPRINTF (s, sizeof(buffer) - strlen(buffer), 
+                   "0x%04x %c ", (int) operandLitValue (IC_RIGHT (ic)),
+                   ic->op);
          s += strlen (s);
          ic = OP_SYMBOL (IC_LEFT (ic))->rematiCode;
          continue;
@@ -1701,7 +1706,8 @@ rematStr (symbol * sym)
          continue;
       }
       /* we reached the end */
-      sprintf (s, "%s", OP_SYMBOL (IC_LEFT (ic))->rname);
+      SNPRINTF (s, sizeof(buffer) - strlen(buffer), 
+               "%s", OP_SYMBOL (IC_LEFT (ic))->rname);
       break;
     }
 
@@ -1764,7 +1770,7 @@ regTypeNum ()
                  symbol *psym = newSymbol (rematStr (OP_SYMBOL (IC_LEFT (ic))), 1);
                  psym->type = sym->type;
                  psym->etype = sym->etype;
-                 strcpy (psym->rname, psym->name);
+                 strncpyz (psym->rname, psym->name, sizeof(psym->rname));
                  sym->isspilt = 1;
                  sym->usl.spillLoc = psym;
                  continue;