qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: malc <av1474@comtv.ru>
To: qemu-devel@nongnu.org
Subject: [Qemu-devel] [6359] Avoid calling qemu_mallocz with zero size
Date: Fri, 16 Jan 2009 22:32:34 +0000	[thread overview]
Message-ID: <E1LNxEw-0001Xn-0a@cvs.savannah.gnu.org> (raw)

Revision: 6359
          http://svn.sv.gnu.org/viewvc/?view=rev&root=qemu&revision=6359
Author:   malc
Date:     2009-01-16 22:32:33 +0000 (Fri, 16 Jan 2009)

Log Message:
-----------
Avoid calling qemu_mallocz with zero size

Currently qemu_mallocz calls malloc and handling of zero by malloc is
implementation defined behaviour:
http://www.opengroup.org/onlinepubs/7990989775/xsh/malloc.html

malloc(0) on AIX returns NULL[1] and qcow2 images without snapshots
are thus unusable

[1] Unless special Linux compatibility define is used when compiling

Modified Paths:
--------------
    trunk/block-qcow2.c

Modified: trunk/block-qcow2.c
===================================================================
--- trunk/block-qcow2.c	2009-01-16 21:48:20 UTC (rev 6358)
+++ trunk/block-qcow2.c	2009-01-16 22:32:33 UTC (rev 6359)
@@ -1809,6 +1809,12 @@
     int64_t offset;
     uint32_t extra_data_size;
 
+    if (!s->nb_snapshots) {
+        s->snapshots = NULL;
+        s->snapshots_size = 0;
+        return 0;
+    }
+
     offset = s->snapshots_offset;
     s->snapshots = qemu_mallocz(s->nb_snapshots * sizeof(QCowSnapshot));
     if (!s->snapshots)
@@ -2023,8 +2029,10 @@
     snapshots1 = qemu_malloc((s->nb_snapshots + 1) * sizeof(QCowSnapshot));
     if (!snapshots1)
         goto fail;
-    memcpy(snapshots1, s->snapshots, s->nb_snapshots * sizeof(QCowSnapshot));
-    qemu_free(s->snapshots);
+    if (s->snapshots) {
+        memcpy(snapshots1, s->snapshots, s->nb_snapshots * sizeof(QCowSnapshot));
+        qemu_free(s->snapshots);
+    }
     s->snapshots = snapshots1;
     s->snapshots[s->nb_snapshots++] = *sn;
 

                 reply	other threads:[~2009-01-16 22:32 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=E1LNxEw-0001Xn-0a@cvs.savannah.gnu.org \
    --to=av1474@comtv.ru \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).