Date: Sat, 10 Dec 2016 16:53:20 -0700 From: Warner Losh <imp@bsdimp.com> To: Ngie Cooper <ngie@freebsd.org> Cc: src-committers <src-committers@freebsd.org>, "svn-src-all@freebsd.org" <svn-src-all@freebsd.org>, "svn-src-head@freebsd.org" <svn-src-head@freebsd.org> Subject: Re: svn commit: r309839 - head/sbin/camcontrol Message-ID: <CANCZdfrmYvbyMP=pvR8PDhmOMDQibEyeVnyQuq8xn-uiz=cZsw@mail.gmail.com> In-Reply-To: <201612102326.uBANQZLE008552@repo.freebsd.org> References: <201612102326.uBANQZLE008552@repo.freebsd.org>
next in thread | previous in thread | raw e-mail | index | archive | help
Why not just unconditionally free them? You don't need to NULL them out. That would save a IMHO bogus macro. On Sat, Dec 10, 2016 at 4:26 PM, Ngie Cooper <ngie@freebsd.org> wrote: > Author: ngie > Date: Sat Dec 10 23:26:34 2016 > New Revision: 309839 > URL: https://svnweb.freebsd.org/changeset/base/309839 > > Log: > free/NULL out variables prior to calling strdup to avoid leaking memory > if arguments are specified more than once with "camcontrol timestamp". > > CID: 1366829, 1366831 > MFC after: 1 week > > Modified: > head/sbin/camcontrol/timestamp.c > > Modified: head/sbin/camcontrol/timestamp.c > ============================================================================== > --- head/sbin/camcontrol/timestamp.c Sat Dec 10 22:31:49 2016 (r309838) > +++ head/sbin/camcontrol/timestamp.c Sat Dec 10 23:26:34 2016 (r309839) > @@ -336,6 +336,11 @@ timestamp(struct cam_device *device, int > int single_arg = 0; > int do_utc = 0; > > +#define FREE(x) do { \ > + free(x); \ > + x = NULL; \ > +} while(0) > + > while ((c = getopt(argc, argv, combinedopt)) != -1) { > switch (c) { > case 'r': { > @@ -358,6 +363,7 @@ timestamp(struct cam_device *device, int > } > case 'f': { > single_arg++; > + FREE(format_string); > format_string = strdup(optarg); > if (format_string == NULL) { > warn("Error allocating memory for format " > @@ -369,6 +375,7 @@ timestamp(struct cam_device *device, int > } > case 'm': { > single_arg++; > + FREE(format_string); > format_string = strdup(MIL); > if (format_string == NULL) { > warn("Error allocating memory"); > @@ -382,6 +389,7 @@ timestamp(struct cam_device *device, int > break; > } > case 'T': > + FREE(timestamp_string); > timestamp_string = strdup(optarg); > if (timestamp_string == NULL) { > warn("Error allocating memory for format " > @@ -395,6 +403,8 @@ timestamp(struct cam_device *device, int > } > } > > +#undef FREE > + > if (action == -1) { > warnx("Must specify an action, either -r or -s"); > error = 1; >
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?CANCZdfrmYvbyMP=pvR8PDhmOMDQibEyeVnyQuq8xn-uiz=cZsw>