Fix uninitialized var errors
cover Fix uninitialized var errors
Hello,
It would be nice if we could set --Werror=uninitialized by default. Uninitialized variables can result in nasty compiler behaviour. You may expect it just to result in garbage data being used. The result being a crash. However as we compile with -O2 this is not the case. Often the compiler chooses values for the variable which will eliminate code. This often means the values are valid. The result being a program which appears to execute correctly on the surface. Meanwhile underneath it is doing very little.
In a lot of cases the compiler is simply confused and gives an error, but there is no undefined behaviour. This is often caused by tst_brk which is the source of much confusion. Most likely there should be two versions of tst_brk. One which never returns and is used by test authors and one which can return, but is usually only used in library functions which may be called from cleanup.
Richard Palethorpe (8):
ipc: Fix uninitialized var error by removing useless arg
ftest: Fix uninitialized var error
hotplug: Fix uninitialized var
dio: Fix uninitialized var error
ksmg01: Fix uninitialized var error
shmat01: Fix uninitialized var error
crash02: Save actual child PID instead of uninitialized variable
locktests: Fix uninitialized var error
libs/libltpipc/libipc.c | 4 +--- testcases/kernel/fs/ftest/ftest01.c | 3 +-- testcases/kernel/fs/ftest/ftest05.c | 3 +-- testcases/kernel/hotplug/memory_hotplug/commands.c | 7 +------ testcases/kernel/io/ltp-aiodio/dio_append.c | 2 +- testcases/kernel/io/ltp-aiodio/dio_sparse.c | 2 +- testcases/kernel/io/ltp-aiodio/dio_truncate.c | 4 ++-- testcases/kernel/logging/kmsg/kmsg01.c | 2 +- testcases/kernel/syscalls/ipc/semctl/semctl01.c | 4 +--- testcases/kernel/syscalls/ipc/shmat/shmat01.c | 2 +- testcases/misc/crash/crash02.c | 6 +++--- testcases/network/nfsv4/locks/locktests.c | 2 +- 12 files changed, 15 insertions(+), 26 deletions(-)
Signature · 1 line
2.31.1
PATCH 1/8 ipc: Fix uninitialized var error by removing useless arg
--- libs/libltpipc/libipc.c | 4 +--- testcases/kernel/syscalls/ipc/semctl/semctl01.c | 4 +--- 2 files changed, 2 insertions(+), 6 deletions(-)
libs/libltpipc/libipc.c +1 -3
@@ -122,13 +122,11 @@ void init_buf(MSGBUF * m_buf, int type, int size)*/ void rm_sema (int sem_id) { -union semun arr; -if (sem_id ==-1 ) {/* no semaphore to remove */ return ; } -if (semctl (sem_id,0 ,IPC_RMID , arr) ==-1 ) { +if (semctl (sem_id,0 ,IPC_RMID ) ==-1 ) {tst_resm (TINFO ,"WARNING: semaphore deletion failed." );tst_resm (TINFO ,"This could lead to IPC resource problems." );tst_resm (TINFO ,"id = %d" , sem_id);
testcases/kernel/syscalls/ipc/semctl/semctl01.c +1 -3
@@ -196,10 +196,8 @@ static void func_sall(void)static void func_sval (void ) { -int semv; -union semun arr; +int semv =SAFE_SEMCTL (sem_id,4 ,GETVAL ); - semv =SAFE_SEMCTL (sem_id,4 ,GETVAL , arr);if (semv !=INCVAL )tst_res (TFAIL ,"semaphore value is not what was set" );else
Signature · 1 line
2.31.1
PATCH 2/8 ftest: Fix uninitialized var error
In reply to Richard PalethorpeMark misc_intvl as const which allows the compiler to eliminate some confusing branches. This is a minimal change to fix the error.
--- testcases/kernel/fs/ftest/ftest01.c | 3 +-- testcases/kernel/fs/ftest/ftest05.c | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-)
testcases/kernel/fs/ftest/ftest01.c +1 -2
@@ -83,7 +83,7 @@ static void term(int sig);static int csize;/* chunk size */ static int iterations;/* # total iterations */ static int max_size;/* max file size */ -static int misc_intvl;/* for doing misc things; 0 ==> no */ +static const int misc_intvl =10 ;/* for doing misc things; 0 ==> no */ static int nchild;/* how many children */ static int fd;/* file descriptor used by child */ static int parent_pid;
@@ -139,7 +139,6 @@ static void setup(void) nchild =5 ; csize =K_2 ;/* should run with 1, 2, and 4 K sizes */ max_size =K_1 *K_1 ; - misc_intvl =10 ;if (sigset (SIGTERM , term) ==SIG_ERR ) {tst_brkm (TBROK |TERRNO ,NULL ,"sigset failed" );
testcases/kernel/fs/ftest/ftest05.c +1 -2
@@ -87,7 +87,7 @@ static void cleanup(void);static int csize;/* chunk size */ static int iterations;/* # total iterations */ static off64_t max_size;/* max file size */ -static int misc_intvl;/* for doing misc things; 0 ==> no */ +static const int misc_intvl =10 ;/* for doing misc things; 0 ==> no */ static int nchild;/* how many children */ static int fd;/* file descriptor used by child */ static int parent_pid;
@@ -147,7 +147,6 @@ static void setup(void) nchild =5 ; csize =K_2 ;/* should run with 1, 2, and 4 K sizes */ max_size =K_1 *K_1 ; - misc_intvl =10 ;if (sigset (SIGTERM , term) ==SIG_ERR ) { tst_brkm(TBROK | TERRNO, NULL,
Signature · 1 line
2.31.1
PATCH 3/8 hotplug: Fix uninitialized var
In reply to Richard PalethorpeThis assumes glctx is the correct thing to use as in every other function.
--- testcases/kernel/hotplug/memory_hotplug/commands.c | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-)
testcases/kernel/hotplug/memory_hotplug/commands.c +1 -6
@@ -438,16 +438,11 @@ static int get_arg_nodeid_list(char *args, unsigned int *list)*/ static int get_current_nodeid_list (unsigned int *fromids) { -/* -* FIXME (garrcoop): gcp is uninitialized and shortly hereafter used in -* an initialization statement..... UHHHHHHH... test writer fail? -*/ -glctx_t *gcp; +glctx_t *gcp = &glctx;nodemask_t my_allowed_nodes;int nr_nodes =0 , max_node = gcp->numa_max_node;int node; - gcp = &glctx; my_allowed_nodes =numa_get_membind_compat ();for (node =0 ; node <= max_node; ++node) {if (nodemask_isset (&my_allowed_nodes, node))
Signature · 1 line
2.31.1
PATCH 4/8 dio: Fix uninitialized var error
In reply to Richard PalethorpeThe compiler does not know that we will return if posix_memalign fails to set bufptr.
--- testcases/kernel/io/ltp-aiodio/dio_append.c | 2 +- testcases/kernel/io/ltp-aiodio/dio_sparse.c | 2 +- testcases/kernel/io/ltp-aiodio/dio_truncate.c | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-)
testcases/kernel/io/ltp-aiodio/dio_append.c +1 -1
@@ -78,7 +78,7 @@ int read_eof(char *filename)void dio_append (char *filename) {int fd; -void *bufptr; +void *bufptr =NULL ;int i;int w;
testcases/kernel/io/ltp-aiodio/dio_sparse.c +1 -1
@@ -57,7 +57,7 @@ int TST_TOTAL = 1;*/ int dio_sparse (int fd,int align,int writesize,int filesize,int offset) { -void *bufptr; +void *bufptr =NULL ;int i, w;TEST (posix_memalign (&bufptr, align, writesize));
testcases/kernel/io/ltp-aiodio/dio_truncate.c +2 -2
@@ -69,7 +69,7 @@ int dio_read(char *filename) {int fd;int r; -void *bufptr; +void *bufptr =NULL ;TEST (posix_memalign (&bufptr,4096 ,64 *1024 ));if (TEST_RETURN ) {
@@ -105,7 +105,7 @@ int dio_read(char *filename)void dio_append (char *filename,int fill) {int fd; -void *bufptr; +void *bufptr =NULL ;int i;int w;
Signature · 1 line
2.31.1
PATCH 5/8 ksmg01: Fix uninitialized var error
In reply to Richard PalethorpeThe compiler is confused by tst_brk which it thinks will return. Setting first_seqno silences the error.
--- testcases/kernel/logging/kmsg/kmsg01.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
testcases/kernel/logging/kmsg/kmsg01.c +1 -1
@@ -384,7 +384,7 @@ static void test_messages_overwritten(void) {int i, fd;char msg[MAX_MSGSIZE ]; -unsigned long first_seqno, seqno; +unsigned long first_seqno =0 , seqno;char filler_str[] ="<7>" MSG_PREFIX "FILLER MESSAGE TO OVERWRITE OTHERS\n" ;/* Keep injecting messages until we overwrite first one.
Signature · 1 line
2.31.1
PATCH 6/8 shmat01: Fix uninitialized var error
In reply to Richard PalethorpeThe compiler is confused by SAFE_WAITPID which it thinks will return after an error. Setting status to zero silences the error.
--- testcases/kernel/syscalls/ipc/shmat/shmat01.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
testcases/kernel/syscalls/ipc/shmat/shmat01.c +1 -1
@@ -82,7 +82,7 @@ static void verify_shmat(unsigned int n) {int *addr;pid_t pid; -int status; +int status =0 ;struct shmid_ds buf;struct test_case_t *tc = &tcases[n];
Signature · 1 line
2.31.1
PATCH 7/8 crash02: Save actual child PID instead of uninitialized variable
In reply to Richard Palethorpe--- testcases/misc/crash/crash02.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
testcases/misc/crash/crash02.c +3 -3
@@ -197,9 +197,8 @@ void monitor_fcn(int sig)void badboy_fork (void ) {int status, pid; -pid_t child; - child =fork (); - badboy_pid = status; +pid_t child =fork (); +switch (child) {case -1 :perror ("fork" );
@@ -211,6 +210,7 @@ void badboy_fork(void)#endif exit (0 );default : + badboy_pid = child;if (verbose_level >3 )printf ("badboy pid = %d\n" , badboy_pid);
Signature · 1 line
2.31.1
PATCH 8/8 locktests: Fix uninitialized var error
In reply to Richard PalethorpeThe compiler does not appear to understand that the initial state (SELECT) will always initialize tLock in the first iteration. Initializing tLock to zero suppresses the error.
--- testcases/network/nfsv4/locks/locktests.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
testcases/network/nfsv4/locks/locktests.c +1 -1
@@ -580,7 +580,7 @@ int master(void)char dbg[16 ];#endif struct flock request; -struct s_test tLock; +struct s_test tLock = {0 };enum state_t state;int offset;/* A test sentence written in the file */
Signature · 1 line
2.31.1
cover Fix uninitialized var errors
In reply to Richard PalethorpeHi!
Quoted 6 lines
In a lot of cases the compiler is simply confused and gives an error, but there is no undefined behaviour. This is often caused by
tst_brkwhich is the source of much confusion. Most likely there should be two versions oftst_brk. One which never returns and is used by test authors and one which can return, but is usually only used in library functions which may be called from cleanup.
Having two tst_brk() wouldn't solve much since all SAFE_MACROS() still needs to use the one that may return.
So as far as I can tell it's easier to initialize variables to 0 in the few cases where compiler ends up confused.
Anyways whole patchset pushed, thanks.
Signature · 2 lines
Cyril Hrubis [email protected]