]> sourceware.org Git - dm.git/commitdiff
fixed dmevent so that it doesn't do a double free when you run
authorBenjamin Marzinski <bmarzins@redhat.com>
Wed, 4 May 2005 18:53:28 +0000 (18:53 +0000)
committerBenjamin Marzinski <bmarzins@redhat.com>
Wed, 4 May 2005 18:53:28 +0000 (18:53 +0000)
# dmevent -l

Also, changed the behaviour of dm_get_registered_device(), so that it doesn't
change the pointer you passed in without freeing the memory on a non-next call,
and doesn't free your pointer without setting it to NULL on a failed next call.

dmeventd/dmevent.c
lib/event/libdm-event.c

index 74b5593f3bd032967a3be59e771084b6194fdd81..d0dcb95626071799cfaad995fcd8c071110368d9 100644 (file)
@@ -50,8 +50,8 @@ static void print_usage(char *name)
 }
 
 /* Parse command line arguments. */
-static int parse_argv(int argc, char **argv,
-                     char **dso_name, char **device, int *reg, int *list)
+static int parse_argv(int argc, char **argv, char **dso_name_arg,
+                     char **device_arg, int *reg, int *list)
 {
        int c;
        const char *options = "d:hlru";
@@ -59,9 +59,7 @@ static int parse_argv(int argc, char **argv,
        while ((c = getopt(argc, argv, options)) != -1) {
                switch (c) {
                case 'd':
-                       if (!(*dso_name = strdup(optarg)))
-                               exit(EXIT_FAILURE);
-
+                       dso_name_arg = optarg;
                        break;
                case 'h':
                        print_usage(argv[0]);
@@ -83,16 +81,13 @@ static int parse_argv(int argc, char **argv,
                }
        }
 
-       if (!*dso_name && !(*dso_name = strdup(default_dso_name)))
-               exit(EXIT_FAILURE);
-
        if (optind >= argc) {
                if (!*list) {
                        fprintf(stderr, "You need to specify a device.\n");
                        return 0;
                }
-       } else if (!(*device = strdup(argv[optind])))
-                       exit(EXIT_FAILURE);
+       } else 
+               device_arg = argv[optind];
 
        return 1;
 }
@@ -100,15 +95,25 @@ static int parse_argv(int argc, char **argv,
 int main(int argc, char **argv)
 {
        int list = 0, next = 0, ret, reg = default_reg;
-       char *device = NULL, *device_arg, *dso_name = NULL, *dso_name_arg;
+       char *device, *device_arg = NULL, *dso_name, *dso_name_arg = NULL;
        struct log_data *ldata;
 
-       if (!parse_argv(argc, argv, &dso_name, &device, &reg, &list))
+       if (!parse_argv(argc, argv, &dso_name_arg, &device_arg, &reg, &list))
                exit(EXIT_FAILURE);
 
-       device_arg = device;
-       dso_name_arg = dso_name;
+       if (device_arg){
+               if (!(device = strdup(device_arg)))
+                       exit(EXIT_FAILURE);
+       } else
+               device = NULL;
 
+       if (dso_name_arg){
+               if (!(dso_name = strdup(dso_name_arg)))
+                       exit(EXIT_FAILURE);
+       } else {
+               if (!(dso_name = strdup(default_dso_name)))
+                       exit(EXIT_FAILURE);
+       }
 
        if(!(ldata = malloc(sizeof(*ldata))))
                exit(ENOMEM);
@@ -134,12 +139,6 @@ int main(int argc, char **argv)
                        }
                } while (!ret);
 
-               if (dso_name)
-                       free(dso_name);
-
-               if (device)
-                       free(device);
-
                ret = (ret && device_arg) ? EXIT_FAILURE : EXIT_SUCCESS;
                goto out;
        }
@@ -158,11 +157,10 @@ int main(int argc, char **argv)
    out:
        multilog_del_type(standard, ldata);
 
-       if (device_arg)
-               free(device_arg);
-
-       if (dso_name_arg)
-               free(dso_name_arg);
+       if (device)
+               free(device);
+       if (dso_name)
+               free(dso_name);
 
        exit(ret);
 }
index 19615f6d6fda394aa46cd5b8984c3edaec29102b..e676ee32113e1e78a21e929b7582a406680bf95a 100644 (file)
@@ -350,25 +350,29 @@ int dm_get_registered_device(char **dso_name, char **device_path,
                             enum event_type *events, int next)
 {
        int ret;
-       char *dso_name_sav = NULL, *device_path_sav = NULL;
+       char *dso_name_arg = NULL, *device_path_arg = NULL;
        struct daemon_message msg;
        struct log_data *ldata;
 
-       if (next) {
-               dso_name_sav = *dso_name;
-               device_path_sav = *device_path;
-       }
-
        if (!(ret = do_event(next ? CMD_GET_NEXT_REGISTERED_DEVICE :
                                    CMD_GET_REGISTERED_DEVICE,
                             &msg, *dso_name, *device_path, *events)))
-               ret = parse_message(&msg, dso_name, device_path, events);
-
-       if (dso_name_sav)
-               free(dso_name_sav);
-
-       if (device_path_sav)
-               free(device_path_sav);
+               ret = parse_message(&msg, dso_name_arg, device_path_arg,
+                                   events);
+
+       if (next){
+               if (*dso_name)
+                       free(*dso_name);
+               if (*device_path)
+                       free(*device_path);
+               *dso_name = dso_name_arg;
+               *device_path = device_path_arg;
+       } else {
+               if (!(*dso_name))
+                       *dso_name = dso_name_arg;
+               if (!(*device_path))
+                       *device_path = device_path_arg;
+       }
 
        return ret;
 }
This page took 0.032513 seconds and 5 git commands to generate.