diff --git a/usr.bin/indent/args.c b/usr.bin/indent/args.c index 631cd9295c9a..f854346d55a6 100644 --- a/usr.bin/indent/args.c +++ b/usr.bin/indent/args.c @@ -294,12 +294,7 @@ set_option(char *arg) case KEY: if (*param_start == 0) goto need_param; - { - char *str = strdup(param_start); - if (str == NULL) - err(1, NULL); - add_typename(str); - } + add_typename(param_start); break; case KEY_FILE: @@ -342,7 +337,6 @@ add_typedefs_from_file(const char *str) { FILE *file; char line[BUFSIZ]; - char *copy; if ((file = fopen(str, "r")) == NULL) { fprintf(stderr, "indent: cannot open file %s\n", str); @@ -351,10 +345,7 @@ add_typedefs_from_file(const char *str) while ((fgets(line, BUFSIZ, file)) != NULL) { /* Remove trailing whitespace */ line[strcspn(line, " \t\n\r")] = '\0'; - if ((copy = strdup(line)) == NULL) { - err(1, NULL); - } - add_typename(copy); + add_typename(line); } fclose(file); } diff --git a/usr.bin/indent/lexi.c b/usr.bin/indent/lexi.c index fe73fe3f63ce..fce364a1ce0f 100644 --- a/usr.bin/indent/lexi.c +++ b/usr.bin/indent/lexi.c @@ -594,6 +594,7 @@ void add_typename(const char *key) { int comparison; + const char *copy; if (typename_top + 1 >= typename_count) { typenames = realloc((void *)typenames, @@ -602,11 +603,12 @@ add_typename(const char *key) err(1, NULL); } if (typename_top == -1) - typenames[++typename_top] = key; + typenames[++typename_top] = copy = strdup(key); else if ((comparison = strcmp(key, typenames[typename_top])) >= 0) { /* take advantage of sorted input */ - if (comparison != 0) /* remove duplicates */ - typenames[++typename_top] = key; + if (comparison == 0) /* remove duplicates */ + return; + typenames[++typename_top] = copy = strdup(key); } else { int p; @@ -617,6 +619,9 @@ add_typename(const char *key) return; memmove(&typenames[p + 1], &typenames[p], sizeof(typenames[0]) * (++typename_top - p)); - typenames[p] = key; + typenames[p] = copy = strdup(key); } + + if (copy == NULL) + err(1, NULL); }