Pino Toscano
2018-Aug-06 14:06 UTC
[Libguestfs] [PATCH] btrfs_balance_status: delay allocation of 'ret'
Allocate 'ret' as late as possible, so there is no risk that early returns will leak it. --- daemon/btrfs.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/daemon/btrfs.c b/daemon/btrfs.c index 26757d4fb..5c4be6cf7 100644 --- a/daemon/btrfs.c +++ b/daemon/btrfs.c @@ -1683,12 +1683,6 @@ do_btrfs_balance_status (const char *path) nlines = guestfs_int_count_strings (lines); - ret = calloc (1, sizeof *ret); - if (ret == NULL) { - reply_with_perror ("calloc"); - return NULL; - } - /* Output of `btrfs balance status' is like: * * running: @@ -1711,6 +1705,12 @@ do_btrfs_balance_status (const char *path) return NULL; } + ret = calloc (1, sizeof *ret); + if (ret == NULL) { + reply_with_perror ("calloc"); + return NULL; + } + if (strstr (lines[0], "No balance found on")) { ret->btrfsbalance_status = strdup ("none"); if (ret->btrfsbalance_status == NULL) { -- 2.17.1
Richard W.M. Jones
2018-Aug-06 17:57 UTC
Re: [Libguestfs] [PATCH] btrfs_balance_status: delay allocation of 'ret'
On Mon, Aug 06, 2018 at 04:06:55PM +0200, Pino Toscano wrote:> Allocate 'ret' as late as possible, so there is no risk that early > returns will leak it. > --- > daemon/btrfs.c | 12 ++++++------ > 1 file changed, 6 insertions(+), 6 deletions(-) > > diff --git a/daemon/btrfs.c b/daemon/btrfs.c > index 26757d4fb..5c4be6cf7 100644 > --- a/daemon/btrfs.c > +++ b/daemon/btrfs.c > @@ -1683,12 +1683,6 @@ do_btrfs_balance_status (const char *path) > > nlines = guestfs_int_count_strings (lines); > > - ret = calloc (1, sizeof *ret); > - if (ret == NULL) { > - reply_with_perror ("calloc"); > - return NULL; > - } > - > /* Output of `btrfs balance status' is like: > * > * running: > @@ -1711,6 +1705,12 @@ do_btrfs_balance_status (const char *path) > return NULL; > } > > + ret = calloc (1, sizeof *ret); > + if (ret == NULL) { > + reply_with_perror ("calloc"); > + return NULL; > + } > + > if (strstr (lines[0], "No balance found on")) { > ret->btrfsbalance_status = strdup ("none"); > if (ret->btrfsbalance_status == NULL) { > -- > 2.17.1ACK, thanks. Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones Read my programming and virtualization blog: http://rwmj.wordpress.com Fedora Windows cross-compiler. Compile Windows programs, test, and build Windows installers. Over 100 libraries supported. http://fedoraproject.org/wiki/MinGW
Reasonably Related Threads
- [PATCH] daemon: btrfs: fix bad handling of a couple of errors
- [PATCH] daemon: add a space after func name to fit code-style
- [PATCH v3 1/2] New API: btrfs_balance_status
- [PATCH v4 1/2] New API: btrfs_balance_status
- Re: [PATCH v4 1/2] New API: btrfs_balance_status