From be7e000389a2f65ef7918b1e94c9831eaf1f444c Mon Sep 17 00:00:00 2001 From: Benno Schulenberg Date: Thu, 20 Feb 2020 16:52:48 +0100 Subject: [PATCH] tweaks: pull the NUL-terminating of a string into a function Also, avoid copying one byte too many that afterward gets overwritten with '\0'. --- src/files.c | 3 +-- src/help.c | 3 +-- src/nano.c | 3 +-- src/text.c | 16 +++++----------- src/utils.c | 8 +++++--- 5 files changed, 13 insertions(+), 20 deletions(-) diff --git a/src/files.c b/src/files.c index 62ab9609..8e442d20 100644 --- a/src/files.c +++ b/src/files.c @@ -2228,8 +2228,7 @@ char *real_dir_from_tilde(const char *path) #ifdef HAVE_PWD_H const struct passwd *userdata; - tilded = measured_copy(path, i + 1); - tilded[i] = '\0'; + tilded = measured_copy(path, i); do { userdata = getpwent(); diff --git a/src/help.c b/src/help.c index e5ff505e..9413dd9b 100644 --- a/src/help.c +++ b/src/help.c @@ -166,8 +166,7 @@ void show_help(void) /* Extract the title from the head of the help text. */ length = break_line(help_text, MAX_BUF_SIZE, TRUE); - title = measured_copy(help_text, length + 1); - title[length] = '\0'; + title = measured_copy(help_text, length); titlebar(title); diff --git a/src/nano.c b/src/nano.c index 3b7941de..5683c913 100644 --- a/src/nano.c +++ b/src/nano.c @@ -223,8 +223,7 @@ void partition_buffer(linestruct *top, size_t top_x, * top of the partition from it, and save the text before top_x. */ foreline = top->prev; top->prev = NULL; - antedata = measured_copy(top->data, top_x + 1); - antedata[top_x] = '\0'; + antedata = measured_copy(top->data, top_x); /* Remember which line is below the bottom of the partition, detach the * bottom of the partition from it, and save the text after bot_x. */ diff --git a/src/text.c b/src/text.c index 7236ea42..0ed95d2f 100644 --- a/src/text.c +++ b/src/text.c @@ -261,9 +261,7 @@ void do_unindent(void) * possible, and saving the removed whitespace in the undo item. */ for (line = top; line != bot->next; line = line->next) { size_t indent_len = length_of_white(line->data); - char *indentation = measured_copy(line->data, indent_len + 1); - - indentation[indent_len] = '\0'; + char *indentation = measured_copy(line->data, indent_len); unindent_a_line(line, indent_len); update_multiline_undo(line->lineno, indentation); @@ -579,8 +577,7 @@ void do_undo(void) } t = make_new_node(f); t->data = copy_of(u->strdata); - data = measured_copy(f->data, u->mark_begin_x + 1); - data[u->mark_begin_x] = '\0'; + data = measured_copy(f->data, u->mark_begin_x); free(f->data); f->data = data; splice_node(f, t); @@ -725,8 +722,7 @@ void do_redo(void) redidmsg = _("line break"); shoveline = make_new_node(f); shoveline->data = copy_of(u->strdata); - data = measured_copy(f->data, u->begin + 1); - data[u->begin] = '\0'; + data = measured_copy(f->data, u->begin); free(f->data); f->data = data; splice_node(f, shoveline); @@ -1906,8 +1902,7 @@ void justify_paragraph(linestruct **line, size_t par_len) /* Copy the leading part (quoting + indentation) of the sample line. */ quote_len = quote_length(sampleline->data); lead_len = quote_len + indent_length(sampleline->data + quote_len); - lead_string = measured_copy(sampleline->data, lead_len + 1); - lead_string[lead_len] = '\0'; + lead_string = measured_copy(sampleline->data, lead_len); /* Concatenate all lines of the paragraph into a single line. */ concat_paragraph(line, par_len); @@ -1988,8 +1983,7 @@ void do_justify(bool full_justify) /* Copy the leading part that is to be used for the new paragraph. */ quote_len = quote_length(first_par_line->data); lead_len = quote_len + indent_length(first_par_line->data + quote_len); - the_lead = measured_copy(first_par_line->data, lead_len + 1); - the_lead[lead_len] = '\0'; + the_lead = measured_copy(first_par_line->data, lead_len); /* Copy the leading part that is to be used for the new paragraph after * its first line (if any): the quoting of the first line, plus the diff --git a/src/utils.c b/src/utils.c index 69557d21..ce5a7518 100644 --- a/src/utils.c +++ b/src/utils.c @@ -324,12 +324,14 @@ char *mallocstrcpy(char *dest, const char *src) return dest; } -/* Return an allocated copy of the first count characters of the given string. */ +/* Return an allocated copy of the first count characters + * of the given string, and NUL-terminate the copy. */ char *measured_copy(const char *string, size_t count) { - char *thecopy = charalloc(count); + char *thecopy = charalloc(count + 1); strncpy(thecopy, string, count); + thecopy[count] = '\0'; return thecopy; } @@ -337,7 +339,7 @@ char *measured_copy(const char *string, size_t count) /* Return an allocated copy of the given string. */ char *copy_of(const char *string) { - return measured_copy(string, strlen(string) + 1); + return measured_copy(string, strlen(string)); } /* Free the string at dest and return the string at src. */