Skip to content

Commit

Permalink
Add NULL checks to memory allocation functions
Browse files Browse the repository at this point in the history
strdup(), malloc(), realloc(), etc. may return NULL. Hence, the caller
should handle it to prevent NULL pointer exceptions.

This bug was discovered and resolved using Coverity Static Analysis
Security Testing (SAST) by Synopsys, Inc.

Signed-off-by: Metin Kaya <metikaya@amazon.co.uk>
  • Loading branch information
Metin Kaya authored and dwmw2 committed Sep 7, 2022
1 parent fc925d3 commit 660a084
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
14 changes: 11 additions & 3 deletions linenoise.c
Original file line number Diff line number Diff line change
Expand Up @@ -879,6 +879,7 @@ linenoiseCompletionCallback * linenoiseSetCompletionCallback(linenoiseCompletion

void linenoiseAddCompletion(linenoiseCompletions *lc, const char *str) {
lc->cvec = (char **)realloc(lc->cvec,sizeof(char*)*(lc->len+1));
assert(lc->cvec);
lc->cvec[lc->len++] = strdup(str);
}

Expand Down Expand Up @@ -1785,8 +1786,10 @@ static int linenoiseEdit(struct current *current) {
if (history_len > 1) {
/* Update the current history entry before to
* overwrite it with tne next one. */
free(history[history_len - 1 - history_index]);
history[history_len - 1 - history_index] = strdup(sb_str(current->buf));
int index = history_len - 1 - history_index;
free(history[index]);
history[index] = strdup(sb_str(current->buf));
assert(history[index]);
/* Show the new entry */
history_index += dir;
if (history_index < 0) {
Expand Down Expand Up @@ -1951,6 +1954,7 @@ static int linenoiseHistoryAddAllocated(char *line) {
}
if (history == NULL) {
history = (char **)calloc(sizeof(char*), history_max_len);
assert(history);
}

/* do not insert duplicate lines into history */
Expand All @@ -1969,7 +1973,9 @@ static int linenoiseHistoryAddAllocated(char *line) {
}

int linenoiseHistoryAdd(const char *line) {
return linenoiseHistoryAddAllocated(strdup(line));
char *new_line = strdup(line);
assert(new_line);
return linenoiseHistoryAddAllocated(new_line);
}

int linenoiseHistoryGetMaxLen(void) {
Expand All @@ -1984,6 +1990,7 @@ int linenoiseHistorySetMaxLen(int len) {
int tocopy = history_len;

newHistory = (char **)calloc(sizeof(char*), len);
assert(newHistory);

/* If we can't copy everything, free the elements we'll not use. */
if (len < tocopy) {
Expand Down Expand Up @@ -2049,6 +2056,7 @@ int linenoiseHistoryLoad(const char *filename) {
while ((sb = sb_getline(fp)) != NULL) {
/* Take the stringbuf and decode backslash escaped values */
char *buf = sb_to_string(sb);
assert(buf);
char *dest = buf;
const char *src;

Expand Down
1 change: 1 addition & 0 deletions stringbuf.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
stringbuf *sb_alloc(void)
{
stringbuf *sb = (stringbuf *)malloc(sizeof(*sb));
assert(sb);
sb->remaining = 0;
sb->last = 0;
#ifdef USE_UTF8
Expand Down

0 comments on commit 660a084

Please sign in to comment.