]> git.ipfire.org Git - thirdparty/open-vm-tools.git/commitdiff
Use malloc everywhere in linux deploypkg plugin code
authorOliver Kurth <okurth@vmware.com>
Tue, 12 Nov 2019 02:12:22 +0000 (18:12 -0800)
committerOliver Kurth <okurth@vmware.com>
Tue, 12 Nov 2019 02:12:22 +0000 (18:12 -0800)
This change is replacing Util_SafeMalloc() by malloc() in Linux
deploypkg plugin code.  Also changing if statement to == NULL
or != NULL for char* type.

open-vm-tools/libDeployPkg/linuxDeployment.c
open-vm-tools/libDeployPkg/processPosix.c

index 97a69cfd74a010c71e9513bc6bf736b72682aa0d..3f86339bcd35a1695d938bb791a58d0060948c9d 100644 (file)
@@ -211,15 +211,19 @@ Panic(const char *fmtstr, ...)
 {
    va_list args;
 
-   char *tmp = Util_SafeMalloc(MAXSTRING);
+   char *tmp = malloc(MAXSTRING);
 
-   va_start(args, fmtstr);
-   Str_Vsnprintf(tmp, MAXSTRING, fmtstr, args);
-   va_end(args);
+   if (tmp != NULL) {
+      va_start(args, fmtstr);
+      Str_Vsnprintf(tmp, MAXSTRING, fmtstr, args);
+      va_end(args);
 
-   sLog(log_error, "Panic callback invoked: '%s'.\n", tmp);
+      sLog(log_error, "Panic callback invoked: '%s'.\n", tmp);
 
-   free(tmp);
+      free(tmp);
+   } else {
+      sLog(log_error, "Error allocating memory to log panic messages\n");
+   }
 
    exit(1);
 }
@@ -242,15 +246,19 @@ Debug(const char *fmtstr, ...)
 #ifdef VMX86_DEBUG
    va_list args;
 
-   char *tmp = Util_SafeMalloc(MAXSTRING);
+   char *tmp = malloc(MAXSTRING);
 
-   va_start(args, fmtstr);
-   Str_Vsnprintf(tmp, MAXSTRING, fmtstr, args);
-   va_end(args);
+   if (tmp != NULL) {
+      va_start(args, fmtstr);
+      Str_Vsnprintf(tmp, MAXSTRING, fmtstr, args);
+      va_end(args);
 
-   sLog(log_debug, "Debug callback invoked: '%s'.\n", tmp);
+      sLog(log_debug, "Debug callback invoked: '%s'.\n", tmp);
 
-   free(tmp);
+      free(tmp);
+   } else {
+      sLog(log_error, "Error allocating memory to log debug messages\n");
+   }
 #endif
 }
 
