From d10711353bf9cf0c03a31f81478b57672065b6f0 Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Fri, 4 Sep 2015 07:25:27 -0700 Subject: linenoise: compile as C++ and use checked allocator. * linenoise/linenoise.c (mem_t): New typedef, compatible with the one in lib.h, which we don't want to include. (chk_malloc, chk_realloc): External declarations added. (unsupported_term): Make element type const char *. (linenoiseAddCompletion): Use checked allocator, add casts, use the superior "sizeof *dest_pointer_var" expression in size calculations rather than "sizeof (maybe_wrong_type)". (abAppend, linenoiseHistoryAdd, linenoiseHistorySetMaxLen): Likewise. * linenoise/linenoise.h: Remove unnecessary include guards; we don't use them in this project. Remove 'extern "C"'; we don't require C linkage when compiling everything as C++. --- linenoise/linenoise.c | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) (limited to 'linenoise/linenoise.c') 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 #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) -- cgit v1.2.3