[libvirt] [PATCH] util.c (two more): don't use a negative value as allocation size

Jim Meyering jim at meyering.net
Mon Feb 1 20:50:06 UTC 2010


This change is nearly identical to one I made last week.
I inspected all remaining sysconf uses, and they seem to be ok.

>From 77add8762c5d00db11bb588b7ea0c9e00975c183 Mon Sep 17 00:00:00 2001
From: Jim Meyering <meyering at redhat.com>
Date: Mon, 1 Feb 2010 21:45:06 +0100
Subject: [PATCH] util.c (two more): don't use a negative value as allocation size

* src/util/util.c (virGetUserID, virGetGroupID): In the unlikely event
that sysconf(_SC_GETPW_R_SIZE_MAX) fails, don't use -1 as the size in
the subsequent allocation.
---
 src/util/util.c |   16 ++++++++++++++--
 1 files changed, 14 insertions(+), 2 deletions(-)

diff --git a/src/util/util.c b/src/util/util.c
index 3c200d3..cf1290d 100644
--- a/src/util/util.c
+++ b/src/util/util.c
@@ -2388,11 +2388,17 @@ int virGetUserID(virConnectPtr conn,
                  uid_t *uid)
 {
     char *strbuf;
     struct passwd pwbuf;
     struct passwd *pw = NULL;
-    size_t strbuflen = sysconf(_SC_GETPW_R_SIZE_MAX);
+    long val = sysconf(_SC_GETPW_R_SIZE_MAX);
+    size_t strbuflen = val;
+
+    if (val < 0) {
+        virReportSystemError(conn, errno, "%s", _("sysconf failed"));
+        return -1;
+    }

     if (VIR_ALLOC_N(strbuf, strbuflen) < 0) {
         virReportOOMError(conn);
         return -1;
     }
@@ -2425,11 +2431,17 @@ int virGetGroupID(virConnectPtr conn,
                   gid_t *gid)
 {
     char *strbuf;
     struct group grbuf;
     struct group *gr = NULL;
-    size_t strbuflen = sysconf(_SC_GETGR_R_SIZE_MAX);
+    long val = sysconf(_SC_GETGR_R_SIZE_MAX);
+    size_t strbuflen = val;
+
+    if (val < 0) {
+        virReportSystemError(conn, errno, "%s", _("sysconf failed"));
+        return -1;
+    }

     if (VIR_ALLOC_N(strbuf, strbuflen) < 0) {
         virReportOOMError(conn);
         return -1;
     }
--
1.7.0.rc1.149.g0b0b7




More information about the libvir-list mailing list