libsepol: do not leak memory if list_prepend fails

When list_prepend() returns an error, it always means it failed to
allocate some memory and does not hold any reference to its argument
data. This argument needs to be freed by the caller in order to prevent
a memory leak.

While reviewing list_prepend() callers, I spend quite some time
understanding why typealiases_gather_map() does not need to strdup(key)
or free(key) when calling list_prepend(..., key) even though "key" comes
from pdb->p_types.table: because typealias_list_destroy() does not free
the inserted items. Add a comment to make this clearer in the code.

Signed-off-by: Nicolas Iooss <nicolas.iooss@m4x.org>
This commit is contained in:
Nicolas Iooss 2018-05-26 19:10:59 +02:00
parent 57e1ab328c
commit 2dc6406a30
No known key found for this signature in database
GPG Key ID: C191415F340DAAA0

View File

@ -298,6 +298,8 @@ static int roles_gather_map(char *key, void *data, void *args)
role_node->role = role;
rc = list_prepend((struct list *)args, role_node);
if (rc != 0)
free(role_node);
return rc;
}
@ -344,6 +346,11 @@ static int typealiases_gather_map(char *key, void *data, void *arg)
goto exit;
}
}
/* As typealias_lists[scope_id] does not hold the
* ownership of its items (typealias_list_destroy does
* not free the list items), "key" does not need to be
* strdup'ed before it is inserted in the list.
*/
list_prepend(typealias_lists[scope_id], key);
}
}