From 4baa7dfbeea7a01baf860f83253ca55e87403331 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-Fran=C3=A7ois=20DEL=20NERO?= Date: Mon, 13 Nov 2023 19:48:56 +0100 Subject: [PATCH] WIP : Static analysis fixes. --- lib_jtag_core/src/bsdl_parser/bsdl_loader.c | 13 ++++++-- lib_jtag_core/src/dbg_logs.c | 7 +++-- .../linux_gpio_jtag/linux_gpio_jtag_drv.c | 18 +++++++---- lib_jtag_core/src/jtag_core.c | 3 ++ lib_jtag_core/src/os_interface/fs.c | 13 +++++--- lib_jtag_core/src/os_interface/os_interface.c | 30 ++++++++++++++++++- lib_jtag_core/src/os_interface/os_interface.h | 1 + lib_jtag_core/src/script/env.c | 2 +- lib_jtag_core/src/script/script.c | 17 ++++++----- 9 files changed, 80 insertions(+), 24 deletions(-) diff --git a/lib_jtag_core/src/bsdl_parser/bsdl_loader.c b/lib_jtag_core/src/bsdl_parser/bsdl_loader.c index b5612c0..9e55b63 100644 --- a/lib_jtag_core/src/bsdl_parser/bsdl_loader.c +++ b/lib_jtag_core/src/bsdl_parser/bsdl_loader.c @@ -23,6 +23,7 @@ * @author Jean-François DEL NERO */ +#include #include #include #include @@ -37,6 +38,8 @@ #include "../natsort/strnatcmp.h" +#include "../os_interface/os_interface.h" + #include "../dbg_logs.h" #define DEBUG 1 @@ -870,8 +873,9 @@ int get_pins_list(jtag_core * jc,jtag_bsdl * bsdl_desc,char ** lines) { char digistr[32]; snprintf((char*)digistr,sizeof(digistr),"(%d)",tmp_start); - strncat((char*)&bsdl_desc->pins_list[number_of_pins].pinname,digistr,sizeof(((pin_ctrl *)0)->pinname)-1); + genos_strndstcat((char*)&bsdl_desc->pins_list[number_of_pins].pinname,digistr,sizeof(((pin_ctrl *)0)->pinname)); } + bsdl_desc->pins_list[number_of_pins].pinname[sizeof(((pin_ctrl *)0)->pinname)-1] = '\0'; bsdl_desc->pins_list[number_of_pins].pintype = tmp_type; number_of_pins++; @@ -1299,7 +1303,9 @@ jtag_bsdl * load_bsdlfile(jtag_core * jc,char *filename) /////////////////////// // copy the entity name & the file name - strncpy(bsdl->entity_name,entityname,sizeof(((jtag_bsdl *)0)->entity_name) - 1); + strncpy(bsdl->entity_name,entityname,sizeof(((jtag_bsdl *)0)->entity_name)); + bsdl->entity_name[ sizeof(((jtag_bsdl *)0)->entity_name) - 1 ] = '\0'; + i = strlen(filename); while(i && filename[i] != '\\') { @@ -1309,7 +1315,8 @@ jtag_bsdl * load_bsdlfile(jtag_core * jc,char *filename) if(filename[i] == '\\') i++; - strncpy(bsdl->src_filename,&filename[i],sizeof(bsdl->src_filename)-1); + strncpy(bsdl->src_filename,&filename[i],sizeof(bsdl->src_filename)); + bsdl->src_filename[ sizeof(bsdl->src_filename) - 1 ] = '0'; /////////////////////// // Extract the chip ID diff --git a/lib_jtag_core/src/dbg_logs.c b/lib_jtag_core/src/dbg_logs.c index 1f77ab8..da0b7aa 100644 --- a/lib_jtag_core/src/dbg_logs.c +++ b/lib_jtag_core/src/dbg_logs.c @@ -22,7 +22,7 @@ * @brief logs/debug output * @author Jean-François DEL NERO */ - +#include #include #include #include @@ -33,6 +33,8 @@ #include "jtag_core_internal.h" #include "jtag_core.h" +#include "./os_interface/os_interface.h" + #include "dbg_logs.h" int jtagcore_logs_printf(jtag_core * jc,int MSGTYPE,char * chaine, ...) @@ -73,7 +75,8 @@ int jtagcore_logs_printf(jtag_core * jc,int MSGTYPE,char * chaine, ...) } vsprintf(tmp_msg2,chaine,marker); - strncat(tmp_msg,tmp_msg2,sizeof(tmp_msg) - ( strlen(tmp_msg) + 1 ) ); + genos_strndstcat(tmp_msg, tmp_msg2, sizeof(tmp_msg) ); + tmp_msg[sizeof(tmp_msg) - 1] = '\0'; print_callback(tmp_msg); diff --git a/lib_jtag_core/src/drivers/linux_gpio_jtag/linux_gpio_jtag_drv.c b/lib_jtag_core/src/drivers/linux_gpio_jtag/linux_gpio_jtag_drv.c index a013ea7..d5f4485 100644 --- a/lib_jtag_core/src/drivers/linux_gpio_jtag/linux_gpio_jtag_drv.c +++ b/lib_jtag_core/src/drivers/linux_gpio_jtag/linux_gpio_jtag_drv.c @@ -23,6 +23,7 @@ * @author Jean-François DEL NERO */ +#include #include #include #include @@ -39,6 +40,8 @@ #include "../../bsdl_parser/bsdl_loader.h" +#include "../../os_interface/os_interface.h" + #include "../../dbg_logs.h" char linux_gpio_base[512]; @@ -163,8 +166,9 @@ int drv_LinuxGPIO_Detect(jtag_core * jc) { jtagcore_getEnvVar( jc, "PROBE_GPIO_LINUX_BASE_FOLDER", (char*)&linux_gpio_base); - strncpy(tmp_str,linux_gpio_base,sizeof(tmp_str)-1); - strncat(tmp_str,"/export",sizeof(tmp_str)-1); + strncpy(tmp_str,linux_gpio_base,sizeof(tmp_str)); + genos_strndstcat(tmp_str,"/export",sizeof(tmp_str)); + tmp_str[sizeof(tmp_str) - 1] = '\0'; f = fopen(tmp_str,"rb"); if(f) @@ -207,7 +211,8 @@ static int setdirGPIO(char * path_base,int pin,int dir) strncpy(tmp_str,path_base,sizeof(tmp_str)-1); snprintf(tmp_str2,sizeof(tmp_str2)-1,"/gpio%d/direction",pin); - strncat(tmp_str,tmp_str2,sizeof(tmp_str)-1); + genos_strndstcat(tmp_str,tmp_str2,sizeof(tmp_str)); + tmp_str[sizeof(tmp_str) - 1] = '\0'; f = fopen(tmp_str,"w"); if(!f) @@ -249,8 +254,11 @@ int drv_LinuxGPIO_Init(jtag_core * jc, int sub_drv,char * params) { gpio_var_names[i].gpio_num = jtagcore_getEnvVarValue( jc, (char*)gpio_var_names[i].name); - strncpy(tmp_str,(char*)gpio_var_names[i].name,sizeof(tmp_str)-1); - strncat(tmp_str,"_INVERT_POLARITY",sizeof(tmp_str)-1); + strncpy(tmp_str,(char*)gpio_var_names[i].name,sizeof(tmp_str) - 1); + tmp_str[sizeof(tmp_str) - 1] = '\0'; + + genos_strndstcat(tmp_str,"_INVERT_POLARITY",sizeof(tmp_str)); + tmp_str[sizeof(tmp_str) - 1] = '\0'; gpio_var_names[i].negate_polarity = jtagcore_getEnvVarValue( jc, (char*)tmp_str); diff --git a/lib_jtag_core/src/jtag_core.c b/lib_jtag_core/src/jtag_core.c index d160828..d8fd8ec 100644 --- a/lib_jtag_core/src/jtag_core.c +++ b/lib_jtag_core/src/jtag_core.c @@ -445,7 +445,10 @@ int jtagcore_get_pin_properties(jtag_core * jc, int device,int pin,char * pinnam type_code = 0x00; if (pinname) + { strncpy(pinname, bsdl_file->pins_list[pin].pinname, maxsize); + pinname[maxsize - 1] = '\0'; + } if (type) { diff --git a/lib_jtag_core/src/os_interface/fs.c b/lib_jtag_core/src/os_interface/fs.c index 264a999..2cac7ef 100644 --- a/lib_jtag_core/src/os_interface/fs.c +++ b/lib_jtag_core/src/os_interface/fs.c @@ -363,7 +363,7 @@ void * genos_find_first_file(char *folder, char *file, filefoundinfo* fileinfo) { fileinfo->isdirectory = 0; fileinfo->size = fileStat.st_size; - strncpy(fileinfo->filename,genos_getfilenamebase(folder,NULL,SYS_PATH_TYPE),FILEFOUND_NAMESIZE - 1); + strncpy(fileinfo->filename,genos_getfilenamebase(folder,NULL,SYS_PATH_TYPE),FILEFOUND_NAMESIZE); fileinfo->filename[FILEFOUND_NAMESIZE - 1] = 0; return (void*)-1; @@ -395,7 +395,8 @@ void * genos_find_first_file(char *folder, char *file, filefoundinfo* fileinfo) fileinfo->size=fileStat.st_size; - strncpy(fileinfo->filename,d->d_name,256); + strncpy(fileinfo->filename,d->d_name,FILEFOUND_NAMESIZE); + fileinfo->filename[FILEFOUND_NAMESIZE - 1] = '\0'; free(tmpstr); return (void*)dir; @@ -481,7 +482,8 @@ int genos_find_next_file(void* handleff, char *folder, char *file, filefoundinfo fileinfo->isdirectory=0; fileinfo->size=fileStat.st_size; - strncpy(fileinfo->filename,d->d_name,256); + strncpy(fileinfo->filename,d->d_name,FILEFOUND_NAMESIZE); + fileinfo->filename[FILEFOUND_NAMESIZE - 1] = '\0'; ret = 1; free(tmpstr); @@ -543,7 +545,10 @@ char * genos_getcurrentdirectory(char *currentdirectory,int buffersize) #else - strncpy(currentdirectory,"./",buffersize-1); + + strncpy(currentdirectory,"./",buffersize); + currentdirectory[buffersize-1] = '\0'; + return currentdirectory; #endif diff --git a/lib_jtag_core/src/os_interface/os_interface.c b/lib_jtag_core/src/os_interface/os_interface.c index 133e2d1..4226e6e 100644 --- a/lib_jtag_core/src/os_interface/os_interface.c +++ b/lib_jtag_core/src/os_interface/os_interface.c @@ -320,7 +320,7 @@ int genos_createthread(jtag_core* jtag_ctx,void* hwcontext,THREADFUNCTION thread //threadinitptr->hwcontext=hwcontext; print_callback = jtag_ctx->jtagcore_print_callback; - + ret = pthread_create(&threadid, &threadattrib,ThreadProc, threadinitptr); if(ret) { @@ -544,6 +544,10 @@ int genos_checkfileext(char * path,char *ext,int type) if(path && ext) { + pathext[0] = '\0'; + srcext[0] = ' '; + srcext[1] = '\0'; + if( ( strlen(genos_getfilenameext(path,0,type)) < 16 ) && ( strlen(ext) < 16 )) { genos_getfilenameext(path,(char*)&pathext,type); @@ -621,3 +625,27 @@ int snprintf(char *s, size_t n, const char *fmt, ...) #endif #endif + +char * genos_strndstcat( char *dest, const char *src, size_t maxdestsize ) +{ + int i,j; + + i = 0; + while( ( i < maxdestsize ) && dest[i] ) + { + i++; + } + + if( !dest[i] ) + { + j = 0; + while( ( i < maxdestsize ) && src[j] ) + { + dest[i] = src[j]; + i++; + j++; + } + } + + return dest; +} diff --git a/lib_jtag_core/src/os_interface/os_interface.h b/lib_jtag_core/src/os_interface/os_interface.h index f1a191d..a6bf97b 100644 --- a/lib_jtag_core/src/os_interface/os_interface.h +++ b/lib_jtag_core/src/os_interface/os_interface.h @@ -75,6 +75,7 @@ void genos_destroycriticalsection( jtag_core* jtag_ctx, unsigned char id ); #endif char * genos_strupper( char * str ); char * genos_strlower( char * str ); +char * genos_strndstcat( char *dest, const char *src, size_t maxdestsize ); /////////////// File functions //////////////// diff --git a/lib_jtag_core/src/script/env.c b/lib_jtag_core/src/script/env.c index 4550de9..3f0df61 100644 --- a/lib_jtag_core/src/script/env.c +++ b/lib_jtag_core/src/script/env.c @@ -223,7 +223,7 @@ envvar_entry * duplicate_env_vars(envvar_entry * src) i = 0; // count entry - while(src[i].name) + while(src[i].name && i < 65536) { i++; } diff --git a/lib_jtag_core/src/script/script.c b/lib_jtag_core/src/script/script.c index e5c059d..68fc41e 100644 --- a/lib_jtag_core/src/script/script.c +++ b/lib_jtag_core/src/script/script.c @@ -971,7 +971,7 @@ static int cmd_print( script_ctx * ctx, char * line) char str[DEFAULT_BUFLEN*2]; char * ptr; - str[0] = 0; + str[0] = '\0'; j = 1; do @@ -990,23 +990,24 @@ static int cmd_print( script_ctx * ctx, char * line) { if(tmp_str[0] != '$') { - strncat((char*)str,tmp_str,sizeof(str) - 1); - strncat((char*)str," ",sizeof(str) - 1); + genos_strndstcat((char*)str,tmp_str,sizeof(str)); + genos_strndstcat((char*)str," ",sizeof(str)); + str[sizeof(str) - 1] = '\0'; } else { ptr = getEnvVar( *((envvar_entry **)ctx->env), &tmp_str[1], NULL); if( ptr ) { - strncat((char*)str,ptr,sizeof(str) - 1); - strncat((char*)str," ",sizeof(str) - 1); - + genos_strndstcat((char*)str,ptr,sizeof(str)); + genos_strndstcat((char*)str," ",sizeof(str)); } else { - strncat((char*)str,tmp_str,sizeof(str) - 1); - strncat((char*)str," ",sizeof(str) - 1); + genos_strndstcat((char*)str,tmp_str,sizeof(str)); + genos_strndstcat((char*)str," ",sizeof(str)); } + str[sizeof(str) - 1] = '\0'; } } }