Skip to content

Commit

Permalink
gh-730: Fix leaks caused by realloc failure.
Browse files Browse the repository at this point in the history
  • Loading branch information
PengZheng committed Feb 22, 2024
1 parent 0bca5ef commit d729e7a
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -339,11 +339,12 @@ static size_t endpointDiscoveryPoller_writeMemory(void *contents, size_t size, s
size_t realsize = size * nmemb;
struct MemoryStruct *mem = (struct MemoryStruct *)memoryPtr;

mem->memory = realloc(mem->memory, mem->size + realsize + 1);
if(mem->memory == NULL) {
printf("ENDPOINT_POLLER: not enough memory (realloc returned NULL)!");
return 0;
}
void* newMem = realloc(mem->memory, mem->size + realsize + 1);
if (newMem == NULL) {
printf("ENDPOINT_POLLER: not enough memory (realloc returned NULL)!");
return 0;
}
mem->memory = newMem;

memcpy(&(mem->memory[mem->size]), contents, realsize);
mem->size += realsize;
Expand Down
28 changes: 28 additions & 0 deletions libs/dfi/gtest/src/dyn_type_ei_tests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,34 @@ TEST_F(DynTypeErrorInjectionTestSuite, SequenceReserveError) {
dynType_destroy(type);
}

TEST_F(DynTypeErrorInjectionTestSuite, SequenceReserveError2) {
struct double_sequence {
uint32_t cap;
uint32_t len;
double* buf;
};

dyn_type *type = NULL;
int rc = 0;
rc = dynType_parseWithStr("[D", NULL, NULL, &type);
ASSERT_EQ(0, rc);

struct double_sequence *seq = NULL;
rc = dynType_alloc(type, (void **)&seq);
ASSERT_EQ(0, rc);
ASSERT_TRUE(seq != NULL);

celix_ei_expect_realloc((void*)dynType_sequence_reserve, 0, nullptr, 2);
rc = dynType_sequence_reserve(type, seq, 1);
ASSERT_EQ(0, rc);
rc = dynType_sequence_reserve(type, seq, 2);
ASSERT_NE(0, rc);
ASSERT_STREQ("Error allocating memory for seq buf", celix_err_popLastError());

dynType_free(type, seq);
dynType_destroy(type);
}

TEST_F(DynTypeErrorInjectionTestSuite, TextAllocateError) {
dyn_type *type = NULL;
int rc = 0;
Expand Down
6 changes: 3 additions & 3 deletions libs/dfi/src/dyn_type.c
Original file line number Diff line number Diff line change
Expand Up @@ -604,12 +604,12 @@ int dynType_sequence_reserve(const dyn_type* type, void* inst, uint32_t cap) {
return OK;
}
size_t size = dynType_size(type->sequence.itemType);
seq->buf = realloc(seq->buf, (size_t)(cap * size));
if (seq->buf == NULL) {
seq->cap = 0;
void* newBuf = realloc(seq->buf, (size_t)(cap * size));
if (newBuf == NULL) {
celix_err_pushf("Error allocating memory for seq buf");
return MEM_ERROR;
}
seq->buf = newBuf;
memset(seq->buf+seq->cap*size, 0, (cap-seq->cap)*size);
seq->cap = cap;
return status;
Expand Down
11 changes: 6 additions & 5 deletions libs/etcdlib/src/etcd.c
Original file line number Diff line number Diff line change
Expand Up @@ -637,12 +637,12 @@ static size_t WriteMemoryCallback(void *contents, size_t size, size_t nmemb, voi
size_t realsize = size * nmemb;
struct MemoryStruct *mem = (struct MemoryStruct *) userp;

mem->memory = realloc(mem->memory, mem->memorySize + realsize + 1);
if (mem->memory == NULL) {
/* out of memory! */
void* newMem = realloc(mem->memory, mem->memorySize + realsize + 1);
if (newMem == NULL) {
fprintf(stderr, "[ETCDLIB] Error: not enough memory (realloc returned NULL)\n");
return 0;
}
mem->memory = newMem;

memcpy(&(mem->memory[mem->memorySize]), contents, realsize);
mem->memorySize += realsize;
Expand All @@ -655,12 +655,13 @@ static size_t WriteHeaderCallback(void *contents, size_t size, size_t nmemb, voi
size_t realsize = size * nmemb;
struct MemoryStruct *mem = (struct MemoryStruct *) userp;

mem->header = realloc(mem->header, mem->headerSize + realsize + 1);
if (mem->header == NULL) {
void* newHeader = realloc(mem->header, mem->headerSize + realsize + 1);
if (newHeader == NULL) {
/* out of memory! */
fprintf(stderr, "[ETCDLIB] Error: not enough header-memory (realloc returned NULL)\n");
return 0;
}
mem->header = newHeader;

memcpy(&(mem->header[mem->headerSize]), contents, realsize);
mem->headerSize += realsize;
Expand Down

0 comments on commit d729e7a

Please sign in to comment.