From 2c91eda5bc38f468bf1428ebd5bdaee5ef149102 Mon Sep 17 00:00:00 2001 From: Kp Date: Mon, 6 May 2019 00:36:16 +0000 Subject: [PATCH] Fix various gcc-9 -Wformat-truncation warnings --- common/main/cmd.cpp | 6 ++++-- similar/main/piggy.cpp | 4 ++-- similar/main/titles.cpp | 9 ++++----- similar/misc/physfsx.cpp | 10 ++++++---- 4 files changed, 16 insertions(+), 13 deletions(-) diff --git a/common/main/cmd.cpp b/common/main/cmd.cpp index 3b1f7a1d7..07a9e54b5 100644 --- a/common/main/cmd.cpp +++ b/common/main/cmd.cpp @@ -151,7 +151,8 @@ static void cmd_parse(char *input) /* Strip leading spaces */ while( isspace(*input) ) { ++input; } - strncpy( buffer, input, sizeof(buffer) ); + buffer[sizeof(buffer) - 1] = 0; + strncpy(buffer, input, sizeof(buffer) - 1); //printf("lead strip \"%s\"\n",buffer); l = strlen(buffer); @@ -343,7 +344,8 @@ static void cmd_alias(unsigned long argc, const char *const *const argv) if (i.second) { alias = (i.first->second = make_unique()).get(); - strncpy(alias->name, argv[1], sizeof(alias->name)); + alias->name[sizeof(alias->name) - 1] = 0; + strncpy(alias->name, argv[1], sizeof(alias->name) - 1); } alias->value.reset(d_strdup(buf)); } diff --git a/similar/main/piggy.cpp b/similar/main/piggy.cpp index 415c44b29..4118edc84 100644 --- a/similar/main/piggy.cpp +++ b/similar/main/piggy.cpp @@ -661,7 +661,7 @@ void piggy_init_pigfile(const char *filename) #endif } - strncpy(Current_pigfile,filename,sizeof(Current_pigfile)); + strncpy(Current_pigfile, filename, sizeof(Current_pigfile) - 1); N_bitmaps = PHYSFSX_readInt(Piggy_fp); @@ -734,7 +734,7 @@ void piggy_new_pigfile(char *pigname) Piggy_bitmap_cache_next = 0; //free up cache - strncpy(Current_pigfile,pigname,sizeof(Current_pigfile)); + strncpy(Current_pigfile, pigname, sizeof(Current_pigfile) - 1); Piggy_fp = PHYSFSX_openReadBuffered(pigname); diff --git a/similar/main/titles.cpp b/similar/main/titles.cpp index 7e6e87cdd..ea71e77f7 100644 --- a/similar/main/titles.cpp +++ b/similar/main/titles.cpp @@ -566,7 +566,8 @@ static void briefing_init(briefing *br, short level_num) br->level_num = 0; // for start of game stuff br->cur_screen = 0; - strncpy(br->background_name, DEFAULT_BRIEFING_BKG, sizeof(br->background_name)); + br->background_name[sizeof(br->background_name) - 1] = 0; + strncpy(br->background_name, DEFAULT_BRIEFING_BKG, sizeof(br->background_name) - 1); #if defined(DXX_BUILD_DESCENT_II) br->robot_playing = 0; #endif @@ -1308,8 +1309,7 @@ static int load_briefing_screen(grs_canvas &canvas, briefing *const br, const ch if (!PHYSFSX_exists(fname2,1)) snprintf(fname2, sizeof(char)*PATH_MAX, "%s", fname); } - if (d_stricmp(br->background_name, fname2)) - strncpy (br->background_name,fname2, sizeof(br->background_name)); + strncpy(br->background_name, fname2, sizeof(br->background_name) - 1); if ((!d_stricmp(fname2, "brief02.pcx") || !d_stricmp(fname2, "brief02h.pcx")) && cheats.baldguy && (bald_guy_load("btexture.xxx", &br->background, gr_palette) == PCX_ERROR_NONE)) @@ -1345,8 +1345,7 @@ static int load_briefing_screen(grs_canvas &canvas, briefing *const br, const ch int pcx_error; free_briefing_screen(br); - if (d_stricmp(br->background_name, fname)) - strncpy (br->background_name,fname, sizeof(br->background_name)); + strncpy(br->background_name, fname, sizeof(br->background_name) - 1); if ((pcx_error = pcx_read_bitmap(fname, br->background, gr_palette))!=PCX_ERROR_NONE) Error( "Error loading briefing screen <%s>, PCX load error: %s (%i)\n",fname, pcx_errormsg(pcx_error), pcx_error); diff --git a/similar/misc/physfsx.cpp b/similar/misc/physfsx.cpp index 087bd268b..8cbf3a3c4 100644 --- a/similar/misc/physfsx.cpp +++ b/similar/misc/physfsx.cpp @@ -150,15 +150,17 @@ bool PHYSFSX_init(int argc, char *argv[]) if (path[0] == '~') // yes, this tilde can be put before non-unix paths. { const char *home = PHYSFS_getUserDir(); - - strcpy(fullPath, home); // prepend home to the path path++; + // prepend home to the path if (*path == *PHYSFS_getDirSeparator()) path++; - strncat(fullPath, path, PATH_MAX + 5 - strlen(home)); + snprintf(fullPath, sizeof(fullPath), "%s%s", home, path); } else - strncpy(fullPath, path, PATH_MAX + 5); + { + fullPath[sizeof(fullPath) - 1] = 0; + strncpy(fullPath, path, sizeof(fullPath) - 1); + } PHYSFS_setWriteDir(fullPath); if (!(writedir = PHYSFS_getWriteDir()))