From 817beb07e406ae505fb0b1354d42025a1bfd14ce Mon Sep 17 00:00:00 2001 From: kvigor Date: Fri, 21 Feb 2003 23:38:38 +0000 Subject: [PATCH] More strcpy() strcat() sprintf() squashing git-svn-id: https://sdcc.svn.sourceforge.net/svnroot/sdcc/trunk/sdcc@2287 4a8a32a2-be11-0410-ad9d-d568d2c75423 --- src/SDCC.y | 30 +++++++++++++---------- src/SDCCBBlock.c | 2 +- src/SDCCast.c | 28 +++++++++++---------- src/SDCCglue.c | 4 +-- src/SDCCicode.c | 44 +++++++++++++++++++++++---------- src/SDCCmacro.c | 19 ++++++++++++++- src/SDCCmain.c | 61 +++++++++++++++++++++++++++------------------- src/SDCCmem.c | 13 ++++++---- src/SDCCpeeph.c | 9 +++---- src/SDCCsymt.c | 33 +++++++++++++------------ src/SDCCutil.c | 1 + src/SDCCval.c | 37 ++++++++++++++++++---------- src/altlex.c | 6 ++--- src/ds390/ralloc.c | 22 +++++++++++------ 14 files changed, 192 insertions(+), 117 deletions(-) diff --git a/src/SDCC.y b/src/SDCC.y index 52da8209..ab27ae5a 100644 --- a/src/SDCC.y +++ b/src/SDCC.y @@ -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++); $$ = newSymbol(lbuff,NestLevel); /* put label in the break stack */ STACK_PUSH(breakStack,$$); @@ -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)); } ; diff --git a/src/SDCCBBlock.c b/src/SDCCBBlock.c index d1f1ebef..559d7341 100644 --- a/src/SDCCBBlock.c +++ b/src/SDCCBBlock.c @@ -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++; } diff --git a/src/SDCCast.c b/src/SDCCast.c index 2a19a3df..fe878f9f 100644 --- a/src/SDCCast.c +++ b/src/SDCCast.c @@ -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 */ diff --git a/src/SDCCglue.c b/src/SDCCglue.c index 14cae902..c4ef6f2f 100644 --- a/src/SDCCglue.c +++ b/src/SDCCglue.c @@ -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); diff --git a/src/SDCCicode.c b/src/SDCCicode.c index b90d09ad..62f2cead 100644 --- a/src/SDCCicode.c +++ b/src/SDCCicode.c @@ -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); diff --git a/src/SDCCmacro.c b/src/SDCCmacro.c index ec961f99..9691c0bd 100644 --- a/src/SDCCmacro.c +++ b/src/SDCCmacro.c @@ -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); diff --git a/src/SDCCmain.c b/src/SDCCmain.c index 502c753a..8b41cce7 100644 --- a/src/SDCCmain.c +++ b/src/SDCCmain.c @@ -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 .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); diff --git a/src/SDCCmem.c b/src/SDCCmem.c index 36edd0f8..bacebcfb 100644 --- a/src/SDCCmem.c +++ b/src/SDCCmem.c @@ -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; diff --git a/src/SDCCpeeph.c b/src/SDCCpeeph.c index 17ac0d12..a928f3a0 100644 --- a/src/SDCCpeeph.c +++ b/src/SDCCpeeph.c @@ -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 { diff --git a/src/SDCCsymt.c b/src/SDCCsymt.c index ed9c4fb5..588935e6 100644 --- a/src/SDCCsymt.c +++ b/src/SDCCsymt.c @@ -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]); diff --git a/src/SDCCutil.c b/src/SDCCutil.c index 065d2834..dd42706c 100644 --- a/src/SDCCutil.c +++ b/src/SDCCutil.c @@ -382,6 +382,7 @@ size_t SDCCsnprintf(char *dst, size_t n, const char *fmt, ...) { fprintf(stderr, "internal error: sprintf truncated.\n"); } + return len; } diff --git a/src/SDCCval.c b/src/SDCCval.c index 67bb1a44..b6988f5e 100644 --- a/src/SDCCval.c +++ b/src/SDCCval.c @@ -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; diff --git a/src/altlex.c b/src/altlex.c index 5b84d12d..5de19546 100644 --- a/src/altlex.c +++ b/src/altlex.c @@ -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; diff --git a/src/ds390/ralloc.c b/src/ds390/ralloc.c index 0d177999..2c3d52c3 100644 --- a/src/ds390/ralloc.c +++ b/src/ds390/ralloc.c @@ -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; -- 2.30.2