@@ -289,7 +297,7 @@ SetCustomizationStatusInVmxEx(int customizationState,
    size_t  responseLength = 0;
    Bool success;
 
-   if (errMsg) {
+   if (errMsg != NULL) {
       int msg_size = strlen(CABCOMMANDLOG) + 1 + strlen(errMsg) + 1;
       msg = malloc(msg_size);
       if (msg == NULL) {
@@ -432,13 +440,13 @@ SetDeployError(const char* format, ...)
 
    char* tmp = malloc(MAXSTRING);
 
-   if (tmp) {
+   if (tmp != NULL) {
       va_start(args, format);
       Str_Vsnprintf(tmp, MAXSTRING, format, args);
       va_end(args);
    }
 
-   if (gDeployError) {
+   if (gDeployError != NULL) {
       free(gDeployError);
       gDeployError = NULL;
    }
@@ -497,7 +505,7 @@ AddToList(struct List* head, const char* token)
    sLog(log_debug, "Adding to list '%s'.\n", token);
 #endif
    data = malloc(strlen(token) + 1);
-   if (!data) {
+   if (data == NULL) {
       SetDeployError("Error allocating memory. (%s)", strerror(errno));
       return NULL;
    }
@@ -505,7 +513,7 @@ AddToList(struct List* head, const char* token)
    strcpy(data, token);
 
    l = malloc(sizeof(struct List));
-   if (!l) {
+   if (l == NULL) {
       SetDeployError("Error allocating memory. (%s)", strerror(errno));
       // clear allocated resource
       free(data);
@@ -520,7 +528,7 @@ AddToList(struct List* head, const char* token)
       tail = tail->next;
    }
 
-   if (tail) {
+   if (tail != NULL) {
       tail->next = l;
    }
 
@@ -652,7 +660,7 @@ GetPackageInfo(const char* packageName,
 
    // Create space and copy the command
    *command = malloc(VMWAREDEPLOYPKG_CMD_LENGTH);
-   if (!*command) {
+   if (*command == NULL) {
       SetDeployError("Error allocating memory.");
       return FALSE;
    }
@@ -703,7 +711,7 @@ Touch(const char* state)
    int fd;
 
    sLog(log_info, "ENTER STATE '%s'.\n", state);
-   if (!fileName) {
+   if (fileName == NULL) {
       SetDeployError("Error allocating memory.");
       return DEPLOYPKG_STATUS_ERROR;
    }
@@ -746,7 +754,7 @@ UnTouch(const char* state)
    int result;
 
    sLog(log_info, "EXIT STATE '%s'.\n", state);
-   if (!fileName) {
+   if (fileName == NULL) {
       SetDeployError("Error allocating memory.");
       return DEPLOYPKG_STATUS_ERROR;
    }
@@ -789,7 +797,7 @@ TransitionState(const char* stateFrom, const char* stateTo)
    sLog(log_info, "Transitioning from state '%s' to state '%s'.\n", stateFrom, stateTo);
 
    // Create a file to indicate state to
-   if (stateTo) {
+   if (stateTo != NULL) {
       if (Touch(stateTo) == DEPLOYPKG_STATUS_ERROR) {
          SetDeployError("Error creating new state '%s'.(%s)", stateTo, GetDeployError());
          return DEPLOYPKG_STATUS_ERROR;
@@ -797,7 +805,7 @@ TransitionState(const char* stateFrom, const char* stateTo)
    }
 
    // Remove the old state file
-   if (stateFrom) {
+   if (stateFrom != NULL) {
       if (UnTouch(stateFrom) == DEPLOYPKG_STATUS_ERROR) {
          SetDeployError("Error deleting old state '%s'.(%s)", stateFrom, GetDeployError());
          return DEPLOYPKG_STATUS_ERROR;
@@ -1349,7 +1357,7 @@ Deploy(const char* packageName)
       } else {
          char *nics = GetNicsToEnable(imcDirPath);
 
-         if (nics) {
+         if (nics != NULL) {
             // XXX: Sleep before the last SetCustomizationStatusInVmx
             //      This is a temporary-hack for PR 422790
             sleep(5);
@@ -1374,7 +1382,7 @@ Deploy(const char* packageName)
    }
    cleanupCommandSize = strlen(CLEANUPCMD) + strlen(imcDirPath) + 1;
    cleanupCommand = malloc(cleanupCommandSize);
-   if (!cleanupCommand) {
+   if (cleanupCommand == NULL) {
       SetDeployError("Error allocating memory.");
       free(imcDirPath);
       return DEPLOYPKG_STATUS_ERROR;
@@ -1458,7 +1466,7 @@ ExtractCabPackage(const char* cabFileName,
    }
 
    // check if cab file is set
-   if (!cabFileName) {
+   if (cabFileName == NULL) {
       SetDeployError("Cab file not set.");
       return FALSE;
    }
@@ -1599,7 +1607,7 @@ GetFormattedCommandLine(const char* command)
 
    // prefixing the start path for the commands
    args = malloc((ListSize(commandTokens) + 1) * sizeof(char*));
-   if (!args) {
+   if (args == NULL) {
       SetDeployError("Error allocating memory.");
       // clear resources
       DeleteList(commandTokens);
@@ -1608,7 +1616,7 @@ GetFormattedCommandLine(const char* command)
 
    for(l = commandTokens, i = 0; l; l = l->next, i++) {
       char* arg = malloc(strlen(l->data) + 1);
-      if (!arg) {
+      if (arg == NULL) {
          unsigned int j;
          SetDeployError("Error allocating memory. (%s)", strerror(errno));
          // free allocated memories in previous iterations if any
index 340b05b0174eb77ee11644f6f724393bf471bc28..1caf6334fa455d1712bebbf23a56e2546bba7a36 100644 (file)
@@ -28,6 +28,7 @@
 #include <unistd.h>
 #include <fcntl.h>
 #include <sys/wait.h>
+#include <stdlib.h>
 
 #include "util.h"
 
@@ -67,13 +68,26 @@ ProcessError
 Process_Create(ProcessHandle *h, char *args[], void *logPtr)
 {
    int i, numArgs;
+   int err = -1;
    ProcessInternal *p;
    LogFunction log = (LogFunction)logPtr;
    log(log_info, "sizeof ProcessInternal is %d\n", sizeof(ProcessInternal));
-   p = Util_SafeMalloc(sizeof(ProcessInternal));
-   p->stdoutStr = Util_SafeMalloc(sizeof(char));
+   p = (ProcessInternal*) calloc(1, sizeof(ProcessInternal));
+   if (p == NULL) {
+      log(log_error, "Error allocating memory for process\n");
+      goto error;
+   }
+   p->stdoutStr = malloc(sizeof(char));
+   if (p->stdoutStr == NULL) {
+      log(log_error, "Error allocating memory for process stdout\n");
+      goto error;
+   }
    p->stdoutStr[0] = '\0';
-   p->stderrStr = Util_SafeMalloc(sizeof(char));
+   p->stderrStr = malloc(sizeof(char));
+   if (p->stderrStr == NULL) {
+      log(log_error, "Error allocating memory for process stderr\n");
+      goto error;
+   }
    p->stderrStr[0] = '\0';
 
    p->stdoutFd = -1;
@@ -84,13 +98,30 @@ Process_Create(ProcessHandle *h, char *args[], void *logPtr)
       numArgs++;
    }
 
-   p->args = Util_SafeMalloc((1 + numArgs) * sizeof(char*));
+   p->args = malloc((1 + numArgs) * sizeof(char*));
+   if (p->args == NULL) {
+      log(log_error, "Error allocating memory for process args\n");
+      goto error;
+   }
    for (i = 0; i < numArgs; i++) {
-      p->args[i] = Util_SafeStrdup(args[i]);
+      p->args[i] = strdup(args[i]);
+      if (p->args[i] == NULL) {
+         log(log_error, "Error allocating memory for duplicate args\n");
+         goto error;
+      }
    }
-   p->args[numArgs] = (char*)0;
+   p->args[numArgs] = NULL;
    p->log = log;
    *h = (ProcessHandle)p;
+   err = 0;
+
+error:
+   if (err != 0) {
+      if (p != NULL) {
+         Process_Destroy((ProcessHandle)p);
+      }
+      exit(1);
+   }
    return PROCESS_SUCCESS;
 }
 
@@ -150,7 +181,7 @@ Process_RunToComplete(ProcessHandle h, unsigned long timeoutSec)
    } else if (p->pid == 0) {
       // we're in the child. close the read ends of the pipes and exec
       close(stdout[0]);
-      close(stderr[0]);  
+      close(stderr[0]);
       dup2(stdout[1], STDOUT_FILENO);
       dup2(stderr[1], STDERR_FILENO);
       execv(p->args[0], p->args);
@@ -395,10 +426,12 @@ Process_Destroy(ProcessHandle h)
    }
    free(p->stdoutStr);
    free(p->stderrStr);
-   for (i = 0; p->args[i] != NULL; i++) {
-      free(p->args[i]);
+   if (p->args != NULL) {
+      for (i = 0; p->args[i] != NULL; i++) {
+         free(p->args[i]);
+      }
+      free(p->args);
    }
-   free(p->args);
    free(p);
    return PROCESS_SUCCESS;
 }