From f784d1b8aa24987bd37111303863a69acdfba476 Mon Sep 17 00:00:00 2001 From: JackCarterSmith Date: Wed, 7 Sep 2022 20:19:08 +0200 Subject: [PATCH] Safe strncpy method to translate HMT file --- RSEModel/src/RSEModel.c | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/RSEModel/src/RSEModel.c b/RSEModel/src/RSEModel.c index cd5da79..3423672 100644 --- a/RSEModel/src/RSEModel.c +++ b/RSEModel/src/RSEModel.c @@ -12,9 +12,13 @@ #include #if defined(_WIN32) #include + #include + #define F_OK 0 + #define access _access #else #include #include + #include #endif #include "options.h" #include @@ -22,6 +26,8 @@ #include "model_export.h" #include "lib_interface.h" +#define MAX_STR_VAR 1024 + /* * Internal functions declarations @@ -75,7 +81,7 @@ static unsigned int mainProcess(int args_cnt, char* args_value[], T_PROG_OPTIONS unsigned short file_index; RSPMODEL_PARAMETERS libParams; T_RSPMODEL_HOB* hobStruct = NULL; - char hmt_filename[1024]; + char hmt_filename[MAX_STR_VAR] = {0}; void* hmtStruct = NULL; unsigned int i; @@ -98,10 +104,19 @@ static unsigned int mainProcess(int args_cnt, char* args_value[], T_PROG_OPTIONS // If Texture module loaded, process HMT file for mtl naming (doesn't extract texture!) if (p_opts->texture_module) { - hmtStruct = RSPTexture_createHMT(); + if (strlen(args_value[file_index]) >= MAX_STR_VAR) return RSPLIB_ERROR_GENERIC; + strncpy(hmt_filename, args_value[file_index], strlen(args_value[file_index]) - 3); + hmt_filename[strlen(args_value[file_index]) - 1] = 0; //C6053 fix strncat(hmt_filename, "HMT", strlen(args_value[file_index])); - RSPTexture_processHMTFile(hmtStruct, hmt_filename, 0); + + if (access(hmt_filename, F_OK) == 0) { + hmtStruct = RSPTexture_createHMT(); + RSPTexture_processHMTFile(hmtStruct, hmt_filename, 0); + } else { + printf("[ERR] HMT file '%s' not found for mtl processing!\n", hmt_filename); + return RSPLIB_ERROR_GENERIC; + } } } else return RSPLIB_ERROR_MEMORY;