* 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());
}
// Guarantee to nul-terminate
resultMessage[sizeof(resultMessage) - 1] = '\0';
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);
// Guarantee to nul-terminate
buf[sizeof(buf) - 1] = '\0';
registry_set_key( pName, buf );
return true;

View File

@ -326,8 +326,6 @@ void _debug( code_part part, const char *function, const char *str, ... )
va_start(ap, str);
vsnprintf(outputBuffer, MAX_LEN_LOG_LINE, str, 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);

View File

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

View File

@ -124,8 +124,6 @@ BOOL NETstartLogging(void)
return false;
}
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 );
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],
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);
}
@ -199,9 +195,6 @@ BOOL NETlogEntry(const char *str,UDWORD a,UDWORD b)
else
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
// Write a starry line above NET_LEAVING messages
PHYSFS_write(pFileHandle, star_line, strlen(star_line), 1);

View File

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

View File

@ -753,8 +753,6 @@ void chat_error(const char *pMessage,...)
va_start(args, pMessage);
vsnprintf(aTxtBuf, sizeof(aTxtBuf), pMessage, args);
// Guarantee to nul-terminate
aTxtBuf[sizeof(aTxtBuf) - 1] = '\0';
chatGetErrorData(&line, &pText);
//debug(LOG_WARNING, "multiplayer message parse error: %s at line %d, token: %d, text: '%s'",
// 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)
{
snprintf(aIDNum, sizeof(aIDNum), "%d (%s)", trigger, pTrigType);
// Guarantee to nul-terminate
aIDNum[sizeof(aIDNum) - 1] = '\0';
}
else
{
@ -228,8 +226,6 @@ const char *eventGetTriggerID(SCRIPT_CODE *psCode, SDWORD trigger)
}
}
snprintf(aIDNum, sizeof(aIDNum), "%s (%s)", pID, pTrigType);
// Guarantee to nul-terminate
aIDNum[sizeof(aIDNum) - 1] = '\0';
}
return aIDNum;
@ -253,8 +249,6 @@ const char *eventGetEventID(SCRIPT_CODE *psCode, SDWORD event)
(event < 0) || (event > psCode->numEvents))
{
snprintf(aIDNum, sizeof(aIDNum), "%d", event);
// Guarantee to nul-terminate
aIDNum[sizeof(aIDNum) - 1] = '\0';
return aIDNum;
}

View File

@ -703,9 +703,7 @@ void script_debug(const char *pFormat, ...)
va_start(pArgs, pFormat);
(void)vsnprintf(buffer, sizeof(buffer), pFormat, pArgs);
// Guarantee to nul-terminate
buffer[sizeof(buffer) - 1] = '\0';
vsnprintf(buffer, sizeof(buffer), pFormat, pArgs);
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]);
snprintf(aErrorString, sizeof(aErrorString), "Type mismatch for paramter %d", i);
// Guarantee to nul-terminate
aErrorString[sizeof(aErrorString) - 1] = '\0';
scr_error(aErrorString);
typeError = true;
}
@ -763,8 +759,6 @@ static CODE_ERROR scriptCodeFunction(FUNC_SYMBOL *psFSymbol, // The function be
if (psFSymbol->numParams != psPBlock->numParams)
{
snprintf(aErrorString, sizeof(aErrorString), "Expected %d parameters", psFSymbol->numParams);
// Guarantee to nul-terminate
aErrorString[sizeof(aErrorString) - 1] = '\0';
scr_error(aErrorString);
*ppsCBlock = NULL;
return CE_PARSE;
@ -919,8 +913,6 @@ static CODE_ERROR scriptCodeCallbackParams(
if (!interpCheckEquiv(psCBSymbol->aParams[i], psPBlock->aParams[i]))
{
snprintf(aErrorString, sizeof(aErrorString), "Type mismatch for paramter %d", i);
// Guarantee to nul-terminate
aErrorString[sizeof(aErrorString) - 1] = '\0';
scr_error(aErrorString);
typeError = true;
}
@ -936,8 +928,6 @@ static CODE_ERROR scriptCodeCallbackParams(
else if (psCBSymbol->numParams != psPBlock->numParams)
{
snprintf(aErrorString, sizeof(aErrorString), "Expected %d parameters", psCBSymbol->numParams);
// Guarantee to nul-terminate
aErrorString[sizeof(aErrorString) - 1] = '\0';
scr_error(aErrorString);
*ppsTDecl = NULL;
return CE_PARSE;
@ -5837,8 +5827,6 @@ void scr_error(const char *pMessage, ...)
va_start(args, pMessage);
vsnprintf(aBuff, sizeof(aBuff), pMessage, args);
va_end(args);
// Guarantee to nul-terminate
aBuff[sizeof(aBuff) - 1] = '\0';
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
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->v.sval = (char*)malloc(MAXSTRLEN); //allocate space for the string, since the result (string) of concatenation will be saved here
break;
case VAL_BOOL:
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->v.sval = (char*)malloc(MAXSTRLEN); //allocate space for the string, since the result (string) of concatenation will be saved here
break;
case VAL_FLOAT:
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->v.sval = (char*)malloc(MAXSTRLEN); //allocate space for the string, since the result (string) of concatenation will be saved here
break;
@ -743,20 +737,14 @@ BOOL stackBinaryOp(OPCODE opcode)
{
case VAL_INT:
snprintf(tempstr2, sizeof(tempstr2), "%d", psV2->v.ival); //int->string
// Guarantee to nul-terminate
tempstr2[sizeof(tempstr2) - 1] = '\0';
break;
case VAL_BOOL:
snprintf(tempstr2, sizeof(tempstr2), "%d", psV2->v.bval); //bool->string
// Guarantee to nul-terminate
tempstr2[sizeof(tempstr2) - 1] = '\0';
break;
case VAL_FLOAT:
snprintf(tempstr2, sizeof(tempstr2), "%f", psV2->v.fval); //float->string
// Guarantee to nul-terminate
tempstr2[sizeof(tempstr2) - 1] = '\0';
break;
case VAL_STRING:

View File

@ -94,8 +94,6 @@ switch (type)
char format[sizeof(FORMAT_PREFIX)+8+1];
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)
/* * */

View File

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

View File

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

View File

@ -479,8 +479,6 @@ void intAddLoopQuantity(WIDGET *psWidget, W_CONTEXT *psContext)
else
{
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;
}