Skip to content

Commit 5ca9d54

Browse files
committed
Add nvlist allocation failure checks
1 parent c41b76a commit 5ca9d54

1 file changed

Lines changed: 17 additions & 7 deletions

File tree

auparse/nvlist.c

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,26 @@
2828
#include "interpret.h"
2929
#include "auparse-idata.h"
3030

31-
static inline void alloc_array(nvlist *l)
31+
/*
32+
* Allocate the initial field array.
33+
* l is the destination list.
34+
* Returns 0 on success and 1 on allocation failure.
35+
*/
36+
static inline int alloc_array(nvlist *l)
3237
{
33-
l->array = calloc(NFIELDS, sizeof(nvnode));
34-
l->size = NFIELDS;
38+
l->array = calloc(NFIELDS, sizeof(nvnode));
39+
if (l->array == NULL)
40+
return 1;
41+
42+
l->size = NFIELDS;
43+
return 0;
3544
}
3645

3746
void nvlist_create(nvlist *l)
3847
{
3948
if (l) {
40-
alloc_array(l);
49+
if (alloc_array(l))
50+
return;
4151
l->cur = 0;
4252
l->cnt = 0;
4353
l->record = NULL;
@@ -62,8 +72,8 @@ int nvlist_append(nvlist *l, const nvnode *node)
6272
(node->val == NULL))
6373
return 1;
6474

65-
if (l->array == NULL)
66-
alloc_array(l);
75+
if (l->array == NULL && alloc_array(l))
76+
return 1;
6777

6878
if (l->cnt == l->size) {
6979
nvnode* tmp;
@@ -73,7 +83,7 @@ int nvlist_append(nvlist *l, const nvnode *node)
7383
memset(l->array + l->size, 0, sizeof(nvnode) * l->size);
7484
l->size = l->size * 2;
7585
}
76-
else
86+
else
7787
return 1;
7888
}
7989

0 commit comments

Comments
 (0)