debuggers.hg

changeset 20689:23bc248302df

mini-os: Fix memory leaks in blkfront, netfront, pcifront, etc.

The return value of Xenbus routines xenbus_transaction_start(),
xenbus_printf(), xenbus_transaction_end(), etc. is a pointer of error
message. This pointer should be passed to free() to release the
allocated memory when it is no longer needed.

Signed-off-by: Yu Zhiguo <yuzg@cn.fujitsu.com>
author Keir Fraser <keir.fraser@citrix.com>
date Mon Dec 14 09:51:07 2009 +0000 (2009-12-14)
parents 0cb22e32795c
children 4bde3bb310b4
files extras/mini-os/blkfront.c extras/mini-os/console/xencons_ring.c extras/mini-os/fbfront.c extras/mini-os/fs-front.c extras/mini-os/netfront.c extras/mini-os/pcifront.c extras/mini-os/xenbus/xenbus.c
line diff
     1.1 --- a/extras/mini-os/blkfront.c	Mon Dec 14 09:48:47 2009 +0000
     1.2 +++ b/extras/mini-os/blkfront.c	Mon Dec 14 09:51:07 2009 +0000
     1.3 @@ -93,7 +93,7 @@ struct blkfront_dev *init_blkfront(char 
     1.4      char* message=NULL;
     1.5      struct blkif_sring *s;
     1.6      int retry=0;
     1.7 -    char* msg;
     1.8 +    char* msg = NULL;
     1.9      char* c;
    1.10      char* nodename = _nodename ? _nodename : "device/vbd/768";
    1.11  
    1.12 @@ -129,6 +129,7 @@ again:
    1.13      err = xenbus_transaction_start(&xbt);
    1.14      if (err) {
    1.15          printk("starting transaction\n");
    1.16 +        free(err);
    1.17      }
    1.18  
    1.19      err = xenbus_printf(xbt, nodename, "ring-ref","%u",
    1.20 @@ -159,6 +160,7 @@ again:
    1.21  
    1.22  
    1.23      err = xenbus_transaction_end(xbt, 0, &retry);
    1.24 +    if (err) free(err);
    1.25      if (retry) {
    1.26              goto again;
    1.27          printk("completing transaction\n");
    1.28 @@ -167,7 +169,8 @@ again:
    1.29      goto done;
    1.30  
    1.31  abort_transaction:
    1.32 -    xenbus_transaction_end(xbt, 1, &retry);
    1.33 +    free(err);
    1.34 +    err = xenbus_transaction_end(xbt, 1, &retry);
    1.35      goto error;
    1.36  
    1.37  done:
    1.38 @@ -238,6 +241,8 @@ done:
    1.39      return dev;
    1.40  
    1.41  error:
    1.42 +    free(msg);
    1.43 +    free(err);
    1.44      free_blkfront(dev);
    1.45      return NULL;
    1.46  }
    1.47 @@ -265,6 +270,7 @@ void shutdown_blkfront(struct blkfront_d
    1.48      state = xenbus_read_integer(path);
    1.49      while (err == NULL && state < XenbusStateClosing)
    1.50          err = xenbus_wait_for_state_change(path, &state, &dev->events);
    1.51 +    if (err) free(err);
    1.52  
    1.53      if ((err = xenbus_switch_state(XBT_NIL, nodename, XenbusStateClosed)) != NULL) {
    1.54          printk("shutdown_blkfront: error changing state to %d: %s\n",
    1.55 @@ -272,8 +278,10 @@ void shutdown_blkfront(struct blkfront_d
    1.56          goto close;
    1.57      }
    1.58      state = xenbus_read_integer(path);
    1.59 -    if (state < XenbusStateClosed)
    1.60 -        xenbus_wait_for_state_change(path, &state, &dev->events);
    1.61 +    if (state < XenbusStateClosed) {
    1.62 +        err = xenbus_wait_for_state_change(path, &state, &dev->events);
    1.63 +        if (err) free(err);
    1.64 +    }
    1.65  
    1.66      if ((err = xenbus_switch_state(XBT_NIL, nodename, XenbusStateInitialising)) != NULL) {
    1.67          printk("shutdown_blkfront: error changing state to %d: %s\n",
    1.68 @@ -286,6 +294,7 @@ void shutdown_blkfront(struct blkfront_d
    1.69          err = xenbus_wait_for_state_change(path, &state, &dev->events);
    1.70  
    1.71  close:
    1.72 +    if (err) free(err);
    1.73      xenbus_unwatch_path_token(XBT_NIL, path, path);
    1.74  
    1.75      snprintf(path, sizeof(path), "%s/ring-ref", nodename);
     2.1 --- a/extras/mini-os/console/xencons_ring.c	Mon Dec 14 09:48:47 2009 +0000
     2.2 +++ b/extras/mini-os/console/xencons_ring.c	Mon Dec 14 09:51:07 2009 +0000
     2.3 @@ -203,7 +203,7 @@ struct consfront_dev *init_consfront(cha
     2.4      char* err;
     2.5      char* message=NULL;
     2.6      int retry=0;
     2.7 -    char* msg;
     2.8 +    char* msg = NULL;
     2.9      char nodename[256];
    2.10      char path[256];
    2.11      static int consfrontends = 1;
    2.12 @@ -242,6 +242,7 @@ again:
    2.13      err = xenbus_transaction_start(&xbt);
    2.14      if (err) {
    2.15          printk("starting transaction\n");
    2.16 +        free(err);
    2.17      }
    2.18  
    2.19      err = xenbus_printf(xbt, nodename, "ring-ref","%u",
    2.20 @@ -278,6 +279,7 @@ again:
    2.21  
    2.22  
    2.23      err = xenbus_transaction_end(xbt, 0, &retry);
    2.24 +    if (err) free(err);
    2.25      if (retry) {
    2.26              goto again;
    2.27          printk("completing transaction\n");
    2.28 @@ -286,7 +288,8 @@ again:
    2.29      goto done;
    2.30  
    2.31  abort_transaction:
    2.32 -    xenbus_transaction_end(xbt, 1, &retry);
    2.33 +    free(err);
    2.34 +    err = xenbus_transaction_end(xbt, 1, &retry);
    2.35      goto error;
    2.36  
    2.37  done:
    2.38 @@ -323,6 +326,8 @@ done:
    2.39      return dev;
    2.40  
    2.41  error:
    2.42 +    free(msg);
    2.43 +    free(err);
    2.44      free_consfront(dev);
    2.45      return NULL;
    2.46  }
     3.1 --- a/extras/mini-os/fbfront.c	Mon Dec 14 09:48:47 2009 +0000
     3.2 +++ b/extras/mini-os/fbfront.c	Mon Dec 14 09:51:07 2009 +0000
     3.3 @@ -71,7 +71,7 @@ struct kbdfront_dev *init_kbdfront(char 
     3.4      char* message=NULL;
     3.5      struct xenkbd_page *s;
     3.6      int retry=0;
     3.7 -    char* msg;
     3.8 +    char* msg = NULL;
     3.9      char* nodename = _nodename ? _nodename : "device/vkbd/0";
    3.10      struct kbdfront_dev *dev;
    3.11  
    3.12 @@ -102,6 +102,7 @@ again:
    3.13      err = xenbus_transaction_start(&xbt);
    3.14      if (err) {
    3.15          printk("starting transaction\n");
    3.16 +        free(err);
    3.17      }
    3.18  
    3.19      err = xenbus_printf(xbt, nodename, "page-ref","%u", virt_to_mfn(s));
    3.20 @@ -124,11 +125,13 @@ again:
    3.21  
    3.22      snprintf(path, sizeof(path), "%s/state", nodename);
    3.23      err = xenbus_switch_state(xbt, path, XenbusStateInitialised);
    3.24 -    if (err)
    3.25 +    if (err) {
    3.26          printk("error writing initialized: %s\n", err);
    3.27 -
    3.28 +        free(err);
    3.29 +    }
    3.30  
    3.31      err = xenbus_transaction_end(xbt, 0, &retry);
    3.32 +    if (err) free(err);
    3.33      if (retry) {
    3.34              goto again;
    3.35          printk("completing transaction\n");
    3.36 @@ -137,7 +140,8 @@ again:
    3.37      goto done;
    3.38  
    3.39  abort_transaction:
    3.40 -    xenbus_transaction_end(xbt, 1, &retry);
    3.41 +    free(err);
    3.42 +    err = xenbus_transaction_end(xbt, 1, &retry);
    3.43      goto error;
    3.44  
    3.45  done:
    3.46 @@ -186,6 +190,8 @@ done:
    3.47  
    3.48      return dev;
    3.49  error:
    3.50 +    free(msg);
    3.51 +    free(err);
    3.52      free_kbdfront(dev);
    3.53      return NULL;
    3.54  }
    3.55 @@ -247,6 +253,7 @@ void shutdown_kbdfront(struct kbdfront_d
    3.56      state = xenbus_read_integer(path);
    3.57      while (err == NULL && state < XenbusStateClosing)
    3.58          err = xenbus_wait_for_state_change(path, &state, &dev->events);
    3.59 +    if (err) free(err);
    3.60  
    3.61      if ((err = xenbus_switch_state(XBT_NIL, nodename, XenbusStateClosed)) != NULL) {
    3.62          printk("shutdown_kbdfront: error changing state to %d: %s\n",
    3.63 @@ -254,8 +261,10 @@ void shutdown_kbdfront(struct kbdfront_d
    3.64          goto close_kbdfront;
    3.65      }
    3.66      state = xenbus_read_integer(path);
    3.67 -    if (state < XenbusStateClosed)
    3.68 -        xenbus_wait_for_state_change(path, &state, &dev->events);
    3.69 +    if (state < XenbusStateClosed) {
    3.70 +        err = xenbus_wait_for_state_change(path, &state, &dev->events);
    3.71 +        if (err) free(err);
    3.72 +    }
    3.73  
    3.74      if ((err = xenbus_switch_state(XBT_NIL, nodename, XenbusStateInitialising)) != NULL) {
    3.75          printk("shutdown_kbdfront: error changing state to %d: %s\n",
    3.76 @@ -266,6 +275,7 @@ void shutdown_kbdfront(struct kbdfront_d
    3.77      //xenbus_wait_for_value(path, "2", &dev->events);
    3.78  
    3.79  close_kbdfront:
    3.80 +    if (err) free(err);
    3.81      xenbus_unwatch_path_token(XBT_NIL, path, path);
    3.82  
    3.83      snprintf(path, sizeof(path), "%s/page-ref", nodename);
    3.84 @@ -446,6 +456,7 @@ again:
    3.85      err = xenbus_transaction_start(&xbt);
    3.86      if (err) {
    3.87          printk("starting transaction\n");
    3.88 +        free(err);
    3.89      }
    3.90  
    3.91      err = xenbus_printf(xbt, nodename, "page-ref","%u", virt_to_mfn(s));
    3.92 @@ -478,6 +489,7 @@ again:
    3.93      }
    3.94  
    3.95      err = xenbus_transaction_end(xbt, 0, &retry);
    3.96 +    if (err) free(err);
    3.97      if (retry) {
    3.98              goto again;
    3.99          printk("completing transaction\n");
   3.100 @@ -486,7 +498,8 @@ again:
   3.101      goto done;
   3.102  
   3.103  abort_transaction:
   3.104 -    xenbus_transaction_end(xbt, 1, &retry);
   3.105 +    free(err);
   3.106 +    err = xenbus_transaction_end(xbt, 1, &retry);
   3.107      goto error;
   3.108  
   3.109  done:
   3.110 @@ -539,6 +552,7 @@ done:
   3.111      return dev;
   3.112  
   3.113  error:
   3.114 +    free(err);
   3.115      free_fbfront(dev);
   3.116      return NULL;
   3.117  }
   3.118 @@ -627,6 +641,7 @@ void shutdown_fbfront(struct fbfront_dev
   3.119      state = xenbus_read_integer(path);
   3.120      while (err == NULL && state < XenbusStateClosing)
   3.121          err = xenbus_wait_for_state_change(path, &state, &dev->events);
   3.122 +    if (err) free(err);
   3.123  
   3.124      if ((err = xenbus_switch_state(XBT_NIL, nodename, XenbusStateClosed)) != NULL) {
   3.125          printk("shutdown_fbfront: error changing state to %d: %s\n",
   3.126 @@ -634,8 +649,10 @@ void shutdown_fbfront(struct fbfront_dev
   3.127          goto close_fbfront;
   3.128      }
   3.129      state = xenbus_read_integer(path);
   3.130 -    if (state < XenbusStateClosed)
   3.131 +    if (state < XenbusStateClosed) {
   3.132          xenbus_wait_for_state_change(path, &state, &dev->events);
   3.133 +        if (err) free(err);
   3.134 +    }
   3.135  
   3.136      if ((err = xenbus_switch_state(XBT_NIL, nodename, XenbusStateInitialising)) != NULL) {
   3.137          printk("shutdown_fbfront: error changing state to %d: %s\n",
   3.138 @@ -646,6 +663,7 @@ void shutdown_fbfront(struct fbfront_dev
   3.139      //xenbus_wait_for_value(path, "2", &dev->events);
   3.140  
   3.141  close_fbfront:
   3.142 +    if (err) free(err);
   3.143      xenbus_unwatch_path_token(XBT_NIL, path, path);
   3.144  
   3.145      snprintf(path, sizeof(path), "%s/page-ref", nodename);
     4.1 --- a/extras/mini-os/fs-front.c	Mon Dec 14 09:48:47 2009 +0000
     4.2 +++ b/extras/mini-os/fs-front.c	Mon Dec 14 09:51:07 2009 +0000
     4.3 @@ -1103,6 +1103,7 @@ again:
     4.4      err = xenbus_transaction_start(&xbt);
     4.5      if (err) {
     4.6          printk("starting transaction\n");
     4.7 +        free(err);
     4.8      }
     4.9      
    4.10      err = xenbus_printf(xbt, 
    4.11 @@ -1140,9 +1141,10 @@ again:
    4.12      }
    4.13  
    4.14      err = xenbus_printf(xbt, nodename, "state", STATE_READY, 0xdeadbeef);
    4.15 +    if (err) free(err);
    4.16  
    4.17 -    
    4.18      err = xenbus_transaction_end(xbt, 0, &retry);
    4.19 +    if (err) free(err);
    4.20      if (retry) {
    4.21              goto again;
    4.22          printk("completing transaction\n");
    4.23 @@ -1159,7 +1161,9 @@ again:
    4.24      goto done;
    4.25  
    4.26  abort_transaction:
    4.27 -    xenbus_transaction_end(xbt, 1, &retry);
    4.28 +    free(err);
    4.29 +    err = xenbus_transaction_end(xbt, 1, &retry);
    4.30 +    if (err) free(err);
    4.31  
    4.32  done:
    4.33  
    4.34 @@ -1189,7 +1193,8 @@ done:
    4.35      sprintf(token, "fs-front-%d", import->import_id);
    4.36      /* The token will not be unique if multiple imports are inited */
    4.37      xenbus_watch_path_token(XBT_NIL, r_nodename, r_nodename, &events);
    4.38 -    xenbus_wait_for_value(r_nodename, STATE_READY, &events);
    4.39 +    err = xenbus_wait_for_value(r_nodename, STATE_READY, &events);
    4.40 +    if (err) free(err);
    4.41      xenbus_unwatch_path_token(XBT_NIL, r_nodename, r_nodename);
    4.42      printk("Backend ready.\n");
    4.43     
     5.1 --- a/extras/mini-os/netfront.c	Mon Dec 14 09:48:47 2009 +0000
     5.2 +++ b/extras/mini-os/netfront.c	Mon Dec 14 09:51:07 2009 +0000
     5.3 @@ -305,7 +305,7 @@ struct netfront_dev *init_netfront(char 
     5.4      struct netif_rx_sring *rxs;
     5.5      int retry=0;
     5.6      int i;
     5.7 -    char* msg;
     5.8 +    char* msg = NULL;
     5.9      char nodename[256];
    5.10      char path[256];
    5.11      struct netfront_dev *dev;
    5.12 @@ -377,6 +377,7 @@ again:
    5.13      err = xenbus_transaction_start(&xbt);
    5.14      if (err) {
    5.15          printk("starting transaction\n");
    5.16 +        free(err);
    5.17      }
    5.18  
    5.19      err = xenbus_printf(xbt, nodename, "tx-ring-ref","%u",
    5.20 @@ -413,6 +414,7 @@ again:
    5.21      }
    5.22  
    5.23      err = xenbus_transaction_end(xbt, 0, &retry);
    5.24 +    if (err) free(err);
    5.25      if (retry) {
    5.26              goto again;
    5.27          printk("completing transaction\n");
    5.28 @@ -421,7 +423,8 @@ again:
    5.29      goto done;
    5.30  
    5.31  abort_transaction:
    5.32 -    xenbus_transaction_end(xbt, 1, &retry);
    5.33 +    free(err);
    5.34 +    err = xenbus_transaction_end(xbt, 1, &retry);
    5.35      goto error;
    5.36  
    5.37  done:
    5.38 @@ -479,6 +482,8 @@ done:
    5.39  
    5.40      return dev;
    5.41  error:
    5.42 +    free(msg);
    5.43 +    free(err);
    5.44      free_netfront(dev);
    5.45      return NULL;
    5.46  }
    5.47 @@ -521,6 +526,7 @@ void shutdown_netfront(struct netfront_d
    5.48      state = xenbus_read_integer(path);
    5.49      while (err == NULL && state < XenbusStateClosing)
    5.50          err = xenbus_wait_for_state_change(path, &state, &dev->events);
    5.51 +    if (err) free(err);
    5.52  
    5.53      if ((err = xenbus_switch_state(XBT_NIL, nodename, XenbusStateClosed)) != NULL) {
    5.54          printk("shutdown_netfront: error changing state to %d: %s\n",
    5.55 @@ -528,8 +534,10 @@ void shutdown_netfront(struct netfront_d
    5.56          goto close;
    5.57      }
    5.58      state = xenbus_read_integer(path);
    5.59 -    if (state < XenbusStateClosed)
    5.60 -        xenbus_wait_for_state_change(path, &state, &dev->events);
    5.61 +    if (state < XenbusStateClosed) {
    5.62 +        err = xenbus_wait_for_state_change(path, &state, &dev->events);
    5.63 +        if (err) free(err);
    5.64 +    }
    5.65  
    5.66      if ((err = xenbus_switch_state(XBT_NIL, nodename, XenbusStateInitialising)) != NULL) {
    5.67          printk("shutdown_netfront: error changing state to %d: %s\n",
    5.68 @@ -542,6 +550,7 @@ void shutdown_netfront(struct netfront_d
    5.69          err = xenbus_wait_for_state_change(path, &state, &dev->events);
    5.70  
    5.71  close:
    5.72 +    if (err) free(err);
    5.73      xenbus_unwatch_path_token(XBT_NIL, path, path);
    5.74  
    5.75      snprintf(path, sizeof(path), "%s/tx-ring-ref", nodename);
     6.1 --- a/extras/mini-os/pcifront.c	Mon Dec 14 09:48:47 2009 +0000
     6.2 +++ b/extras/mini-os/pcifront.c	Mon Dec 14 09:51:07 2009 +0000
     6.3 @@ -181,6 +181,7 @@ again:
     6.4      err = xenbus_transaction_start(&xbt);
     6.5      if (err) {
     6.6          printk("starting transaction\n");
     6.7 +        free(err);
     6.8      }
     6.9  
    6.10      err = xenbus_printf(xbt, nodename, "pci-op-ref","%u",
    6.11 @@ -210,6 +211,7 @@ again:
    6.12      }
    6.13  
    6.14      err = xenbus_transaction_end(xbt, 0, &retry);
    6.15 +    if (err) free(err);
    6.16      if (retry) {
    6.17              goto again;
    6.18          printk("completing transaction\n");
    6.19 @@ -218,7 +220,8 @@ again:
    6.20      goto done;
    6.21  
    6.22  abort_transaction:
    6.23 -    xenbus_transaction_end(xbt, 1, &retry);
    6.24 +    free(err);
    6.25 +    err = xenbus_transaction_end(xbt, 1, &retry);
    6.26      goto error;
    6.27  
    6.28  done:
    6.29 @@ -268,6 +271,7 @@ done:
    6.30      return dev;
    6.31  
    6.32  error:
    6.33 +    free(err);
    6.34      free_pcifront(dev);
    6.35      return NULL;
    6.36  }
    6.37 @@ -341,6 +345,7 @@ void shutdown_pcifront(struct pcifront_d
    6.38      state = xenbus_read_integer(path);
    6.39      while (err == NULL && state < XenbusStateClosing)
    6.40          err = xenbus_wait_for_state_change(path, &state, &dev->events);
    6.41 +    if (err) free(err);
    6.42  
    6.43      if ((err = xenbus_switch_state(XBT_NIL, nodename, XenbusStateClosed)) != NULL) {
    6.44          printk("shutdown_pcifront: error changing state to %d: %s\n",
    6.45 @@ -348,8 +353,10 @@ void shutdown_pcifront(struct pcifront_d
    6.46          goto close_pcifront;
    6.47      }
    6.48      state = xenbus_read_integer(path);
    6.49 -    if (state < XenbusStateClosed)
    6.50 -        xenbus_wait_for_state_change(path, &state, &dev->events);
    6.51 +    if (state < XenbusStateClosed) {
    6.52 +        err = xenbus_wait_for_state_change(path, &state, &dev->events);
    6.53 +        free(err);
    6.54 +    }
    6.55  
    6.56      if ((err = xenbus_switch_state(XBT_NIL, nodename, XenbusStateInitialising)) != NULL) {
    6.57          printk("shutdown_pcifront: error changing state to %d: %s\n",
    6.58 @@ -362,6 +369,7 @@ void shutdown_pcifront(struct pcifront_d
    6.59          err = xenbus_wait_for_state_change(path, &state, &dev->events);
    6.60  
    6.61  close_pcifront:
    6.62 +    if (err) free(err);
    6.63      xenbus_unwatch_path_token(XBT_NIL, path, path);
    6.64  
    6.65      snprintf(path, sizeof(path), "%s/info-ref", nodename);
     7.1 --- a/extras/mini-os/xenbus/xenbus.c	Mon Dec 14 09:48:47 2009 +0000
     7.2 +++ b/extras/mini-os/xenbus/xenbus.c	Mon Dec 14 09:51:07 2009 +0000
     7.3 @@ -135,7 +135,8 @@ char *xenbus_switch_state(xenbus_transac
     7.4  
     7.5      do {
     7.6          if (xbt == XBT_NIL) {
     7.7 -            xenbus_transaction_start(&xbt);
     7.8 +            msg = xenbus_transaction_start(&xbt);
     7.9 +            if (msg) goto exit;
    7.10              xbt_flag = 1;
    7.11          }
    7.12