* Don't "Guarantee nul-termination" after snprintf as snprintf does that itself already (per the C99 spec)

* Use strlcpy for copying strings instead of snprintf "%s"
 * Don't cast the return value from vsnprintf to (void)
 * When we use va_start make sure to use va_end as well...
 * Fix indentation


git-svn-id: svn+ssh://svn.gna.org/svn/warzone/trunk@4785 4a71c877-e1ca-e34f-864e-861f7616d084
master
Giel van Schijndel 2008-04-26 14:21:41 +00:00
parent 477711c9a4
commit 61c8bbc4b9
15 changed files with 21 additions and 94 deletions

View File

@ -92,9 +92,6 @@ static LONG WINAPI windowsExceptionHandler(PEXCEPTION_POINTERS pExceptionInfo)
snprintf(resultMessage, sizeof(resultMessage), "Failed to create dump file '%s' (error %d)", miniDumpPath, (int)GetLastError()); snprintf(resultMessage, sizeof(resultMessage), "Failed to create dump file '%s' (error %d)", miniDumpPath, (int)GetLastError());
} }
// Guarantee to nul-terminate
resultMessage[sizeof(resultMessage) - 1] = '\0';
MessageBoxA( NULL, resultMessage, applicationName, MB_OK ); MessageBoxA( NULL, resultMessage, applicationName, MB_OK );
} }

View File

@ -347,8 +347,6 @@ BOOL setWarzoneKeyNumeric( const char *pName, SDWORD val )
//~~~~~~~~~~~~ //~~~~~~~~~~~~
snprintf(buf, sizeof(buf), "%i", val); snprintf(buf, sizeof(buf), "%i", val);
// Guarantee to nul-terminate
buf[sizeof(buf) - 1] = '\0';
registry_set_key( pName, buf ); registry_set_key( pName, buf );
return true; return true;

View File

@ -326,8 +326,6 @@ void _debug( code_part part, const char *function, const char *str, ... )
va_start(ap, str); va_start(ap, str);
vsnprintf(outputBuffer, MAX_LEN_LOG_LINE, str, ap); vsnprintf(outputBuffer, MAX_LEN_LOG_LINE, str, ap);
va_end(ap); va_end(ap);
// Guarantee to nul-terminate
outputBuffer[MAX_LEN_LOG_LINE - 1] = '\0';
snprintf(inputBuffer[useInputBuffer1 ? 1 : 0], MAX_LEN_LOG_LINE, "[%s] %s", function, outputBuffer); snprintf(inputBuffer[useInputBuffer1 ? 1 : 0], MAX_LEN_LOG_LINE, "[%s] %s", function, outputBuffer);

View File

@ -334,21 +334,18 @@ void pie_LoadBackDrop(SCREENTYPE screenType)
switch (screenType) switch (screenType)
{ {
case SCREEN_RANDOMBDROP: case SCREEN_RANDOMBDROP:
snprintf(backd, sizeof(backd), "texpages/bdrops/backdrop%i.png", rand() % 7); // Range: 0-6 snprintf(backd, sizeof(backd), "texpages/bdrops/backdrop%i.png", rand() % 7); // Range: 0-6
break; break;
case SCREEN_MISSIONEND: case SCREEN_MISSIONEND:
snprintf(backd, sizeof(backd), "texpages/bdrops/missionend.png"); strlcpy(backd, "texpages/bdrops/missionend.png", sizeof(backd));
break; break;
case SCREEN_CREDITS: case SCREEN_CREDITS:
default: default:
snprintf(backd, sizeof(backd), "texpages/bdrops/credits.png"); strlcpy(backd, "texpages/bdrops/credits.png", sizeof(backd));
break; break;
} }
// Guarantee to nul-terminate
backd[sizeof(backd) - 1] = '\0';
screen_SetBackDropFromFile(backd); screen_SetBackDropFromFile(backd);
} }

View File

