tweaks: avoid copying an option's argument when there is no need

This takes eight extra calls of copy_of(), but saves ten calls of
free().  But more importantly: it avoids an unneeded copying and
then freeing again of the argument after 'titlecolor' and friends.
This commit is contained in:
Benno Schulenberg 2020-10-21 11:02:20 +02:00
parent d9944c03c4
commit e96bda4b9f
1 changed files with 9 additions and 21 deletions

View File

@ -1201,10 +1201,8 @@ colortype *parse_interface_color(char *combostr)
colortype *trio = nmalloc(sizeof(colortype)); colortype *trio = nmalloc(sizeof(colortype));
if (parse_combination(combostr, &trio->fg, &trio->bg, &trio->attributes)) { if (parse_combination(combostr, &trio->fg, &trio->bg, &trio->attributes)) {
free(combostr);
return trio; return trio;
} else { } else {
free(combostr);
free(trio); free(trio);
return NULL; return NULL;
} }
@ -1546,8 +1544,6 @@ void parse_rcfile(FILE *rcstream, bool just_syntax, bool intros_only)
continue; continue;
} }
#endif #endif
argument = copy_of(argument);
#ifdef ENABLE_COLOR #ifdef ENABLE_COLOR
if (strcmp(option, "titlecolor") == 0) if (strcmp(option, "titlecolor") == 0)
color_combo[TITLE_BAR] = parse_interface_color(argument); color_combo[TITLE_BAR] = parse_interface_color(argument);
@ -1571,7 +1567,7 @@ void parse_rcfile(FILE *rcstream, bool just_syntax, bool intros_only)
#endif #endif
#ifdef ENABLE_OPERATINGDIR #ifdef ENABLE_OPERATINGDIR
if (strcmp(option, "operatingdir") == 0) if (strcmp(option, "operatingdir") == 0)
operating_dir = argument; operating_dir = copy_of(argument);
else else
#endif #endif
#ifdef ENABLED_WRAPORJUSTIFY #ifdef ENABLED_WRAPORJUSTIFY
@ -1580,25 +1576,21 @@ void parse_rcfile(FILE *rcstream, bool just_syntax, bool intros_only)
jot_error(N_("Requested fill size \"%s\" is invalid"), argument); jot_error(N_("Requested fill size \"%s\" is invalid"), argument);
fill = -COLUMNS_FROM_EOL; fill = -COLUMNS_FROM_EOL;
} }
free(argument);
} else } else
#endif #endif
#ifndef NANO_TINY #ifndef NANO_TINY
if (strcmp(option, "matchbrackets") == 0) { if (strcmp(option, "matchbrackets") == 0) {
if (has_blank_char(argument)) { if (has_blank_char(argument)) {
jot_error(N_("Non-blank characters required")); jot_error(N_("Non-blank characters required"));
free(argument);
} else if (mbstrlen(argument) % 2 != 0) { } else if (mbstrlen(argument) % 2 != 0) {
jot_error(N_("Even number of characters required")); jot_error(N_("Even number of characters required"));
free(argument);
} else } else
matchbrackets = argument; matchbrackets = copy_of(argument);
} else if (strcmp(option, "whitespace") == 0) { } else if (strcmp(option, "whitespace") == 0) {
if (mbstrlen(argument) != 2 || breadth(argument) != 2) { if (mbstrlen(argument) != 2 || breadth(argument) != 2) {
jot_error(N_("Two single-column characters required")); jot_error(N_("Two single-column characters required"));
free(argument);
} else { } else {
whitespace = argument; whitespace = copy_of(argument);
whitelen[0] = char_length(whitespace); whitelen[0] = char_length(whitespace);
whitelen[1] = char_length(whitespace + whitelen[0]); whitelen[1] = char_length(whitespace + whitelen[0]);
} }
@ -1608,41 +1600,37 @@ void parse_rcfile(FILE *rcstream, bool just_syntax, bool intros_only)
if (strcmp(option, "punct") == 0) { if (strcmp(option, "punct") == 0) {
if (has_blank_char(argument)) { if (has_blank_char(argument)) {
jot_error(N_("Non-blank characters required")); jot_error(N_("Non-blank characters required"));
free(argument);
} else } else
punct = argument; punct = copy_of(argument);
} else if (strcmp(option, "brackets") == 0) { } else if (strcmp(option, "brackets") == 0) {
if (has_blank_char(argument)) { if (has_blank_char(argument)) {
jot_error(N_("Non-blank characters required")); jot_error(N_("Non-blank characters required"));
free(argument);
} else } else
brackets = argument; brackets = copy_of(argument);
} else if (strcmp(option, "quotestr") == 0) } else if (strcmp(option, "quotestr") == 0)
quotestr = argument; quotestr = copy_of(argument);
else else
#endif #endif
#ifdef ENABLE_SPELLER #ifdef ENABLE_SPELLER
if (strcmp(option, "speller") == 0) if (strcmp(option, "speller") == 0)
alt_speller = argument; alt_speller = copy_of(argument);
else else
#endif #endif
#ifndef NANO_TINY #ifndef NANO_TINY
if (strcmp(option, "backupdir") == 0) if (strcmp(option, "backupdir") == 0)
backup_dir = argument; backup_dir = copy_of(argument);
else if (strcmp(option, "wordchars") == 0) else if (strcmp(option, "wordchars") == 0)
word_chars = argument; word_chars = copy_of(argument);
else if (strcmp(option, "guidestripe") == 0) { else if (strcmp(option, "guidestripe") == 0) {
if (!parse_num(argument, &stripe_column) || stripe_column <= 0) { if (!parse_num(argument, &stripe_column) || stripe_column <= 0) {
jot_error(N_("Guide column \"%s\" is invalid"), argument); jot_error(N_("Guide column \"%s\" is invalid"), argument);
stripe_column = 0; stripe_column = 0;
} }
free(argument);
} else if (strcmp(option, "tabsize") == 0) { } else if (strcmp(option, "tabsize") == 0) {
if (!parse_num(argument, &tabsize) || tabsize <= 0) { if (!parse_num(argument, &tabsize) || tabsize <= 0) {
jot_error(N_("Requested tab size \"%s\" is invalid"), argument); jot_error(N_("Requested tab size \"%s\" is invalid"), argument);
tabsize = -1; tabsize = -1;
} }
free(argument);
} }
#else #else
; /* Properly terminate any earlier 'else'. */ ; /* Properly terminate any earlier 'else'. */