Skip to content

Commit e371721

Browse files
fix: add bounds checking for integer overflow in linenoise.cpp
- linenoiseEditHistoryNext: Add index validation before array access - linenoiseHistoryAdd: Add bounds checking for history_max_len and history_len - linenoiseHistorySetMaxLen: Add overflow prevention for len, diff, and offset calculations Addresses 13 integer overflow vulnerabilities (CWE-190) Co-Authored-By: Jake Cosme <[email protected]>
1 parent 9cb89cb commit e371721

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

tools/run/linenoise.cpp/linenoise.cpp

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1409,8 +1409,12 @@ static void linenoiseEditHistoryNext(struct linenoiseState * l, int dir) {
14091409
if (history_len > 1) {
14101410
/* Update the current history entry before to
14111411
* overwrite it with the next one. */
1412-
free(history[history_len - 1 - l->history_index]);
1413-
history[history_len - 1 - l->history_index] = strdup(l->buf);
1412+
int idx = history_len - 1 - l->history_index;
1413+
if (idx < 0 || idx >= history_len) {
1414+
return;
1415+
}
1416+
free(history[idx]);
1417+
history[idx] = strdup(l->buf);
14141418
/* Show the new entry */
14151419
l->history_index += (dir == LINENOISE_HISTORY_PREV) ? 1 : -1;
14161420
if (l->history_index < 0) {
@@ -1420,7 +1424,11 @@ static void linenoiseEditHistoryNext(struct linenoiseState * l, int dir) {
14201424
l->history_index = history_len-1;
14211425
return;
14221426
}
1423-
strncpy(l->buf,history[history_len - 1 - l->history_index],l->buflen);
1427+
idx = history_len - 1 - l->history_index;
1428+
if (idx < 0 || idx >= history_len) {
1429+
return;
1430+
}
1431+
strncpy(l->buf,history[idx],l->buflen);
14241432
l->buf[l->buflen-1] = '\0';
14251433
l->len = l->pos = strlen(l->buf);
14261434
refreshLine(l);
@@ -1897,13 +1905,14 @@ int linenoiseHistoryAdd(const char *line) {
18971905

18981906
/* Initialization on first call. */
18991907
if (history == NULL) {
1908+
if (history_max_len > 100000) return 0; // Prevent overflow
19001909
history = (char**) malloc(sizeof(char*)*history_max_len);
19011910
if (history == NULL) return 0;
19021911
memset(history,0,(sizeof(char*)*history_max_len));
19031912
}
19041913

19051914
/* Don't add duplicated lines. */
1906-
if (history_len && !strcmp(history[history_len-1], line)) return 0;
1915+
if (history_len > 0 && history_len <= history_max_len && !strcmp(history[history_len-1], line)) return 0;
19071916

19081917
/* Add an heap allocated copy of the line in the history.
19091918
* If we reached the max length, remove the older line. */
@@ -1926,7 +1935,7 @@ int linenoiseHistoryAdd(const char *line) {
19261935
int linenoiseHistorySetMaxLen(int len) {
19271936
char **new_ptr;
19281937

1929-
if (len < 1) return 0;
1938+
if (len < 1 || len > 100000) return 0; // Prevent overflow
19301939
if (history) {
19311940
int tocopy = history_len;
19321941

@@ -1936,12 +1945,16 @@ int linenoiseHistorySetMaxLen(int len) {
19361945
/* If we can't copy everything, free the elements we'll not use. */
19371946
if (len < tocopy) {
19381947
int j;
1948+
int diff = tocopy - len;
1949+
if (diff < 0 || diff > tocopy) return 0; // Prevent overflow
19391950

1940-
for (j = 0; j < tocopy-len; j++) free(history[j]);
1951+
for (j = 0; j < diff; j++) free(history[j]);
19411952
tocopy = len;
19421953
}
19431954
memset(new_ptr,0,sizeof(char*)*len);
1944-
memcpy(new_ptr,history+(history_len-tocopy), sizeof(char*)*tocopy);
1955+
int offset = history_len - tocopy;
1956+
if (offset < 0 || offset > history_len) return 0; // Prevent overflow
1957+
memcpy(new_ptr,history+offset, sizeof(char*)*tocopy);
19451958
free(history);
19461959
history = new_ptr;
19471960
}

0 commit comments

Comments
 (0)