tweaks: pull the NUL-terminating of a string into a function

Also, avoid copying one byte too many that afterward gets
overwritten with '\0'.
master
Benno Schulenberg 2020-02-20 16:52:48 +01:00
parent 1d4411a474
commit be7e000389
5 changed files with 13 additions and 20 deletions

View File

@ -2228,8 +2228,7 @@ char *real_dir_from_tilde(const char *path)
#ifdef HAVE_PWD_H #ifdef HAVE_PWD_H
const struct passwd *userdata; const struct passwd *userdata;
tilded = measured_copy(path, i + 1); tilded = measured_copy(path, i);
tilded[i] = '\0';
do { do {
userdata = getpwent(); userdata = getpwent();

View File

@ -166,8 +166,7 @@ void show_help(void)
/* Extract the title from the head of the help text. */ /* Extract the title from the head of the help text. */
length = break_line(help_text, MAX_BUF_SIZE, TRUE); length = break_line(help_text, MAX_BUF_SIZE, TRUE);
title = measured_copy(help_text, length + 1); title = measured_copy(help_text, length);
title[length] = '\0';
titlebar(title); titlebar(title);

View File

@ -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. */ * top of the partition from it, and save the text before top_x. */
foreline = top->prev; foreline = top->prev;
top->prev = NULL; top->prev = NULL;
antedata = measured_copy(top->data, top_x + 1); antedata = measured_copy(top->data, top_x);
antedata[top_x] = '\0';
/* Remember which line is below the bottom of the partition, detach the /* 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. */ * bottom of the partition from it, and save the text after bot_x. */

View File

@ -261,9 +261,7 @@ void do_unindent(void)
* possible, and saving the removed whitespace in the undo item. */ * possible, and saving the removed whitespace in the undo item. */
for (line = top; line != bot->next; line = line->next) { for (line = top; line != bot->next; line = line->next) {
size_t indent_len = length_of_white(line->data); size_t indent_len = length_of_white(line->data);
char *indentation = measured_copy(line->data, indent_len + 1); char *indentation = measured_copy(line->data, indent_len);
indentation[indent_len] = '\0';
unindent_a_line(line, indent_len); unindent_a_line(line, indent_len);
update_multiline_undo(line->lineno, indentation); update_multiline_undo(line->lineno, indentation);
@ -579,8 +577,7 @@ void do_undo(void)
} }
t = make_new_node(f); t = make_new_node(f);
t->data = copy_of(u->strdata); t->data = copy_of(u->strdata);
data = measured_copy(f->data, u->mark_begin_x + 1); data = measured_copy(f->data, u->mark_begin_x);
data[u->mark_begin_x] = '\0';
free(f->data); free(f->data);
f->data = data; f->data = data;
splice_node(f, t); splice_node(f, t);
@ -725,8 +722,7 @@ void do_redo(void)
redidmsg = _("line break"); redidmsg = _("line break");
shoveline = make_new_node(f); shoveline = make_new_node(f);
shoveline->data = copy_of(u->strdata); shoveline->data = copy_of(u->strdata);
data = measured_copy(f->data, u->begin + 1); data = measured_copy(f->data, u->begin);
data[u->begin] = '\0';
free(f->data); free(f->data);
f->data = data; f->data = data;
splice_node(f, shoveline); 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. */ /* Copy the leading part (quoting + indentation) of the sample line. */
quote_len = quote_length(sampleline->data); quote_len = quote_length(sampleline->data);
lead_len = quote_len + indent_length(sampleline->data + quote_len); lead_len = quote_len + indent_length(sampleline->data + quote_len);
lead_string = measured_copy(sampleline->data, lead_len + 1); lead_string = measured_copy(sampleline->data, lead_len);
lead_string[lead_len] = '\0';
/* Concatenate all lines of the paragraph into a single line. */ /* Concatenate all lines of the paragraph into a single line. */
concat_paragraph(line, par_len); 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. */ /* Copy the leading part that is to be used for the new paragraph. */
quote_len = quote_length(first_par_line->data); quote_len = quote_length(first_par_line->data);
lead_len = quote_len + indent_length(first_par_line->data + quote_len); 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 = measured_copy(first_par_line->data, lead_len);
the_lead[lead_len] = '\0';
/* Copy the leading part that is to be used for the new paragraph after /* 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 * its first line (if any): the quoting of the first line, plus the

View File

@ -324,12 +324,14 @@ char *mallocstrcpy(char *dest, const char *src)
return dest; 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 *measured_copy(const char *string, size_t count)
{ {
char *thecopy = charalloc(count); char *thecopy = charalloc(count + 1);
strncpy(thecopy, string, count); strncpy(thecopy, string, count);
thecopy[count] = '\0';
return thecopy; return thecopy;
} }
@ -337,7 +339,7 @@ char *measured_copy(const char *string, size_t count)
/* Return an allocated copy of the given string. */ /* Return an allocated copy of the given string. */
char *copy_of(const char *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. */ /* Free the string at dest and return the string at src. */