mirror of
https://github.com/SELinuxProject/selinux
synced 2025-01-19 20:00:52 +00:00
libselinux: matchpathcon: free memory on realloc failure
In case `realloc()` fails and returns NULL, free the passed array, instead of just setting the size helper variables to 0. Also free the string contents in `free_array_elts()` of the array `con_array`, instead of just the array of pointers. Found by cppcheck. src/matchpathcon.c:86:4: error: Common realloc mistake: 'con_array' nulled but not freed upon failure [memleakOnRealloc] con_array = (char **)realloc(con_array, sizeof(char*) * ^ Signed-off-by: Christian Göttsche <cgzones@googlemail.com>
This commit is contained in:
parent
bc0a0327ca
commit
65f1ccbecc
@ -78,17 +78,30 @@ static pthread_once_t once = PTHREAD_ONCE_INIT;
|
||||
static pthread_key_t destructor_key;
|
||||
static int destructor_key_initialized = 0;
|
||||
|
||||
static void free_array_elts(void)
|
||||
{
|
||||
int i;
|
||||
for (i = 0; i < con_array_used; i++)
|
||||
free(con_array[i]);
|
||||
free(con_array);
|
||||
|
||||
con_array_size = con_array_used = 0;
|
||||
con_array = NULL;
|
||||
}
|
||||
|
||||
static int add_array_elt(char *con)
|
||||
{
|
||||
char **tmp;
|
||||
if (con_array_size) {
|
||||
while (con_array_used >= con_array_size) {
|
||||
con_array_size *= 2;
|
||||
con_array = (char **)realloc(con_array, sizeof(char*) *
|
||||
tmp = (char **)realloc(con_array, sizeof(char*) *
|
||||
con_array_size);
|
||||
if (!con_array) {
|
||||
con_array_size = con_array_used = 0;
|
||||
if (!tmp) {
|
||||
free_array_elts();
|
||||
return -1;
|
||||
}
|
||||
con_array = tmp;
|
||||
}
|
||||
} else {
|
||||
con_array_size = 1000;
|
||||
@ -105,13 +118,6 @@ static int add_array_elt(char *con)
|
||||
return con_array_used++;
|
||||
}
|
||||
|
||||
static void free_array_elts(void)
|
||||
{
|
||||
con_array_size = con_array_used = 0;
|
||||
free(con_array);
|
||||
con_array = NULL;
|
||||
}
|
||||
|
||||
void set_matchpathcon_invalidcon(int (*f) (const char *p, unsigned l, char *c))
|
||||
{
|
||||
myinvalidcon = f;
|
||||
|
Loading…
Reference in New Issue
Block a user