From 97d53ced7eb163c36b17f3b6539c9cc87808eded Mon Sep 17 00:00:00 2001 From: pionere Date: Wed, 25 Nov 2020 19:05:44 +0100 Subject: [PATCH] get rid of a few warnings (strncpy) --- 3rdParty/Storm/Source/storm.h | 7 ++----- DiabloUI/diabloui.h | 6 +++--- Source/mainmenu.cpp | 4 ++-- Source/multi.cpp | 5 ++--- Source/pfile.cpp | 2 +- Source/plrmsg.cpp | 6 ++---- Source/plrmsg.h | 2 +- SourceX/DiabloUI/diabloui.cpp | 18 +++++++++--------- SourceX/DiabloUI/selhero.cpp | 10 +++++----- SourceX/storm/storm.cpp | 5 ++--- SourceX/storm/storm_net.cpp | 4 +--- 11 files changed, 30 insertions(+), 39 deletions(-) diff --git a/3rdParty/Storm/Source/storm.h b/3rdParty/Storm/Source/storm.h index a150278ce..ec06ef181 100644 --- a/3rdParty/Storm/Source/storm.h +++ b/3rdParty/Storm/Source/storm.h @@ -85,7 +85,6 @@ SNetDropPlayer( * type: The type of data to retrieve. See GAMEINFO_ flags. * dst: The destination buffer for the data. * length: The maximum size of the destination buffer. - * byteswritten: The number of bytes written to the destination buffer. * * Returns TRUE if the function was called successfully and FALSE otherwise. */ @@ -94,8 +93,7 @@ STORMAPI SNetGetGameInfo( int type, void *dst, - unsigned int length, - unsigned int *byteswritten); + unsigned int length); /* SNetGetTurnsInTransit @ 115 * @@ -336,9 +334,8 @@ SErrSetLastError( * src: The source array. * max_length: The maximum length of dest. * - * Returns the number of characters copied. */ -int +void STORMAPI SStrCopy( char *dest, diff --git a/DiabloUI/diabloui.h b/DiabloUI/diabloui.h index 3267f299b..77048a244 100644 --- a/DiabloUI/diabloui.h +++ b/DiabloUI/diabloui.h @@ -9,8 +9,8 @@ void UiTitleDialog(); void UiSetSpawned(BOOL bSpawned); void UiInitialize(); BOOL UiValidPlayerName(const char *name); /* check */ -BOOL UiSelHeroMultDialog(BOOL(*fninfo)(BOOL(*fninfofunc)(_uiheroinfo *)), BOOL(*fncreate)(_uiheroinfo *), BOOL(*fnremove)(_uiheroinfo *), void(*fnstats)(unsigned int, _uidefaultstats *), int *dlgresult, BOOL *hero_is_created, char *name); -BOOL UiSelHeroSingDialog(BOOL(*fninfo)(BOOL(*fninfofunc)(_uiheroinfo *)), BOOL(*fncreate)(_uiheroinfo *), BOOL(*fnremove)(_uiheroinfo *), void(*fnstats)(unsigned int, _uidefaultstats *), int *dlgresult, char *name, int *difficulty); +BOOL UiSelHeroMultDialog(BOOL(*fninfo)(BOOL(*fninfofunc)(_uiheroinfo *)), BOOL(*fncreate)(_uiheroinfo *), BOOL(*fnremove)(_uiheroinfo *), void(*fnstats)(unsigned int, _uidefaultstats *), int *dlgresult, BOOL *hero_is_created, char (* name)[16]); +BOOL UiSelHeroSingDialog(BOOL(*fninfo)(BOOL(*fninfofunc)(_uiheroinfo *)), BOOL(*fncreate)(_uiheroinfo *), BOOL(*fnremove)(_uiheroinfo *), void(*fnstats)(unsigned int, _uidefaultstats *), int *dlgresult, char (* name)[16], int *difficulty); BOOL UiCreditsDialog(int a1); BOOL UiMainMenuDialog(const char *name, int *pdwResult, void(*fnSound)(const char *file), int attractTimeOut); BOOL UiProgressDialog(const char *msg, int enable, int(*fnfunc)(), int rate); @@ -27,7 +27,7 @@ BOOL UiCreateGameCallback(int a1, int a2, int a3, int a4, int a5, int a6); BOOL UiArtCallback(int game_type, unsigned int art_code, SDL_Color *pPalette, BYTE *pBuffer, DWORD dwBuffersize, DWORD *pdwWidth, DWORD *pdwHeight, DWORD *pdwBpp); int UiSelectGame(int a1, _SNETPROGRAMDATA *client_info, _SNETPLAYERDATA *user_info, _SNETUIDATA *ui_info, _SNETVERSIONDATA *file_info, int *a6); int UiSelectProvider(int a1, _SNETPROGRAMDATA *client_info, _SNETPLAYERDATA *user_info, _SNETUIDATA *ui_info, _SNETVERSIONDATA *file_info, int *type); -BOOL UiCreatePlayerDescription(_uiheroinfo *info, DWORD mode, char *desc); +BOOL UiCreatePlayerDescription(_uiheroinfo *info, DWORD mode, char (*desc)[128]); void UiSetupPlayerInfo(char *infostr, _uiheroinfo *pInfo, DWORD type); } diff --git a/Source/mainmenu.cpp b/Source/mainmenu.cpp index f468df556..f718c9252 100644 --- a/Source/mainmenu.cpp +++ b/Source/mainmenu.cpp @@ -106,7 +106,7 @@ BOOL mainmenu_select_hero_dialog( pfile_delete_save, pfile_ui_set_class_stats, &dlgresult, - gszHero, + &gszHero, &gnDifficulty)) app_fatal("Unable to display SelHeroSing"); client_info->initdata->bDiff = gnDifficulty; @@ -123,7 +123,7 @@ BOOL mainmenu_select_hero_dialog( pfile_ui_set_class_stats, &dlgresult, &hero_is_created, - gszHero)) { + &gszHero)) { app_fatal("Can't load multiplayer dialog"); } if (dlgresult == SELHERO_PREVIOUS) { diff --git a/Source/multi.cpp b/Source/multi.cpp index 38d5b79ac..fe232a192 100644 --- a/Source/multi.cpp +++ b/Source/multi.cpp @@ -732,7 +732,6 @@ BOOL NetInit(BOOL bSinglePlayer, BOOL *pfExitProgram) _SNETPROGRAMDATA ProgramData; _SNETUIDATA UiData; _SNETPLAYERDATA plrdata; - unsigned int len; while (1) { *pfExitProgram = FALSE; @@ -820,9 +819,9 @@ BOOL NetInit(BOOL bSinglePlayer, BOOL *pfExitProgram) glSeedTbl[i] = AdvanceRndSeed(); gnLevelTypeTbl[i] = InitLevelType(i); } - if (!SNetGetGameInfo(GAMEINFO_NAME, szPlayerName, 128, &len)) + if (!SNetGetGameInfo(GAMEINFO_NAME, szPlayerName, 128)) nthread_terminate_game("SNetGetGameInfo1"); - if (!SNetGetGameInfo(GAMEINFO_PASSWORD, szPlayerDescript, 128, &len)) + if (!SNetGetGameInfo(GAMEINFO_PASSWORD, szPlayerDescript, 128)) nthread_terminate_game("SNetGetGameInfo2"); return TRUE; diff --git a/Source/pfile.cpp b/Source/pfile.cpp index 043c4f294..dfa68c434 100644 --- a/Source/pfile.cpp +++ b/Source/pfile.cpp @@ -202,7 +202,7 @@ BOOL pfile_create_player_description(char *dst, DWORD len) UiSetupPlayerInfo(gszHero, &uihero, GAME_ID); if (dst != NULL && len) { - if (UiCreatePlayerDescription(&uihero, GAME_ID, desc) == 0) + if (UiCreatePlayerDescription(&uihero, GAME_ID, &desc) == 0) return FALSE; SStrCopy(dst, desc, len); } diff --git a/Source/plrmsg.cpp b/Source/plrmsg.cpp index db3505ddd..a60e4b570 100644 --- a/Source/plrmsg.cpp +++ b/Source/plrmsg.cpp @@ -30,16 +30,14 @@ void plrmsg_delay(BOOL delay) pMsg->time += plrmsg_ticks; } -char *ErrorPlrMsg(const char *pszMsg) +void ErrorPlrMsg(const char *pszMsg) { - char *result; _plrmsg *pMsg = &plr_msgs[plr_msg_slot]; plr_msg_slot = (plr_msg_slot + 1) & (PMSG_COUNT - 1); pMsg->player = MAX_PLRS; pMsg->time = SDL_GetTicks(); - result = strncpy(pMsg->str, pszMsg, sizeof(pMsg->str)); + strncpy(pMsg->str, pszMsg, sizeof(pMsg->str)); pMsg->str[sizeof(pMsg->str) - 1] = '\0'; - return result; } size_t EventPlrMsg(const char *pszFmt, ...) diff --git a/Source/plrmsg.h b/Source/plrmsg.h index bcfefc00c..2b51334e1 100644 --- a/Source/plrmsg.h +++ b/Source/plrmsg.h @@ -19,7 +19,7 @@ typedef struct _plrmsg { } _plrmsg; void plrmsg_delay(BOOL delay); -char *ErrorPlrMsg(const char *pszMsg); +void ErrorPlrMsg(const char *pszMsg); size_t EventPlrMsg(const char *pszFmt, ...); void SendPlrMsg(int pnum, const char *pszStr); void ClearPlrMsg(); diff --git a/SourceX/DiabloUI/diabloui.cpp b/SourceX/DiabloUI/diabloui.cpp index f34e7d419..fd2104790 100644 --- a/SourceX/DiabloUI/diabloui.cpp +++ b/SourceX/DiabloUI/diabloui.cpp @@ -449,14 +449,14 @@ char connect_plrinfostr[128]; char connect_categorystr[128]; void UiSetupPlayerInfo(char *infostr, _uiheroinfo *pInfo, DWORD type) { - SStrCopy(connect_plrinfostr, infostr, 128); + SStrCopy(connect_plrinfostr, infostr, sizeof(connect_plrinfostr)); char format[32] = ""; - strncpy(format, (char *)&type, 4); - strcat(format, " %d %d %d %d %d %d %d %d %d"); + *(DWORD *)format = type; + strcpy(&format[sizeof(DWORD)], " %d %d %d %d %d %d %d %d %d"); snprintf( connect_categorystr, - 128, + sizeof(connect_categorystr), format, pInfo->level, pInfo->heroclass, @@ -557,15 +557,15 @@ BOOL UiArtCallback(int game_type, unsigned int art_code, SDL_Color *pPalette, BY UNIMPLEMENTED(); } -BOOL UiCreatePlayerDescription(_uiheroinfo *info, DWORD mode, char *desc) +BOOL UiCreatePlayerDescription(_uiheroinfo *info, DWORD mode, char (*desc)[128]) { char format[32] = ""; - strncpy(format, (char *)&mode, 4); - strcat(format, " %d %d %d %d %d %d %d %d %d"); + *(DWORD *)format = mode; + strcpy(&format[sizeof(DWORD)], " %d %d %d %d %d %d %d %d %d"); snprintf( - desc, - 128, + *desc, + sizeof(*desc), format, info->level, info->heroclass, diff --git a/SourceX/DiabloUI/selhero.cpp b/SourceX/DiabloUI/selhero.cpp index 09d59ac5d..eb481feae 100644 --- a/SourceX/DiabloUI/selhero.cpp +++ b/SourceX/DiabloUI/selhero.cpp @@ -463,13 +463,13 @@ BOOL SelHero_GetHeroInfo(_uiheroinfo *pInfo) return true; } -BOOL UiSelHeroDialog( +static BOOL UiSelHeroDialog( BOOL (*fninfo)(BOOL (*fninfofunc)(_uiheroinfo *)), BOOL (*fncreate)(_uiheroinfo *), void (*fnstats)(unsigned int, _uidefaultstats *), BOOL (*fnremove)(_uiheroinfo *), int *dlgresult, - char *name) + char (*name)[16]) { bUIElementsLoaded = true; @@ -517,7 +517,7 @@ BOOL UiSelHeroDialog( } while (selhero_navigateYesNo); *dlgresult = selhero_result; - strncpy(name, selhero_heroInfo.name, 16 - 1); + snprintf(*name, sizeof(*name), selhero_heroInfo.name); UnloadScrollBar(); return true; @@ -529,7 +529,7 @@ BOOL UiSelHeroSingDialog( BOOL (*fnremove)(_uiheroinfo *), void (*fnstats)(unsigned int, _uidefaultstats *), int *dlgresult, - char *name, + char (*name)[16], int *difficulty) { selhero_isMultiPlayer = false; @@ -545,7 +545,7 @@ BOOL UiSelHeroMultDialog( void (*fnstats)(unsigned int, _uidefaultstats *), int *dlgresult, BOOL *hero_is_created, - char *name) + char (*name)[16]) { selhero_isMultiPlayer = true; return UiSelHeroDialog(fninfo, fncreate, fnstats, fnremove, dlgresult, name); diff --git a/SourceX/storm/storm.cpp b/SourceX/storm/storm.cpp index 98cc8148c..29b158e7a 100644 --- a/SourceX/storm/storm.cpp +++ b/SourceX/storm/storm.cpp @@ -288,7 +288,7 @@ bool getIniValue(const char *sectionName, const char *keyName, char *string, int if (dataSize) *dataSize = value.length(); - if (string) + if (string != NULL) strncpy(string, value.c_str(), stringSize); return true; @@ -777,10 +777,9 @@ void SErrSetLastError(DWORD dwErrCode) nLastError = dwErrCode; } -int SStrCopy(char *dest, const char *src, int max_length) +void SStrCopy(char *dest, const char *src, int max_length) { strncpy(dest, src, max_length); - return strlen(dest); } BOOL SFileSetBasePath(const char *path) diff --git a/SourceX/storm/storm_net.cpp b/SourceX/storm/storm_net.cpp index 3faeb4f50..dfc644e64 100644 --- a/SourceX/storm/storm_net.cpp +++ b/SourceX/storm/storm_net.cpp @@ -68,16 +68,14 @@ BOOL SNetDropPlayer(int playerid, DWORD flags) return dvlnet_inst->SNetDropPlayer(playerid, flags); } -BOOL SNetGetGameInfo(int type, void *dst, unsigned int length, unsigned int *byteswritten) +BOOL SNetGetGameInfo(int type, void *dst, unsigned int length) { switch (type) { case GAMEINFO_NAME: strncpy((char *)dst, gpszGameName, length); - *byteswritten = strlen(gpszGameName) + 1; break; case GAMEINFO_PASSWORD: strncpy((char *)dst, gpszGamePassword, length); - *byteswritten = strlen(gpszGamePassword) + 1; break; }