From a7ba1da07dbc23fe0e310d51b7bdf2d2a1bc1004 Mon Sep 17 00:00:00 2001 From: Byrial Jensen Date: Thu, 22 Dec 2011 14:30:08 +0100 Subject: [PATCH] Fix checking of return value from snprintf() snprintf() returns the needed space excluding the trailing 0 byte, so it has truncated if the returned valued is greater than or EQUAL TO the buffersize --- args.h | 12 ++++++------ backend.c | 12 ++++++------ 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/args.h b/args.h index 6ea0237..6f51446 100644 --- a/args.h +++ b/args.h @@ -768,7 +768,7 @@ ExitArgError(char *msg, char *badArg, Boolean quit) int len; len = snprintf(buf,MSG_SIZ, "%s %s", msg, badArg); - if( (len > MSG_SIZ) && appData.debugMode ) + if( (len >= MSG_SIZ) && appData.debugMode ) fprintf(debugFP, "ExitArgError: buffer truncated. Input: msg=%s badArg=%s\n", msg, badArg); if(!quit) { printf(_("%s in settings file\n"), buf); return; } // DisplayError does not work yet at this stage... @@ -820,7 +820,7 @@ ParseSettingsFile(char *name, char **addr) if(!ok && strchr(name, '.') == NULL) { // append default file-name extension '.ini' when needed len = snprintf(buf,MSG_SIZ, "%s.ini", name); - if( (len > MSG_SIZ) && appData.debugMode ) + if( (len >= MSG_SIZ) && appData.debugMode ) fprintf(debugFP, "ParseSettingsFile: buffer truncated. Input: name=%s \n",name); ok = MySearchPath(installDir, buf, fullname); @@ -1267,7 +1267,7 @@ InitAppData(char *lpCmdLine) if(p != NULL) { // engine command line contains WinBoard options len = snprintf(buf, MSG_SIZ, p+6, f, f, f, f, f, f, f, f, f, f); // replace %s in them by "first" - if( (len > MSG_SIZ) && appData.debugMode ) + if( (len >= MSG_SIZ) && appData.debugMode ) fprintf(debugFP, "InitAppData: buffer truncated.\n"); ParseArgs(StringGet, &q); @@ -1284,7 +1284,7 @@ InitAppData(char *lpCmdLine) if(p != NULL) { // engine command line contains WinBoard options len = snprintf(buf,MSG_SIZ, p+6, s, s, s, s, s, s, s, s, s, s); // replace %s in them by "first" - if( (len > MSG_SIZ) && appData.debugMode ) + if( (len >= MSG_SIZ) && appData.debugMode ) fprintf(debugFP, "InitAppData: buffer truncated.\n"); ParseArgs(StringGet, &q); @@ -1483,13 +1483,13 @@ GetArgValue(char *name) return TRUE; case ArgInt: len = snprintf(name, MSG_SIZ, "%d", *(int*) ad->argLoc); - if( (len > MSG_SIZ) && appData.debugMode ) + if( (len >= MSG_SIZ) && appData.debugMode ) fprintf(debugFP, "GetArgValue: buffer truncated.\n"); return TRUE; case ArgBoolean: len = snprintf(name, MSG_SIZ, "%s", *(Boolean*) ad->argLoc ? "true" : "false"); - if( (len > MSG_SIZ) && appData.debugMode ) + if( (len >= MSG_SIZ) && appData.debugMode ) fprintf(debugFP, "GetArgValue: buffer truncated.\n"); return TRUE; diff --git a/backend.c b/backend.c index 935b9a0..8dc8f07 100644 --- a/backend.c +++ b/backend.c @@ -815,7 +815,7 @@ InitEngine(ChessProgramState *cps, int n) len = snprintf(buf, MSG_SIZ, _("protocol version %d not supported"), appData.protocolVersion[n]); - if( (len > MSG_SIZ) && appData.debugMode ) + if( (len >= MSG_SIZ) && appData.debugMode ) fprintf(debugFP, "InitBackEnd1: buffer truncated.\n"); DisplayFatalError(buf, 0, 2); @@ -1062,7 +1062,7 @@ InitBackEnd1() case VariantKriegspiel: /* need to hide pieces and move details */ /* case VariantFischeRandom: (Fabien: moved below) */ len = snprintf(buf,MSG_SIZ, _("Variant %s supported only in ICS mode"), appData.variant); - if( (len > MSG_SIZ) && appData.debugMode ) + if( (len >= MSG_SIZ) && appData.debugMode ) fprintf(debugFP, "InitBackEnd1: buffer truncated.\n"); DisplayFatalError(buf, 0, 2); @@ -1080,7 +1080,7 @@ InitBackEnd1() case Variant36: default: len = snprintf(buf, MSG_SIZ, _("Unknown variant name %s"), appData.variant); - if( (len > MSG_SIZ) && appData.debugMode ) + if( (len >= MSG_SIZ) && appData.debugMode ) fprintf(debugFP, "InitBackEnd1: buffer truncated.\n"); DisplayFatalError(buf, 0, 2); @@ -1485,7 +1485,7 @@ InitBackEnd3 P((void)) len = snprintf(buf, MSG_SIZ, _("Could not connect to host %s, port %s"), appData.icsHost, appData.icsPort); - if( (len > MSG_SIZ) && appData.debugMode ) + if( (len >= MSG_SIZ) && appData.debugMode ) fprintf(debugFP, "InitBackEnd3: buffer truncated.\n"); DisplayFatalError(buf, err, 1); @@ -1539,7 +1539,7 @@ InitBackEnd3 P((void)) initialMode = Training; } else { len = snprintf(buf, MSG_SIZ, _("Unknown initialMode %s"), appData.initialMode); - if( (len > MSG_SIZ) && appData.debugMode ) + if( (len >= MSG_SIZ) && appData.debugMode ) fprintf(debugFP, "InitBackEnd3: buffer truncated.\n"); DisplayFatalError(buf, 0, 2); @@ -2138,7 +2138,7 @@ StringToVariant(e) break; default: len = snprintf(buf, MSG_SIZ, _("Unknown wild type %d"), wnum); - if( (len > MSG_SIZ) && appData.debugMode ) + if( (len >= MSG_SIZ) && appData.debugMode ) fprintf(debugFP, "StringToVariant: buffer truncated.\n"); DisplayError(buf, 0); -- 1.7.0.4