Fix: replace strdup with memcpy 47/179947/2
authorDariusz Michaluk <d.michaluk@samsung.com>
Wed, 23 May 2018 11:16:06 +0000 (13:16 +0200)
committerDariusz Michaluk <d.michaluk@samsung.com>
Wed, 13 Jun 2018 13:40:26 +0000 (15:40 +0200)
Change-Id: Ie8923d089b5cce03b5d9a354a7ed6d0a173fc509

dcm-client/device_certificate_manager.cpp

index 1ee2896..83381ee 100644 (file)
@@ -122,8 +122,10 @@ int dcm_get_certificate_chain(const void *key_ctx, char **cert_chain, size_t *ce
        std::vector<uint8_t> cert;
        int result = context->connection->get_certificate_chain(cert);
        if(result == DCM_ERROR_NONE) {
-               if(!(*cert_chain = strdup(reinterpret_cast<const char*>(cert.data()))))
+               *cert_chain = (char*)malloc(sizeof(uint8_t) * cert.size());
+               if(*cert_chain == NULL)
                        return DCM_ERROR_OUT_OF_MEMORY;
+               memcpy(*cert_chain, cert.data(), cert.size());
                *cert_chain_len = cert.size();
        }
 
@@ -153,8 +155,10 @@ int dcm_get_key_type(const void *key_ctx, char **key_type)
                reinterpret_cast<const dcm_key_context_internal *>(key_ctx);
 
        std::string type = context->connection->key_type();
-       if(!(*key_type = strdup(type.c_str())))
+       *key_type = (char*)malloc(sizeof(char) * (type.length() + 1));
+       if(*key_type == NULL)
                return DCM_ERROR_OUT_OF_MEMORY;
+       memcpy(*key_type, type.c_str(), type.length() + 1);
 
        return DCM_ERROR_NONE;
 }
@@ -176,8 +180,10 @@ int dcm_create_signature(const void *key_ctx, dcm_digest_algorithm_e md,
                if(digest.size() > MBEDTLS_MPI_MAX_SIZE)
                        return DCM_ERROR_INVALID_PARAMETER;
 
-               if(!(*signature = strdup(reinterpret_cast<const char*>(digest.data()))))
+               *signature = (char*)malloc(sizeof(uint8_t) * digest.size());
+               if(*signature == NULL)
                        return DCM_ERROR_OUT_OF_MEMORY;
+               memcpy(*signature, digest.data(), digest.size());
                *signature_len = digest.size();
        }