Date: Tue, 23 Aug 2016 01:40:45 +0000 (UTC) From: "Pedro F. Giffuni" <pfg@FreeBSD.org> To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r304650 - head/usr.bin/indent Message-ID: <201608230140.u7N1ejWr017792@repo.freebsd.org>
next in thread | raw e-mail | index | archive | help
Author: pfg Date: Tue Aug 23 01:40:45 2016 New Revision: 304650 URL: https://svnweb.freebsd.org/changeset/base/304650 Log: indent(1): Fix memory leaks pointed out by clang-analyzer. Shift the responsibility of allocating memory for the string duplicate from the caller (set_option, add_typedefs_from_file) to the callee (add_typename) as it has more knowledge about when the duplication actually needs to occur. Taken from: Piotr Stefaniak Modified: head/usr.bin/indent/args.c head/usr.bin/indent/lexi.c Modified: head/usr.bin/indent/args.c ============================================================================== --- head/usr.bin/indent/args.c Tue Aug 23 00:46:22 2016 (r304649) +++ head/usr.bin/indent/args.c Tue Aug 23 01:40:45 2016 (r304650) @@ -294,12 +294,7 @@ found: 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); } Modified: head/usr.bin/indent/lexi.c ============================================================================== --- head/usr.bin/indent/lexi.c Tue Aug 23 00:46:22 2016 (r304649) +++ head/usr.bin/indent/lexi.c Tue Aug 23 01:40:45 2016 (r304650) @@ -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); }
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201608230140.u7N1ejWr017792>