summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--lib.c8
-rw-r--r--lib.h1
-rw-r--r--linenoise/linenoise.c9
3 files changed, 14 insertions, 4 deletions
diff --git a/lib.c b/lib.c
index a3f0c6fb..fbabda77 100644
--- a/lib.c
+++ b/lib.c
@@ -1926,6 +1926,14 @@ wchar_t *chk_strdup(const wchar_t *str)
return copy;
}
+char *chk_strdup_utf8(const char *str)
+{
+ size_t nchar = strlen(str) + 1;
+ char *copy = coerce(char *, chk_malloc(nchar));
+ assert (!async_sig_enabled);
+ memcpy(copy, str, nchar);
+ return copy;
+}
val cons(val car, val cdr)
{
diff --git a/lib.h b/lib.h
index 7cb6d89b..6b8a9d6e 100644
--- a/lib.h
+++ b/lib.h
@@ -531,6 +531,7 @@ mem_t *chk_realloc(mem_t *, size_t size);
mem_t *chk_grow_vec(mem_t *old, size_t oldelems, size_t newelems,
size_t elsize);
wchar_t *chk_strdup(const wchar_t *str);
+char *chk_strdup_utf8(const char *str);
val cons(val car, val cdr);
val make_lazy_cons(val func);
val make_half_lazy_cons(val func, val car);
diff --git a/linenoise/linenoise.c b/linenoise/linenoise.c
index f0311782..c28e7dd2 100644
--- a/linenoise/linenoise.c
+++ b/linenoise/linenoise.c
@@ -182,6 +182,7 @@ enum key_action {
typedef unsigned char mem_t;
mem_t *chk_malloc(size_t n);
mem_t *chk_realloc(mem_t *old, size_t size);
+char *chk_strdup_utf8(const char *str);
static lino_t lino_list = { &lino_list, &lino_list };
static int atexit_registered = 0; /* Register atexit just 1 time. */
@@ -702,7 +703,7 @@ static void edit_history_next(lino_t *l, int dir) {
/* Update the current history entry before to
* overwrite it with the next one. */
free(l->history[l->history_len - 1 - l->history_index]);
- l->history[l->history_len - 1 - l->history_index] = strdup(l->data);
+ l->history[l->history_len - 1 - l->history_index] = chk_strdup_utf8(l->data);
/* Show the new entry */
l->history_index += (dir == LINENOISE_HISTORY_PREV) ? 1 : -1;
if (l->history_index < 0) {
@@ -1019,7 +1020,7 @@ char *linenoise(lino_t *ls, const char *prompt)
if (count && ls->data[count-1] == '\n')
ls->data[count-1] = '\0';
- return strdup(ls->data);
+ return chk_strdup_utf8(ls->data);
} else {
/* Interactive editing. */
if (enable_raw_mode(ls) == -1)
@@ -1030,7 +1031,7 @@ char *linenoise(lino_t *ls, const char *prompt)
printf("\n");
if (count == -1)
return 0;
- return strdup(ls->data);
+ return chk_strdup_utf8(ls->data);
}
}
@@ -1132,7 +1133,7 @@ int lino_hist_add(lino_t *ls, const char *line) {
/* Add an heap allocated copy of the line in the history.
* If we reached the max length, remove the older line. */
- linecopy = strdup(line);
+ linecopy = chk_strdup_utf8(line);
if (!linecopy) return 0;
if (ls->history_len == ls->history_max_len) {
free(ls->history[0]);