From 30b4e2a145c10004cdecebb49f9dbf543500d22d Mon Sep 17 00:00:00 2001
From: dlukes <dlukes@4ea69e1a-61f1-4043-bf83-b5c94c648137>
Date: Mon, 21 Dec 2009 08:54:43 +0000
Subject: [PATCH] check reallocf() result for failures

git-svn-id: https://smartmontools.svn.sourceforge.net/svnroot/smartmontools/trunk@3008 4ea69e1a-61f1-4043-bf83-b5c94c648137
---
 smartmontools/CHANGELOG      | 2 ++
 smartmontools/os_freebsd.cpp | 6 ++++++
 2 files changed, 8 insertions(+)

diff --git a/smartmontools/CHANGELOG b/smartmontools/CHANGELOG
index 64b69d811..b217deac9 100644
--- a/smartmontools/CHANGELOG
+++ b/smartmontools/CHANGELOG
@@ -43,6 +43,8 @@ NOTES FOR FUTURE RELEASES: see TODO file.
 
 <DEVELOPERS: ADDITIONS TO THE CHANGE LOG GO JUST BELOW HERE, PLEASE>
 
+  [DL] FreeBSD: check reallocf() result for failures
+
   [AS] FreeBSD: fixing crash on kFreeBSD (#29), patch provided by Petr Salinger
 
   [CF] Makefile.am: 'make check' now tests the syntax of drivedb.h.
diff --git a/smartmontools/os_freebsd.cpp b/smartmontools/os_freebsd.cpp
index a439b3d23..b06ca5ed9 100644
--- a/smartmontools/os_freebsd.cpp
+++ b/smartmontools/os_freebsd.cpp
@@ -1535,6 +1535,12 @@ int get_dev_names_ata(char*** names) {
     };
   };  
   mp = (char **)reallocf(mp,n*(sizeof (char*))); // shrink to correct size
+  if (mp == NULL && n > 0 ) { // reallocf never fail for size=0, but may return NULL
+    serrno=errno;
+    pout("Out of memory constructing scan device list (on line %d)\n", __LINE__);
+    n = -1;
+    goto end;
+  };
   bytes += (n)*(sizeof(char*)); // and set allocated byte count
 
 end:
-- 
GitLab