On Tue, Jun 02, 2015 at 03:08:44PM +0100, Richard W.M. Jones wrote:
On Fri, May 29, 2015 at 12:24:38PM +0300, Nikos Skalkotos wrote:
> + /* Merge the src mappings to dst */
> + mappings = realloc (dst->drive_mappings, (n + 1) * sizeof (char *));
> + if (mappings == NULL) {
> + perrorf (g, "realloc");
> + return -1;
> + }
[...]
> + n = dst->nr_fstab + src->nr_fstab;
> + fstab = realloc (dst->fstab, n * sizeof (struct inspect_fstab_entry));
> + if (fstab == NULL) {
> + perrorf (g, "realloc");
> + return -1;
> + }
These both leak the original pointers on failure, and also leave the
dst / src structures in a half-merged state.
Since allocation failures are unlikely to be recoverable, just call
`safe_realloc (g, ...)' instead. It calls the per-handle out of
memory handler (guestfs_set_out_of_memory_handler) on failure.
And in case that's not clear, you can just ignore the error case when
using safe_realloc.
Rich.
--
Richard Jones, Virtualization Group, Red Hat
http://people.redhat.com/~rjones
Read my programming and virtualization blog:
http://rwmj.wordpress.com
virt-p2v converts physical machines to virtual machines. Boot with a
live CD or over the network (PXE) and turn machines into KVM guests.
http://libguestfs.org/virt-v2v