@ -124,8 +124,6 @@ BOOL NETstartLogging(void)
return false; return false;
} }
snprintf(buf, sizeof(buf), "NETPLAY log: %s\n", asctime(newtime)); snprintf(buf, sizeof(buf), "NETPLAY log: %s\n", asctime(newtime));
// Guarantee to nul-terminate
buf[sizeof(buf) - 1] = '\0';
PHYSFS_write( pFileHandle, buf, strlen( buf ), 1 ); PHYSFS_write( pFileHandle, buf, strlen( buf ), 1 );
return true; return true;
} }
@ -140,8 +138,6 @@ BOOL NETstopLogging(void)
{ {
snprintf(buf, sizeof(buf), "%s: received %u times, %u bytes; sent %u times, %u bytes\n", packetname[i], snprintf(buf, sizeof(buf), "%s: received %u times, %u bytes; sent %u times, %u bytes\n", packetname[i],
packetcount[0][i], packetsize[0][i], packetcount[1][i], packetsize[1][i]); packetcount[0][i], packetsize[0][i], packetcount[1][i], packetsize[1][i]);
// Guarantee to nul-terminate
buf[sizeof(buf) - 1] = '\0';
PHYSFS_write(pFileHandle, buf, strlen(buf), 1); PHYSFS_write(pFileHandle, buf, strlen(buf), 1);
} }
@ -199,9 +195,6 @@ BOOL NETlogEntry(const char *str,UDWORD a,UDWORD b)
else else
snprintf(buf, sizeof(buf), "%s \t:%d \t\t\t:%d\t\t%s", str, a, b, asctime(newtime)); snprintf(buf, sizeof(buf), "%s \t:%d \t\t\t:%d\t\t%s", str, a, b, asctime(newtime));
// Guarantee to nul-terminate
buf[sizeof(buf) - 1] = '\0';
if (a == 18) // NET_LEAVING if (a == 18) // NET_LEAVING
// Write a starry line above NET_LEAVING messages // Write a starry line above NET_LEAVING messages
PHYSFS_write(pFileHandle, star_line, strlen(star_line), 1); PHYSFS_write(pFileHandle, star_line, strlen(star_line), 1);

View File

@ -1566,9 +1566,6 @@ BOOL NETfindGame(void)
(int)(address[1]), (int)(address[1]),
(int)(address[2]), (int)(address[2]),
(int)(address[3])); (int)(address[3]));
// Guarantee to nul-terminate
NetPlay.games[gamecount].desc.host[sizeof(NetPlay.games[gamecount].desc.host) - 1] = '\0';
} }
++gamecount; ++gamecount;

View File

@ -753,8 +753,6 @@ void chat_error(const char *pMessage,...)
va_start(args, pMessage); va_start(args, pMessage);
vsnprintf(aTxtBuf, sizeof(aTxtBuf), pMessage, args); vsnprintf(aTxtBuf, sizeof(aTxtBuf), pMessage, args);
// Guarantee to nul-terminate
aTxtBuf[sizeof(aTxtBuf) - 1] = '\0';
chatGetErrorData(&line, &pText); chatGetErrorData(&line, &pText);
//debug(LOG_WARNING, "multiplayer message parse error: %s at line %d, token: %d, text: '%s'", //debug(LOG_WARNING, "multiplayer message parse error: %s at line %d, token: %d, text: '%s'",
// aTxtBuf, line, chat_char, pText); // aTxtBuf, line, chat_char, pText);

View File

@ -213,8 +213,6 @@ const char *eventGetTriggerID(SCRIPT_CODE *psCode, SDWORD trigger)
if (psCode->psDebug == NULL || type != TR_CODE) if (psCode->psDebug == NULL || type != TR_CODE)
{ {
snprintf(aIDNum, sizeof(aIDNum), "%d (%s)", trigger, pTrigType); snprintf(aIDNum, sizeof(aIDNum), "%d (%s)", trigger, pTrigType);
// Guarantee to nul-terminate
aIDNum[sizeof(aIDNum) - 1] = '\0';
} }
else else
{ {
@ -228,8 +226,6 @@ const char *eventGetTriggerID(SCRIPT_CODE *psCode, SDWORD trigger)
} }
} }
snprintf(aIDNum, sizeof(aIDNum), "%s (%s)", pID, pTrigType); snprintf(aIDNum, sizeof(aIDNum), "%s (%s)", pID, pTrigType);
// Guarantee to nul-terminate
aIDNum[sizeof(aIDNum) - 1] = '\0';
} }
return aIDNum; return aIDNum;
@ -253,8 +249,6 @@ const char *eventGetEventID(SCRIPT_CODE *psCode, SDWORD event)
(event < 0) || (event > psCode->numEvents)) (event < 0) || (event > psCode->numEvents))
{ {
snprintf(aIDNum, sizeof(aIDNum), "%d", event); snprintf(aIDNum, sizeof(aIDNum), "%d", event);
// Guarantee to nul-terminate
aIDNum[sizeof(aIDNum) - 1] = '\0';
return aIDNum; return aIDNum;
} }

View File

