diff options
-rw-r--r-- | linenoise/linenoise.c | 33 | ||||
-rw-r--r-- | linenoise/linenoise.h | 13 |
2 files changed, 19 insertions, 27 deletions
diff --git a/linenoise/linenoise.c b/linenoise/linenoise.c index c54e4c83..86649a32 100644 --- a/linenoise/linenoise.c +++ b/linenoise/linenoise.c @@ -118,9 +118,13 @@ #include <unistd.h> #include "linenoise.h" +typedef unsigned char mem_t; +mem_t *chk_malloc(size_t n); +mem_t *chk_realloc(mem_t *old, size_t size); + #define LINENOISE_DEFAULT_HISTORY_MAX_LEN 100 #define LINENOISE_MAX_LINE 4096 -static char *unsupported_term[] = {"dumb","cons25","emacs",NULL}; +static const char *unsupported_term[] = {"dumb","cons25","emacs",NULL}; static linenoiseCompletionCallback *completionCallback = NULL; static struct termios orig_termios; /* In order to restore at exit.*/ @@ -417,10 +421,11 @@ void linenoiseAddCompletion(linenoiseCompletions *lc, const char *str) { size_t len = strlen(str); char *copy, **cvec; - copy = malloc(len+1); + copy = (char *) chk_malloc(len+1); if (copy == NULL) return; memcpy(copy,str,len+1); - cvec = realloc(lc->cvec,sizeof(char*)*(lc->len+1)); + cvec = (char **) chk_realloc((mem_t *) lc->cvec, + (lc->len+1) * sizeof *cvec); if (cvec == NULL) { free(copy); return; @@ -446,11 +451,11 @@ static void abInit(struct abuf *ab) { } static void abAppend(struct abuf *ab, const char *s, int len) { - char *new = realloc(ab->b,ab->len+len); + char *ns = (char *) chk_realloc((mem_t *) ab->b,ab->len+len); - if (new == NULL) return; - memcpy(new+ab->len,s,len); - ab->b = new; + if (ns == NULL) return; + memcpy(ns+ab->len,s,len); + ab->b = ns; ab->len += len; } @@ -1016,7 +1021,7 @@ int linenoiseHistoryAdd(const char *line) { /* Initialization on first call. */ if (history == NULL) { - history = malloc(sizeof(char*)*history_max_len); + history = (char **) chk_malloc(history_max_len * sizeof *history); if (history == NULL) return 0; memset(history,0,(sizeof(char*)*history_max_len)); } @@ -1043,14 +1048,14 @@ int linenoiseHistoryAdd(const char *line) { * just the latest 'len' elements if the new history length value is smaller * than the amount of items already inside the history. */ int linenoiseHistorySetMaxLen(int len) { - char **new; + char **nsv; if (len < 1) return 0; if (history) { int tocopy = history_len; - new = malloc(sizeof(char*)*len); - if (new == NULL) return 0; + nsv = (char **) chk_malloc(len * sizeof *nsv); + if (nsv == NULL) return 0; /* If we can't copy everything, free the elements we'll not use. */ if (len < tocopy) { @@ -1059,10 +1064,10 @@ int linenoiseHistorySetMaxLen(int len) { for (j = 0; j < tocopy-len; j++) free(history[j]); tocopy = len; } - memset(new,0,sizeof(char*)*len); - memcpy(new,history+(history_len-tocopy), sizeof(char*)*tocopy); + memset(nsv,0,sizeof(char*)*len); + memcpy(nsv,history+(history_len-tocopy), sizeof(char*)*tocopy); free(history); - history = new; + history = nsv; } history_max_len = len; if (history_len > history_max_len) diff --git a/linenoise/linenoise.h b/linenoise/linenoise.h index fbb01cfa..10456c32 100644 --- a/linenoise/linenoise.h +++ b/linenoise/linenoise.h @@ -36,13 +36,6 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#ifndef __LINENOISE_H -#define __LINENOISE_H - -#ifdef __cplusplus -extern "C" { -#endif - typedef struct linenoiseCompletions { size_t len; char **cvec; @@ -60,9 +53,3 @@ int linenoiseHistoryLoad(const char *filename); void linenoiseClearScreen(void); void linenoiseSetMultiLine(int ml); void linenoisePrintKeyCodes(void); - -#ifdef __cplusplus -} -#endif - -#endif /* __LINENOISE_H */ |