- fixed some potential bugs introduced when replacing strncpy and

strncat by strlcpy and strlcat
This commit is contained in:
zolliker
2010-04-14 08:46:17 +00:00
parent 1c558e7f6c
commit 9690db1c3d
10 changed files with 20 additions and 20 deletions

4
Dbg.c
View File

@ -410,7 +410,7 @@ char *argv[];
if (compress) { if (compress) {
/* this copies from our static buf to printify's static buf */ /* this copies from our static buf to printify's static buf */
/* and back to our static buf */ /* and back to our static buf */
strlcpy(buf, printify(buf), buf_width); strncpy(buf, printify(buf), buf_width);
} }
/* usually but not always right, but assume truncation if buffer is */ /* usually but not always right, but assume truncation if buffer is */
@ -684,7 +684,7 @@ char **argv;
if (argc == 1) if (argc == 1)
argv[1] = "1"; argv[1] = "1";
strlcpy(viewFrameName, argv[1], FRAMENAMELEN); strncpy(viewFrameName, argv[1], FRAMENAMELEN);
return TCL_RETURN; return TCL_RETURN;
} }

View File

@ -87,7 +87,7 @@ void WriteToCommandLogId(char *prompt, int id, char *text)
pCopy = malloc(l + 1); pCopy = malloc(l + 1);
if (pCopy == NULL) if (pCopy == NULL)
return; return;
strlcpy(pCopy, text, l); strncpy(pCopy, text, l); /* strlcpy is not correct here */
pCopy[l] = '\0'; pCopy[l] = '\0';
if (prompt == cmdPrompt && iCompact) { if (prompt == cmdPrompt && iCompact) {
pPtr = strstr(pCopy, "fulltransact "); pPtr = strstr(pCopy, "fulltransact ");

View File

@ -210,7 +210,7 @@ int exeBufProcess(pExeBuf self, SicsInterp * pSics,
} }
l = ende - cmd; l = ende - cmd;
if (l < sizeof cmdName) { if (l < sizeof cmdName) {
strlcpy(cmdName, cmd, l); strncpy(cmdName, cmd, l);
cmdName[l] = '\0'; cmdName[l] = '\0';
if (FindCommand(pSics, cmdName) != NULL) { if (FindCommand(pSics, cmdName) != NULL) {
/* print only SICS commands */ /* print only SICS commands */

View File

@ -201,15 +201,15 @@ static int makeExePath(pExeMan self, SConnection * pCon, int argc,
* do nothing to absolute path * do nothing to absolute path
*/ */
if (argv[2][0] == '/') { if (argv[2][0] == '/') {
strlcat(buffer, argv[2], 511 - strlen(buffer)); strlcat(buffer, argv[2], sizeof buffer);
SCWrite(pCon, buffer, eValue); SCWrite(pCon, buffer, eValue);
return 1; return 1;
} }
pPtr = self->batchPath; pPtr = self->batchPath;
pPtr = stptok(pPtr, pPath, 131, ":"); pPtr = stptok(pPtr, pPath, 131, ":");
strlcat(buffer, pPath, 511 - strlen(buffer)); strlcat(buffer, pPath, sizeof buffer);
strlcat(buffer, "/", 511 - strlen(buffer)); strlcat(buffer, "/", sizeof buffer);
strlcat(buffer, argv[2], 511 - strlen(buffer)); strlcat(buffer, argv[2], sizeof buffer);
SCWrite(pCon, buffer, eValue); SCWrite(pCon, buffer, eValue);
return 1; return 1;

6
help.c
View File

@ -43,9 +43,9 @@ static FILE *findHelpFile(char *name)
pPtr = helpDirs; pPtr = helpDirs;
while ((pPtr = stptok(pPtr, dir, 131, PATHSEP)) != NULL) { while ((pPtr = stptok(pPtr, dir, 131, PATHSEP)) != NULL) {
strcpy(pBueffel, dir); strlcpy(pBueffel, dir, sizeof pBueffel);
strcat(pBueffel, DIRSEP); strlcat(pBueffel, DIRSEP, sizeof pBueffel);
strlcat(pBueffel, name, (254 - strlen(pBueffel))); strlcat(pBueffel, name, sizeof pBueffel);
fd = fopen(pBueffel, "r"); fd = fopen(pBueffel, "r");
if (fd != NULL) { if (fd != NULL) {
return fd; return fd;

View File

@ -118,7 +118,7 @@ IPair *IFReadConfigFile(FILE * fd)
continue; continue;
iLen = pPos - pBueffel; iLen = pPos - pBueffel;
strlcpy(pName, pBueffel, iLen); strncpy(pName, pBueffel, iLen);
pName[iLen] = '\0'; pName[iLen] = '\0';
strlcpy(pValue, (pPos + 1),131); strlcpy(pValue, (pPos + 1),131);
RemoveWhiteSpace(pName); RemoveWhiteSpace(pName);

View File

@ -502,7 +502,7 @@ static int LogReader(SConnection * pCon, SicsInterp * pSics, void *pData,
if (!inRange) { if (!inRange) {
if (t < startim) { if (t < startim) {
lastval[0] = '\0'; lastval[0] = '\0';
strlcat(lastval, val, sizeof lastval - 1); strncat(lastval, val, sizeof lastval - 1);
lastt = t; lastt = t;
} else { } else {
inRange = 1; inRange = 1;

8
scan.c
View File

@ -1396,16 +1396,16 @@ static void PrintScanVars(pScanData self, char *scanname,
assert(pCon); assert(pCon);
assert(self); assert(self);
snprintf(pBueffel, 1023, "%s.scanvars = { ", scanname); snprintf(pBueffel, sizeof pBueffel, "%s.scanvars = { ", scanname);
for (i = 0; i < self->iScanVar; i++) { for (i = 0; i < self->iScanVar; i++) {
DynarGet(self->pScanVar, i, &pPtr); DynarGet(self->pScanVar, i, &pPtr);
pVar = (pVarEntry) pPtr; pVar = (pVarEntry) pPtr;
if (pVar != NULL) { if (pVar != NULL) {
strlcat(pBueffel, ScanVarName(pVar), 1023 - strlen(pBueffel)); strlcat(pBueffel, ScanVarName(pVar), sizeof pBueffel);
strcat(pBueffel, " "); strlcat(pBueffel, " ", sizeof pBueffel);
} }
} }
strcat(pBueffel, "}"); strlcat(pBueffel, "}", sizeof pBueffel);
SCWrite(pCon, pBueffel, eValue); SCWrite(pCon, pBueffel, eValue);
} }

View File

@ -215,7 +215,7 @@ tryagain:
return 0; return 0;
} else { } else {
if (test > 0) { if (test > 0) {
strlcat(pBueffel, pRead, 2047 - strlen(pBueffel)); strlcat(pBueffel, pRead, sizeof pBueffel);
} }
} }
if (strstr(pBueffel, "TRANSACTIONFINISHED") != NULL) { if (strstr(pBueffel, "TRANSACTIONFINISHED") != NULL) {

View File

@ -99,7 +99,7 @@ static int TclRun(void *self, float fVal)
return HWFault; return HWFault;
} }
snprintf(num, 79, "%f", fVal); snprintf(num, 79, "%f", fVal);
strlcat(tclCommand, num, 1023 - strlen(tclCommand)); strlcat(tclCommand, num, sizeof tclCommand);
status = Tcl_Eval(pServ->pSics->pTcl, tclCommand); status = Tcl_Eval(pServ->pSics->pTcl, tclCommand);
result = Tcl_GetStringResult(pServ->pSics->pTcl); result = Tcl_GetStringResult(pServ->pSics->pTcl);
@ -199,7 +199,7 @@ static int TclFix(void *self, int iError, float fNew)
return HWFault; return HWFault;
} }
snprintf(num, 79, "%d %f", pDriv->errorCode, fNew); snprintf(num, 79, "%d %f", pDriv->errorCode, fNew);
strlcat(tclCommand, num, 1023 - strlen(tclCommand)); strlcat(tclCommand, num, sizeof tclCommand);
status = Tcl_Eval(pServ->pSics->pTcl, tclCommand); status = Tcl_Eval(pServ->pSics->pTcl, tclCommand);
result = Tcl_GetStringResult(pServ->pSics->pTcl); result = Tcl_GetStringResult(pServ->pSics->pTcl);