@ -703,9 +703,7 @@ void script_debug(const char *pFormat, ...)
va_start(pArgs, pFormat); va_start(pArgs, pFormat);
(void)vsnprintf(buffer, sizeof(buffer), pFormat, pArgs); vsnprintf(buffer, sizeof(buffer), pFormat, pArgs);
// Guarantee to nul-terminate
buffer[sizeof(buffer) - 1] = '\0';
debug(LOG_SCRIPT, "%s", buffer); debug(LOG_SCRIPT, "%s", buffer);
} }
@ -747,8 +745,6 @@ static CODE_ERROR scriptCodeFunction(FUNC_SYMBOL *psFSymbol, // The function be
{ {
debug(LOG_ERROR, "scriptCodeFunction: Type mismatch for paramter %d (%d/%d)", i, psFSymbol->aParams[i], psPBlock->aParams[i]); debug(LOG_ERROR, "scriptCodeFunction: Type mismatch for paramter %d (%d/%d)", i, psFSymbol->aParams[i], psPBlock->aParams[i]);
snprintf(aErrorString, sizeof(aErrorString), "Type mismatch for paramter %d", i); snprintf(aErrorString, sizeof(aErrorString), "Type mismatch for paramter %d", i);
// Guarantee to nul-terminate
aErrorString[sizeof(aErrorString) - 1] = '\0';
scr_error(aErrorString); scr_error(aErrorString);
typeError = true; typeError = true;
} }
@ -763,8 +759,6 @@ static CODE_ERROR scriptCodeFunction(FUNC_SYMBOL *psFSymbol, // The function be
if (psFSymbol->numParams != psPBlock->numParams) if (psFSymbol->numParams != psPBlock->numParams)
{ {
snprintf(aErrorString, sizeof(aErrorString), "Expected %d parameters", psFSymbol->numParams); snprintf(aErrorString, sizeof(aErrorString), "Expected %d parameters", psFSymbol->numParams);
// Guarantee to nul-terminate
aErrorString[sizeof(aErrorString) - 1] = '\0';
scr_error(aErrorString); scr_error(aErrorString);
*ppsCBlock = NULL; *ppsCBlock = NULL;
return CE_PARSE; return CE_PARSE;
@ -919,8 +913,6 @@ static CODE_ERROR scriptCodeCallbackParams(
if (!interpCheckEquiv(psCBSymbol->aParams[i], psPBlock->aParams[i])) if (!interpCheckEquiv(psCBSymbol->aParams[i], psPBlock->aParams[i]))
{ {
snprintf(aErrorString, sizeof(aErrorString), "Type mismatch for paramter %d", i); snprintf(aErrorString, sizeof(aErrorString), "Type mismatch for paramter %d", i);
// Guarantee to nul-terminate
aErrorString[sizeof(aErrorString) - 1] = '\0';
scr_error(aErrorString); scr_error(aErrorString);
typeError = true; typeError = true;
} }
@ -936,8 +928,6 @@ static CODE_ERROR scriptCodeCallbackParams(
else if (psCBSymbol->numParams != psPBlock->numParams) else if (psCBSymbol->numParams != psPBlock->numParams)
{ {
snprintf(aErrorString, sizeof(aErrorString), "Expected %d parameters", psCBSymbol->numParams); snprintf(aErrorString, sizeof(aErrorString), "Expected %d parameters", psCBSymbol->numParams);
// Guarantee to nul-terminate
aErrorString[sizeof(aErrorString) - 1] = '\0';
scr_error(aErrorString); scr_error(aErrorString);
*ppsTDecl = NULL; *ppsTDecl = NULL;
return CE_PARSE; return CE_PARSE;
@ -5837,8 +5827,6 @@ void scr_error(const char *pMessage, ...)
va_start(args, pMessage); va_start(args, pMessage);
vsnprintf(aBuff, sizeof(aBuff), pMessage, args); vsnprintf(aBuff, sizeof(aBuff), pMessage, args);
va_end(args); va_end(args);
// Guarantee to nul-terminate
aBuff[sizeof(aBuff) - 1] = '\0';
scriptGetErrorData(&line, &text); scriptGetErrorData(&line, &text);

View File

@ -706,24 +706,18 @@ BOOL stackBinaryOp(OPCODE opcode)
{ {
case VAL_INT: //first value isn't string, but can be converted to string case VAL_INT: //first value isn't string, but can be converted to string
snprintf(tempstr1, sizeof(tempstr1), "%d", psV1->v.ival); //int->string snprintf(tempstr1, sizeof(tempstr1), "%d", psV1->v.ival); //int->string
// Guarantee to nul-terminate
tempstr1[sizeof(tempstr1) - 1] = '\0';
psV1->type = VAL_STRING; //Mark as string psV1->type = VAL_STRING; //Mark as string
psV1->v.sval = (char*)malloc(MAXSTRLEN); //allocate space for the string, since the result (string) of concatenation will be saved here psV1->v.sval = (char*)malloc(MAXSTRLEN); //allocate space for the string, since the result (string) of concatenation will be saved here
break; break;
case VAL_BOOL: case VAL_BOOL:
snprintf(tempstr1, sizeof(tempstr1), "%d",psV1->v.bval); //bool->string snprintf(tempstr1, sizeof(tempstr1), "%d",psV1->v.bval); //bool->string
// Guarantee to nul-terminate
tempstr1[sizeof(tempstr1) - 1] = '\0';
psV1->type = VAL_STRING; //Mark as string psV1->type = VAL_STRING; //Mark as string
psV1->v.sval = (char*)malloc(MAXSTRLEN); //allocate space for the string, since the result (string) of concatenation will be saved here psV1->v.sval = (char*)malloc(MAXSTRLEN); //allocate space for the string, since the result (string) of concatenation will be saved here
break; break;
case VAL_FLOAT: case VAL_FLOAT:
snprintf(tempstr1, sizeof(tempstr1), "%f", psV1->v.fval); //float->string snprintf(tempstr1, sizeof(tempstr1), "%f", psV1->v.fval); //float->string
// Guarantee to nul-terminate
tempstr1[sizeof(tempstr1) - 1] = '\0';
psV1->type = VAL_STRING; //Mark as string psV1->type = VAL_STRING; //Mark as string
psV1->v.sval = (char*)malloc(MAXSTRLEN); //allocate space for the string, since the result (string) of concatenation will be saved here psV1->v.sval = (char*)malloc(MAXSTRLEN); //allocate space for the string, since the result (string) of concatenation will be saved here
break; break;
@ -743,20 +737,14 @@ BOOL stackBinaryOp(OPCODE opcode)
{ {
case VAL_INT: case VAL_INT:
snprintf(tempstr2, sizeof(tempstr2), "%d", psV2->v.ival); //int->string snprintf(tempstr2, sizeof(tempstr2), "%d", psV2->v.ival); //int->string
// Guarantee to nul-terminate
tempstr2[sizeof(tempstr2) - 1] = '\0';
break; break;
case VAL_BOOL: case VAL_BOOL:
snprintf(tempstr2, sizeof(tempstr2), "%d", psV2->v.bval); //bool->string snprintf(tempstr2, sizeof(tempstr2), "%d", psV2->v.bval); //bool->string
// Guarantee to nul-terminate
tempstr2[sizeof(tempstr2) - 1] = '\0';
break; break;
case VAL_FLOAT: case VAL_FLOAT:
snprintf(tempstr2, sizeof(tempstr2), "%f", psV2->v.fval); //float->string snprintf(tempstr2, sizeof(tempstr2), "%f", psV2->v.fval); //float->string
// Guarantee to nul-terminate
tempstr2[sizeof(tempstr2) - 1] = '\0';
break; break;
case VAL_STRING: case VAL_STRING:

View File

@ -94,8 +94,6 @@ switch (type)
char format[sizeof(FORMAT_PREFIX)+8+1]; char format[sizeof(FORMAT_PREFIX)+8+1];
snprintf(format, sizeof(format), "%s%08lx", FORMAT_PREFIX, (unsigned long)type); snprintf(format, sizeof(format), "%s%08lx", FORMAT_PREFIX, (unsigned long)type);
// Guarantee to nul-terminate
format[sizeof(format) - 1] = '\0';
#if defined(WZ_WS_X11) #if defined(WZ_WS_X11)
/* * */ /* * */

View File

@ -205,9 +205,6 @@ BOOL SetUpInputFile(SDWORD nPlayer)
/* Assemble dir */ /* Assemble dir */
snprintf(sPlayer, sizeof(sPlayer), "%d", nPlayer); snprintf(sPlayer, sizeof(sPlayer), "%d", nPlayer);
// Guarantee to nul-terminate
sPlayer[sizeof(sPlayer) - 1] = '\0';
astrlcat(SaveDir, "player"); astrlcat(SaveDir, "player");
astrlcat(SaveDir, sPlayer); astrlcat(SaveDir, sPlayer);
astrlcat(SaveDir, "/"); // like "multiplay\learndata\player0\" astrlcat(SaveDir, "/"); // like "multiplay\learndata\player0\"

View File

@ -757,22 +757,16 @@ UDWORD getConsoleLineInfo(void)
/// Function with printf arguments to print to the console /// Function with printf arguments to print to the console
void consolePrintf(char *layout, ...) void consolePrintf(char *layout, ...)
{ {
char consoleString[MAX_CONSOLE_STRING_LENGTH]; char consoleString[MAX_CONSOLE_STRING_LENGTH];
va_list arguments; // Formatting info va_list arguments; // Formatting info
/* Boot off the argument List */
va_start(arguments,layout);
/* 'print' it out into our buffer */ /* 'print' it out into our buffer */
va_start(arguments,layout);
vsnprintf(consoleString, sizeof(consoleString), layout, arguments); vsnprintf(consoleString, sizeof(consoleString), layout, arguments);
// Guarantee to nul-terminate va_end(arguments);
consoleString[sizeof(consoleString) - 1] = '\0';
/* Add the message through the normal channels! */ /* Add the message through the normal channels! */
addConsoleMessage(consoleString,DEFAULT_JUSTIFY,SYSTEM_MESSAGE); addConsoleMessage(consoleString,DEFAULT_JUSTIFY,SYSTEM_MESSAGE);
/* Close arguments */
va_end(arguments);
} }
/// Set if new messages may be added to the console /// Set if new messages may be added to the console
@ -792,15 +786,12 @@ void printf_console(const char *pFormat, ...)
{ {
#ifdef DEBUG #ifdef DEBUG
char aBuffer[500]; // Output string buffer char aBuffer[500]; // Output string buffer
va_list pArgs; // Format arguments va_list pArgs; // Format arguments
/* Initialise the argument list */
va_start(pArgs, pFormat);
/* Print out the string */ /* Print out the string */
va_start(pArgs, pFormat);
vsnprintf(aBuffer, sizeof(aBuffer), pFormat, pArgs); vsnprintf(aBuffer, sizeof(aBuffer), pFormat, pArgs);
// Guarantee to nul-terminate va_end(pArgs);
aBuffer[sizeof(aBuffer) - 1] = '\0';
/* Output it */ /* Output it */
@ -812,15 +803,12 @@ void printf_console(const char *pFormat, ...)
void console(const char *pFormat, ...) void console(const char *pFormat, ...)
{ {
char aBuffer[500]; // Output string buffer char aBuffer[500]; // Output string buffer
va_list pArgs; // Format arguments va_list pArgs; // Format arguments
/* Initialise the argument list */
va_start(pArgs, pFormat);
/* Print out the string */ /* Print out the string */
va_start(pArgs, pFormat);
vsnprintf(aBuffer, sizeof(aBuffer), pFormat, pArgs); vsnprintf(aBuffer, sizeof(aBuffer), pFormat, pArgs);
// Guarantee to nul-terminate va_end(pArgs);
aBuffer[sizeof(aBuffer) - 1] = '\0';
/* Output it */ /* Output it */
addConsoleMessage(aBuffer,DEFAULT_JUSTIFY,SYSTEM_MESSAGE); addConsoleMessage(aBuffer,DEFAULT_JUSTIFY,SYSTEM_MESSAGE);

View File

@ -1091,8 +1091,6 @@ BOOL intAddTemplateButtons(UDWORD formID, UDWORD formWidth, UDWORD formHeight,
if(sBarInit.size > WBAR_SCALE) sBarInit.size = WBAR_SCALE; if(sBarInit.size > WBAR_SCALE) sBarInit.size = WBAR_SCALE;
snprintf(TempString, sizeof(TempString), "%s - %d",_("Power Usage"), psTempl->powerPoints); snprintf(TempString, sizeof(TempString), "%s - %d",_("Power Usage"), psTempl->powerPoints);
// Guarantee to nul-terminate
TempString[sizeof(TempString) - 1] = '\0';
ASSERT(BufferPos + strlen(TempString) + 1 < sizeof(StringBuffer), "String Buffer Overflow"); ASSERT(BufferPos + strlen(TempString) + 1 < sizeof(StringBuffer), "String Buffer Overflow");
strlcpy(&StringBuffer[BufferPos], TempString, sizeof(StringBuffer) - BufferPos); strlcpy(&StringBuffer[BufferPos], TempString, sizeof(StringBuffer) - BufferPos);

View File

@ -479,8 +479,6 @@ void intAddLoopQuantity(WIDGET *psWidget, W_CONTEXT *psContext)
else else
{ {
snprintf(Label->aText, sizeof(Label->aText), "%02u", psFactory->quantity + DEFAULT_LOOP); snprintf(Label->aText, sizeof(Label->aText), "%02u", psFactory->quantity + DEFAULT_LOOP);
// Guarantee to nul-terminate
Label->aText[sizeof(Label->aText) - 1] = '\0';
} }
Label->style &= ~WIDG_HIDDEN; Label->style &= ~WIDG_HIDDEN;
} }