]> git.ipfire.org Git - thirdparty/open-vm-tools.git/commitdiff
Fix a Coverity-reported NULL pointer issue.
authorJohn Wolfe <jwolfe@vmware.com>
Wed, 19 Aug 2020 17:01:17 +0000 (10:01 -0700)
committerJohn Wolfe <jwolfe@vmware.com>
Wed, 19 Aug 2020 17:01:17 +0000 (10:01 -0700)
open-vm-tools/libguestlib/vmGuestLib.c

index 5cf0c811a026d5f690c0f9d5fc17e8999e4db8c5..8d911ba042b66437a8341b06fd1f02682b248b21 100644 (file)
@@ -1,5 +1,5 @@
 /*********************************************************
- * Copyright (C) 2005-2016,2019 VMware, Inc. All rights reserved.
+ * Copyright (C) 2005-2016,2019-2020 VMware, Inc. All rights reserved.
  *
  * This program is free software; you can redistribute it and/or modify it
  * under the terms of the GNU Lesser General Public License as published
@@ -40,7 +40,7 @@
 
 #define GUESTLIB_NAME "VMware Guest API"
 
-/* 
+/*
  * These are client side data structures, separate from the wire data formats
  * (VMGuestLibDataV[23]).
  */
@@ -60,7 +60,7 @@ typedef struct {
     * Statistics.
     *
     * dataSize is the size of the buffer pointed to by 'data'.
-    * For v2 protocol: 
+    * For v2 protocol:
     *   - 'data' points to VMGuestLibDataV2 struct,
     * For v3 protocol:
     *   - 'data' points to VMGuestLibStatisticsV3 struct.
@@ -353,7 +353,7 @@ VMGuestLibUpdateInfo(VMGuestLibHandle handle) // IN
    VMGuestLibError ret = VMGUESTLIB_ERROR_INVALID_ARG;
    uint32 hostVersion = HANDLE_VERSION(handle);
 
-   /* 
+   /*
     * Starting with the highest supported protocol (major) version, negotiate
     * down to the highest host supported version. Host supports minimum version
     * 2.
@@ -395,15 +395,20 @@ VMGuestLibUpdateInfo(VMGuestLibHandle handle) // IN
          break;
       }
 
-      /* 
-       * Host is older and doesn't support the requested protocol version.
-       * Request the highest version the host supports.
-       */
       Debug("Failed to retrieve info: %s\n", reply ? reply : "NULL");
 
-      if (hostVersion == 2 ||
+      /*
+       * See why the request failed and either attempt a recovery action or
+       * set an appropriate error code.  If the problem is that the host
+       * is older and doesn't support the requested protocol version, then
+       * try to determine the highest version the host supports and use that.
+       */
+      if (reply == NULL) {
+         ret = VMGUESTLIB_ERROR_OTHER;
+         break;
+      } else if (hostVersion == 2 ||
           Str_Strncmp(reply, "Unknown command", sizeof "Unknown command") == 0) {
-         /* 
+         /*
           * Host does not support this feature. Older (v2) host would return
           * "Unsupported version" if it doesn't recognize the requested version.
           *
@@ -414,18 +419,18 @@ VMGuestLibUpdateInfo(VMGuestLibHandle handle) // IN
          break;
       } else if (hostVersion == 3) {
          /*
-          * Host supports v2 at a minimum. If request for v3 fails, then just use
-          * v2, since v2 host does not send the highest supported version in the
-          * reply.
+          * Host supports v2 at a minimum. If request for v3 fails, then just
+          * use v2, since v2 host does not send the highest supported version
+          * in the reply.
           */
          hostVersion = 2;
          HANDLE_SESSIONID(handle) = 0;
          continue;
       } else if (!StrUtil_GetNextUintToken(&hostVersion, &index, reply, ":")) {
          /*
-          * v3 and onwards, the host returns the highest major version it supports,
-          * if the requested version is not supported. So parse out the host
-          * version from the reply and return error if it didn't.
+          * v3 and onwards, the host returns the highest major version it
+          * supports, if the requested version is not supported. So parse
+          * out the host version from the reply and return error if it didn't.
           */
          Debug("Bad reply received from host.\n");
          ret = VMGUESTLIB_ERROR_OTHER;
@@ -499,7 +504,7 @@ VMGuestLibUpdateInfo(VMGuestLibHandle handle) // IN
       HANDLE_VERSION(handle) = v3reply->hdr.version;
       HANDLE_SESSIONID(handle) = v3reply->hdr.sessionId;
 
-      /* 
+      /*
        * 1. Retrieve the length of the statistics array from the XDR encoded
        * part of the reply.
        */
@@ -510,7 +515,7 @@ VMGuestLibUpdateInfo(VMGuestLibHandle handle) // IN
          goto done;
       }
       if (count >= GUESTLIB_MAX_STATISTIC_ID) {
-         /* 
+         /*
           * Host has more than we can process. So process only what this side
           * can.
           */
@@ -544,7 +549,7 @@ VMGuestLibUpdateInfo(VMGuestLibHandle handle) // IN
       if (count >= v3stats->numStats) {
          ret = VMGUESTLIB_ERROR_SUCCESS;
       } else {
-         /* 
+         /*
           * Error while unmarshalling. Deep-free already unmarshalled
           * statistics and invalidate the data in the handle.
           */
@@ -670,7 +675,7 @@ VMGuestLibGetStatisticsV3(VMGuestLibHandle handle,   // IN
    VMGuestLibStatisticsV3 *stats = HANDLE_DATA(handle);
    uint32 statIdx = statId - 1;
 
-   /* 
+   /*
     * Check that the requested statistic is supported by the host. V3 host sends
     * all the available statistics, in order. So any statistics that were added
     * to this version of guestlib, but unsupported by the host, would not be