mirror of
https://codeberg.org/libreboot/lbmk.git
synced 2026-07-21 19:26:22 +02:00
libreboot-utils: extremely safe(ish) malloc usage
yes, a common thing in C programs is one or all of the following: * use after frees * double free (on non-NULL pointer) * over-writing currently used pointer (mem leak) i try to reduce the chance of this in my software, by running free() through a filter function, free_if_not_null, that returns if a function is being freed twice - because it sets NULL after freeing, but will only free if it's not null already. this patch adds two functions: smalloc and vmalloc, for strings and voids. using these makes the program abort if: * non-null pointer given for initialisation * pointer to pointer is null (of course) * size of zero given, for malloc (zero bytes) i myself was caught out by this change, prompting me to make the following fix in fs_dirname_basename() inside lib/file.c: - char *buf; + char *buf = NULL; Yes. Signed-off-by: Leah Rowe <leah@libreboot.org>
This commit is contained in:
@@ -72,13 +72,6 @@
|
||||
* or your program dies.
|
||||
*/
|
||||
|
||||
void *
|
||||
rmalloc(size_t *rval)
|
||||
{
|
||||
return if_err(rval == NULL, EFAULT) ?
|
||||
NULL : mkrstr(*rval = rsize(BUFSIZ));
|
||||
}
|
||||
|
||||
size_t
|
||||
rsize(size_t n)
|
||||
{
|
||||
@@ -120,17 +113,7 @@ void *
|
||||
mkrbuf(size_t n)
|
||||
{
|
||||
void *buf = NULL;
|
||||
|
||||
if (n == 0)
|
||||
err_no_cleanup(0, EPERM, "mkrbuf: zero-byte request");
|
||||
|
||||
if (n >= SIZE_MAX - 1)
|
||||
err_no_cleanup(0, EOVERFLOW, "integer overflow in mkrbuf");
|
||||
|
||||
if ((buf = malloc(n)) == NULL)
|
||||
err_no_cleanup(0, ENOMEM, "mkrbuf: malloc");
|
||||
|
||||
rset(buf, n);
|
||||
rset(vmalloc(&buf, n), n);
|
||||
return buf; /* basically malloc() but with rand */
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user