2007-02-10 02:14:55

by Brian Behlendorf

[permalink] [raw]
Subject: e2fsprogs coverity patch <cid-5.diff>

Lawrence Livermore National Labs recently ran the source code
analysis tool Coverity over the e2fsprogs-1.39 source to see
if it would identify any significant bugs. The analysis
turned up 38 mostly minor issues which are enumerated here
with patches. We went through and resolved these issues
but would love to see these mostly minor changes reviewed
and commited upstream.

Thanks,
Brian Behlendorf <[email protected]>, and
Herb Wartens <[email protected]>

-----------------------------------------------------------------------------
Coverity ID: 5: Forward NULL

array is initially set to NULL, so it is possible that readdir() will return
NULL and leave array set to NULL. Thus we do need to check if array is NULL or
check if num != 0.

There is definitely space allocated to array in the normal case. It calls
realloc which since array is NULL the first time through will behave like
malloc. The other times we will resize the array to be 11 larger.

It should be safe to run qsort on array since num should be set to 0 so nothing
will be sorted. It should also be safe to set *ret_array = array. The array
passed in should just be NULL at this point.

Index: e2fsprogs+chaos/e2fsck/profile.c
===================================================================
--- e2fsprogs+chaos.orig/e2fsck/profile.c
+++ e2fsprogs+chaos/e2fsck/profile.c
@@ -280,7 +280,8 @@ static errcode_t get_dirlist(const char
array[num++] = fn;
}
qsort(array, num, sizeof(char *), compstr);
- array[num++] = 0;
+ if(array)
+ array[num++] = 0;
*ret_array = array;
closedir(dir);
return 0;


2007-03-19 17:02:54

by Theodore Ts'o

[permalink] [raw]
Subject: Re: e2fsprogs coverity patch <cid-05.diff>

On Fri, Feb 09, 2007 at 06:11:41PM -0800, Brian D. Behlendorf wrote:
> array is initially set to NULL, so it is possible that readdir() will return
> NULL and leave array set to NULL. Thus we do need to check if array is NULL or
> check if num != 0.
>
> Coverity ID: 5: Forward NULL

This wasn't the only bug; it turns out that if /etc/e2fsck.conf or
/etc/mke2fs.conf is an empty directory, then your patch would fix this
segfault, only for it to segfault later in profile_init(). I'll fix
this before I commit.

- Ted