]> git.proxmox.com Git - libtpms.git/commitdiff
Use malloc/free rather than TPM_Malloc/TPM_Free in library code
authorStefan Berger <stefanb@linux.vnet.ibm.com>
Fri, 15 Jun 2018 21:41:33 +0000 (17:41 -0400)
committerStefan Berger <stefanb@linux.vnet.ibm.com>
Fri, 15 Jun 2018 22:02:04 +0000 (18:02 -0400)
Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
src/tpm_library.c
src/tpm_library_intern.h
src/tpm_tpm12_interface.c

index 30362841beee2decc9b47dbdc956c050b9d6012d..57c979e48c66fd23b472dd1adada703145c6fe39 100644 (file)
@@ -240,7 +240,6 @@ static unsigned char *TPMLIB_OpenSSL_Base64Decode(char *input,
     BIO *b64, *bmem;
     unsigned char *res = NULL;
     int n;
-    TPM_RESULT rc;
 
     b64 = BIO_new(BIO_f_base64());
     if (!b64) {
@@ -255,14 +254,15 @@ static unsigned char *TPMLIB_OpenSSL_Base64Decode(char *input,
     bmem = BIO_push(b64, bmem);
     BIO_set_flags(bmem, BIO_FLAGS_BASE64_NO_NL);
 
-    rc = TPM_Malloc(&res, outputlen);
-    if (rc != TPM_SUCCESS) {
+    res = malloc(outputlen);
+    if (!res) {
+        TPMLIB_LogError("Could not allocate %u bytes.\n", outputlen);
         goto cleanup;
     }
 
     n = BIO_read(bmem, res, outputlen);
     if (n <= 0) {
-        TPM_Free(res);
+        free(res);
         res = NULL;
         goto cleanup;
     }
@@ -296,8 +296,12 @@ static unsigned char *TPMLIB_Base64Decode(const char *start, const char *end,
 
     end++;
 
-    if (TPM_Malloc((unsigned char **)&input, end - start + 1) != TPM_SUCCESS)
+    input = malloc(end - start + 1);
+    if (!input) {
+        TPMLIB_LogError("Could not allocate %u bytes.\n",
+                        (unsigned int)(end - start + 1));
         return NULL;
+    }
 
     /* copy from source string skipping '\n' and '\r' and using
        '=' to calculate the exact length */
@@ -537,9 +541,13 @@ TPM_RESULT CopyCachedState(enum TPMLIB_StateType st,
     *is_empty_buffer = (*buflen == BUFLEN_EMPTY_BUFFER);
 
     if (cached_blobs[st].buffer) {
-        ret = TPM_Malloc(buffer, *buflen);
-        if (ret == TPM_SUCCESS)
+        *buffer = malloc(*buflen);
+        if (!*buffer) {
+            TPMLIB_LogError("Could not allocate %u bytes.\n", *buflen);
+            ret = TPM_SIZE;
+        } else {
             memcpy(*buffer, cached_blobs[st].buffer, *buflen);
+        }
     } else {
         *buffer = NULL;
     }
index af4dc4463a76a3c5ee16d633aa7e141ba5d9ead4..171036c226335aa9677f5c37f2b52feb9ff4255e 100644 (file)
@@ -90,6 +90,8 @@ uint32_t TPM12_GetBufferSize(void);
 int TPMLIB_LogPrintf(const char *format, ...);
 void TPMLIB_LogPrintfA(unsigned int indent, const char *format, ...);
 
+#define TPMLIB_LogError(format, ...) \
+     TPMLIB_LogPrintfA(~0, "libtpms: "format, __VA_ARGS__)
 #define TPMLIB_LogTPM12Error(format, ...) \
      TPMLIB_LogPrintfA(~0, "libtpms/tpm12: "format, __VA_ARGS__)
 
index 59b3295713451ae3019303b4be2f6e9aa5041c4f..dbd1ed519cc43488e99c895d974f347210329088 100644 (file)
@@ -445,14 +445,23 @@ TPM_RESULT TPM12_SetState(enum TPMLIB_StateType st,
         return TPM_INVALID_POSTINIT;
 
     if (ret == TPM_SUCCESS) {
-        ret = TPM_Malloc((unsigned char **)&stream, buflen);
+        stream = malloc(buflen);
+        if (!stream) {
+            TPMLIB_LogError("Could not allocate %u bytes.\n", buflen);
+            ret = TPM_SIZE;
+        }
     }
 
     if (ret == TPM_SUCCESS) {
         orig_stream = stream;
         memcpy(stream, buffer, buflen);
 
-        ret = TPM_Malloc((unsigned char **)&tpm_state, sizeof(tpm_state_t));
+        tpm_state = malloc(sizeof(tpm_state_t));
+        if (!tpm_state) {
+            TPMLIB_LogError("Could not allocated %zu bytes.\n",
+                            sizeof(tpm_state_t));
+            ret = TPM_SIZE;
+        }
     }
 
     if (ret == TPM_SUCCESS) {