Add support for cppcheck 2.17.0

cppcheck 2.17.0 adds checks that a NULL pointer returned from malloc() and
calloc() is not used.

We do this quite a lot.

I've addressed this by adding functions g_malloc_nofail() and
g_calloc_nofail() which either allocate memory or abort.
functions are now called in places where we are not making these
checks.

Many of these checks are in test programs or example programs.

I've modified the list16 module to handle out-of-memory conditions.
This commit is contained in:
matt335672
2025-02-24 11:36:02 +00:00
parent 3446614961
commit 2f46ef27a2
9 changed files with 190 additions and 62 deletions
+9 -8
View File
@@ -9,6 +9,7 @@
#include "libmem.h"
#include "log.h"
#include "os_calls.h"
#define ALIGN_BY 32
#define ALIGN_BY_M1 (ALIGN_BY - 1)
@@ -78,12 +79,12 @@ libmem_init(unsigned int addr, int bytes)
struct mem_info *self;
struct mem_item *mi;
self = (struct mem_info *)malloc(sizeof(struct mem_info));
self = (struct mem_info *)g_malloc_nofail(sizeof(struct mem_info));
memset(self, 0, sizeof(struct mem_info));
self->addr = addr;
self->bytes = bytes;
//self->flags = 1;
mi = (struct mem_item *)malloc(sizeof(struct mem_item));
mi = (struct mem_item *)g_malloc_nofail(sizeof(struct mem_item));
memset(mi, 0, sizeof(struct mem_item));
mi->addr = addr;
mi->bytes = bytes;
@@ -129,7 +130,7 @@ libmem_add_used_item(struct mem_info *self, unsigned int addr, int bytes)
if (self->used_head == 0)
{
/* add first item */
new_mi = (struct mem_item *)malloc(sizeof(struct mem_item));
new_mi = (struct mem_item *)g_malloc_nofail(sizeof(struct mem_item));
memset(new_mi, 0, sizeof(struct mem_item));
new_mi->addr = addr;
new_mi->bytes = bytes;
@@ -144,7 +145,7 @@ libmem_add_used_item(struct mem_info *self, unsigned int addr, int bytes)
if (mi->addr > addr)
{
/* add before */
new_mi = (struct mem_item *)malloc(sizeof(struct mem_item));
new_mi = (struct mem_item *)g_malloc_nofail(sizeof(struct mem_item));
memset(new_mi, 0, sizeof(struct mem_item));
new_mi->addr = addr;
new_mi->bytes = bytes;
@@ -167,7 +168,7 @@ libmem_add_used_item(struct mem_info *self, unsigned int addr, int bytes)
if (!added)
{
/* add last */
new_mi = (struct mem_item *)malloc(sizeof(struct mem_item));
new_mi = (struct mem_item *)g_malloc_nofail(sizeof(struct mem_item));
memset(new_mi, 0, sizeof(struct mem_item));
new_mi->addr = addr;
new_mi->bytes = bytes;
@@ -193,7 +194,7 @@ libmem_add_free_item(struct mem_info *self, unsigned int addr, int bytes)
if (self->free_head == 0)
{
/* add first item */
new_mi = (struct mem_item *)malloc(sizeof(struct mem_item));
new_mi = (struct mem_item *)g_malloc_nofail(sizeof(struct mem_item));
memset(new_mi, 0, sizeof(struct mem_item));
new_mi->addr = addr;
new_mi->bytes = bytes;
@@ -230,7 +231,7 @@ libmem_add_free_item(struct mem_info *self, unsigned int addr, int bytes)
return 0;
}
/* add before */
new_mi = (struct mem_item *)malloc(sizeof(struct mem_item));
new_mi = (struct mem_item *)g_malloc_nofail(sizeof(struct mem_item));
memset(new_mi, 0, sizeof(struct mem_item));
new_mi->addr = addr;
new_mi->bytes = bytes;
@@ -253,7 +254,7 @@ libmem_add_free_item(struct mem_info *self, unsigned int addr, int bytes)
if (!added)
{
/* add last */
new_mi = (struct mem_item *)malloc(sizeof(struct mem_item));
new_mi = (struct mem_item *)g_malloc_nofail(sizeof(struct mem_item));
memset(new_mi, 0, sizeof(struct mem_item));
new_mi->addr = addr;
new_mi->bytes = bytes;