Skip to content

Commit d6d3394

Browse files
committed
Malloc and strdup may fail. Check return value and handle the situation.
1 parent 168ac24 commit d6d3394

5 files changed

Lines changed: 28 additions & 0 deletions

File tree

src/hal/user_comps/gs2_vfd.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,8 @@ int main(int argc, char **argv)
554554
goto out_noclose;
555555
}
556556
device = strdup(optarg);
557+
if(!device)
558+
device = "/dev/strdup/error_out_of_memory";
557559
break;
558560
case 'g':
559561
debug = 1;

src/hal/utils/halcmd_completion.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,8 @@ static char *loadrt_generator(const char *text, int state) {
541541
if(startswith(ent->d_name, "rtapi.")) continue;
542542
if(strncmp(text, ent->d_name, len) != 0) continue;
543543
result = strdup(ent->d_name);
544+
if(!result)
545+
return NULL;
544546
result[strlen(result) - strlen(MODULE_EXT)] = 0;
545547
return result;
546548
}

src/hal/utils/halrmt.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3395,6 +3395,10 @@ void *readClient(void *arg)
33953395

33963396
// res = 1;
33973397
context = (connectionRecType *) malloc(sizeof(connectionRecType));
3398+
if(NULL == context) {
3399+
perror("readClient():malloc");
3400+
abort(); // There is no "clean" way. Ensure we make some noise.
3401+
}
33983402
context->cliSock = client_sockfd;
33993403
context->linked = 0;
34003404
context->echo = 1;

src/libnml/linklist/linklist.cc

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,10 @@ int LinkedList::store_at_head(void *_data, size_t _size, int _copy)
234234

235235
if (_copy) {
236236
last_data_stored = malloc(_size);
237+
if(!last_data_stored) {
238+
perror("LinkedList::store_at_head()");
239+
return -1;
240+
}
237241
memcpy(last_data_stored, _data, _size);
238242
last_size_stored = _size;
239243
new_head = new LinkedListNode(last_data_stored, _size);
@@ -316,6 +320,10 @@ int LinkedList::store_at_tail(void *_data, size_t _size, int _copy)
316320

317321
if (_copy) {
318322
last_data_stored = malloc(_size);
323+
if(!last_data_stored) {
324+
perror("LinkedList::store_at_tail()");
325+
return -1;
326+
}
319327
memcpy(last_data_stored, _data, _size);
320328
last_size_stored = _size;
321329
new_tail = new LinkedListNode(last_data_stored, _size);
@@ -419,6 +427,10 @@ int LinkedList::store_after_current_node(void *_data, size_t _size,
419427

420428
if (_copy) {
421429
last_data_stored = malloc(_size);
430+
if(!last_data_stored) {
431+
perror("LinkedList::store_after_current_node()");
432+
return -1;
433+
}
422434
memcpy(last_data_stored, _data, _size);
423435
last_size_stored = _size;
424436
new_node = new LinkedListNode(last_data_stored, _size);
@@ -539,6 +551,10 @@ int LinkedList::store_before_current_node(void *_data, size_t _size,
539551

540552
if (_copy) {
541553
last_data_stored = malloc(_size);
554+
if(!last_data_stored) {
555+
perror("LinkedList::store_before_current_node()");
556+
return -1;
557+
}
542558
memcpy(last_data_stored, _data, _size);
543559
last_size_stored = _size;
544560
new_node = new LinkedListNode(last_data_stored, _size);

src/libnml/os_intf/_sem.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,10 @@ rcs_sem_t *rcs_sem_open(key_t name, int oflag, /* int mode */ ...)
128128
so we need to allocate space that users will free later with
129129
rcs_sem_close */
130130
retval = (rcs_sem_t *) malloc(sizeof(rcs_sem_t));
131+
if(!retval) {
132+
rcs_print_error("rcs_sem_open: %s\n", strerror(errno));
133+
return NULL;
134+
}
131135
*retval = semid;
132136
return retval;
133137
}

0 commit comments

Comments
 (0)