Blob Blame History Raw
From 48c296e7b9851c07ef277d7d37a05e20da3ef76d Mon Sep 17 00:00:00 2001
From: Gerd Hoffmann <kraxel@redhat.com>
Date: Tue, 17 Sep 2013 09:42:30 +0200
Subject: [PATCH 5/5] ahci: add missing check for allocation failure

RH-Author: Gerd Hoffmann <kraxel@redhat.com>
Message-id: <1379410950-22494-2-git-send-email-kraxel@redhat.com>
Patchwork-id: 54411
O-Subject: [RHEL-7 seabios PATCH 1/1] ahci: add missing check for allocation failure
Bugzilla: 1005747
RH-Acked-by: Bandan Das <bsd@redhat.com>
RH-Acked-by: Miroslav Rezanina <mrezanin@redhat.com>
RH-Acked-by: Paolo Bonzini <pbonzini@redhat.com>

Triggerable by creating a virtual machine with
*lots* of ahci controllers and disks.

Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
(cherry picked from commit ce12eaf2044d6aae08795403ecbb888d2b6527ff)
---
 src/ahci.c | 25 ++++++++++++++++---------
 1 file changed, 16 insertions(+), 9 deletions(-)

Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
---
 src/ahci.c |   25 ++++++++++++++++---------
 1 files changed, 16 insertions(+), 9 deletions(-)

diff --git a/src/ahci.c b/src/ahci.c
index 4a8eafb..056d894 100644
--- a/src/ahci.c
+++ b/src/ahci.c
@@ -381,12 +381,26 @@ ahci_port_alloc(struct ahci_ctrl_s *ctrl, u32 pnr)
     return port;
 }
 
+static void ahci_port_release(struct ahci_port_s *port)
+{
+    ahci_port_reset(port->ctrl, port->pnr);
+    free(port->list);
+    free(port->fis);
+    free(port->cmd);
+    free(port);
+}
+
 static struct ahci_port_s* ahci_port_realloc(struct ahci_port_s *port)
 {
     struct ahci_port_s *tmp;
     u32 cmd;
 
     tmp = malloc_fseg(sizeof(*port));
+    if (!tmp) {
+        warn_noalloc();
+        ahci_port_release(port);
+        return NULL;
+    }
     *tmp = *port;
     free(port);
     port = tmp;
@@ -410,15 +424,6 @@ static struct ahci_port_s* ahci_port_realloc(struct ahci_port_s *port)
     return port;
 }
 
-static void ahci_port_release(struct ahci_port_s *port)
-{
-    ahci_port_reset(port->ctrl, port->pnr);
-    free(port->list);
-    free(port->fis);
-    free(port->cmd);
-    free(port);
-}
-
 #define MAXMODEL 40
 
 /* See ahci spec chapter 10.1 "Software Initialization of HBA" */
@@ -554,6 +559,8 @@ ahci_port_detect(void *data)
         ahci_port_release(port);
     else {
         port = ahci_port_realloc(port);
+        if (port == NULL)
+            return;
         dprintf(1, "AHCI/%d: registering: \"%s\"\n", port->pnr, port->desc);
         if (!port->atapi) {
             // Register with bcv system.
-- 
1.7.1