fuse_opt_parse(): fix memory leak
authorMiklos Szeredi <mszeredi@suse.cz>
Mon, 18 Feb 2013 15:24:11 +0000 (16:24 +0100)
committerMiklos Szeredi <mszeredi@suse.cz>
Tue, 19 Feb 2013 13:14:00 +0000 (14:14 +0100)
when storing a newly allocated string for format "%s", free the previous value
stored at that location.

Reported by Marco Schuster

ChangeLog
include/fuse_opt.h
lib/fuse_opt.c

index 502a9bb9011e86c20d77e91e5db38a0311f3e4d2..9a775bb0d4eec8b79b1b7c34e03f2e53906d0452 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2013-02-18  Miklos Szeredi <miklos@szeredi.hu>
+
+       * fuse_opt_parse(): when storing a newly allocated string for
+       format "%s", free the previous value stored at that location.
+       Reported by Marco Schuster
+
 2013-02-07  Miklos Szeredi <miklos@szeredi.hu>
 
        * libfuse: add readdirplus support in fuse_lowlevel_ops.  Patch by
index add0a30894db5ca3bb7feebb463c42fbb3d766a8..20653b189eaba565a327083c56d2cb2fec7eb0c2 100644 (file)
@@ -70,8 +70,9 @@ extern "C" {
  *
  * 6) "-x %s", etc.  Combination of 4) and 5)
  *
- * If the format is "%s", memory is allocated for the string unlike
- * with scanf().
+ * If the format is "%s", memory is allocated for the string unlike with
+ * scanf().  The previous value (if non-NULL) stored at the this location is
+ * freed.
  */
 struct fuse_opt {
        /** Matching template and optional parameter formatting */
index 93efd2952ef2b4a8365a99c43289d7e2b00c321a..15f9e2152ba31eec8eab5a684004b6b7e9018de8 100644 (file)
@@ -204,11 +204,13 @@ static int process_opt_param(void *var, const char *format, const char *param,
 {
        assert(format[0] == '%');
        if (format[1] == 's') {
+               char **s = var;
                char *copy = strdup(param);
                if (!copy)
                        return alloc_failed();
 
-               *(char **) var = copy;
+               free(*s);
+               *s = copy;
        } else {
                if (sscanf(param, format, var) != 1) {
                        fprintf(stderr, "fuse: invalid parameter in option `%s'\n", arg);