From 2312f39a0d56e55a9a9df282c478173d8d8567b6 Mon Sep 17 00:00:00 2001 From: Markus Mittendrein Date: Sun, 17 Mar 2019 15:11:21 +0100 Subject: Add malloc error checking in new, mention the error case in the description and add assert(file) in openFd --- src/cc4group.c | 10 +++++++++- src/cc4group.h | 2 +- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/cc4group.c b/src/cc4group.c index 1a726ca..95d604d 100644 --- a/src/cc4group.c +++ b/src/cc4group.c @@ -1080,7 +1080,10 @@ static bool cc4group_initNewHeader(CC4Group* const this) static CC4Group* cc4group_new(void) { CC4Group* this = malloc(sizeof(CC4Group)); - cc4group_init(this); + if(this != NULL) + { + cc4group_init(this); + } return this; } @@ -1101,12 +1104,17 @@ static bool cc4group_create(CC4Group* const this) static bool cc4group_openFd(CC4Group* const this, int fd) { + // assert is in cc4group_uncompressGroup + ChunkedReadData arg = {.arg = &fd}; return cc4group_uncompressGroup(this, cc4group_readFdReadCallback, &arg, Reference, cc4group_initChunkBufferCallback, cc4group_deinitChunkBufferCallback); } static bool cc4group_openFilePointer(CC4Group* const this, FILE* file) { + // assert(this) is in cc4group_uncompressGroup + assert(file); + ChunkedReadData arg = {.arg = file}; return cc4group_uncompressGroup(this, cc4group_readFilePointerReadCallback, &arg, Reference, cc4group_initChunkBufferCallback, cc4group_deinitChunkBufferCallback); } diff --git a/src/cc4group.h b/src/cc4group.h index d15f135..d79f6fe 100644 --- a/src/cc4group.h +++ b/src/cc4group.h @@ -57,8 +57,8 @@ typedef struct { void (*setTmpMemoryStrategy)(const CC4Group_TmpMemoryStrategy strategy); - // allocates and initializes a new group object, like the operator new + // NULL may be returned if the memory allocation (malloc) fails; in this case errno contains additional error information CC4Group* (*new)(void); // destructs the group object and frees all memory used by group, like the operator delete void (*delete)(CC4Group* const this); -- cgit v1.2.3-54-g00ecf