Skip to content

Commit

Permalink
WIP : Static analysis fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
jfdelnero committed Nov 13, 2023
1 parent 1559379 commit 4baa7df
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 24 deletions.
13 changes: 10 additions & 3 deletions lib_jtag_core/src/bsdl_parser/bsdl_loader.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
* @author Jean-François DEL NERO <[email protected]>
*/

#include <stdint.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
Expand All @@ -37,6 +38,8 @@

#include "../natsort/strnatcmp.h"

#include "../os_interface/os_interface.h"

#include "../dbg_logs.h"
#define DEBUG 1

Expand Down Expand Up @@ -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++;
Expand Down Expand Up @@ -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] != '\\')
{
Expand All @@ -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
Expand Down
7 changes: 5 additions & 2 deletions lib_jtag_core/src/dbg_logs.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* @brief logs/debug output
* @author Jean-François DEL NERO <[email protected]>
*/

#include <stdint.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
Expand All @@ -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, ...)
Expand Down Expand Up @@ -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);

Expand Down
18 changes: 13 additions & 5 deletions lib_jtag_core/src/drivers/linux_gpio_jtag/linux_gpio_jtag_drv.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
* @author Jean-François DEL NERO <[email protected]>
*/

#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
Expand All @@ -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];
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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);

Expand Down
3 changes: 3 additions & 0 deletions lib_jtag_core/src/jtag_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
13 changes: 9 additions & 4 deletions lib_jtag_core/src/os_interface/fs.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down
30 changes: 29 additions & 1 deletion lib_jtag_core/src/os_interface/os_interface.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
1 change: 1 addition & 0 deletions lib_jtag_core/src/os_interface/os_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 ////////////////

Expand Down
2 changes: 1 addition & 1 deletion lib_jtag_core/src/script/env.c
Original file line number Diff line number Diff line change
Expand Up @@ -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++;
}
Expand Down
17 changes: 9 additions & 8 deletions lib_jtag_core/src/script/script.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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';
}
}
}
Expand Down

0 comments on commit 4baa7df

Please sign in to comment.