Manage null input for empty password on CertSvcString 85/56385/1
authorKyungwook Tak <k.tak@samsung.com>
Thu, 7 Jan 2016 07:26:04 +0000 (16:26 +0900)
committerKyungwook Tak <k.tak@samsung.com>
Thu, 7 Jan 2016 07:26:04 +0000 (16:26 +0900)
Change-Id: Ia2ebb8ef2d9fa36ca70f54d834b3706baaee3f47
Signed-off-by: Kyungwook Tak <k.tak@samsung.com>
tests/capi/test-certificate.cpp
tests/pkcs12/new_test_cases.cpp
vcore/vcore/api.cpp

index ca76716..e77034f 100644 (file)
@@ -335,12 +335,17 @@ RUNNER_TEST(T0106_chain_sort)
 
        RUNNER_ASSERT_MSG(CERTSVC_SUCCESS == certsvc_certificate_chain_sort(collection, 3), "FAIL TO SORT CERTIFICATE");
 
-       RUNNER_ASSERT_MSG(collection[2].privateHandler == cert3.privateHandler, "certsvc_certificate_chain_sort failed");
+       RUNNER_ASSERT_MSG(
+               (memcmp(&collection[2], &cert3, sizeof(CertSvcCertificate)) == 0
+                       && memcmp(&collection[1], &cert2, sizeof(CertSvcCertificate)) == 0
+                       && memcmp(&collection[0], &cert1, sizeof(CertSvcCertificate)) == 0),
+               "certsvc_certificate_chain_sort success but it's not sorted really.");
 
        collection[0] = cert1;
        collection[1] = cert3;
 
-       RUNNER_ASSERT_MSG(CERTSVC_FAIL == certsvc_certificate_chain_sort(collection, 2), "certsvc_certificate_chain_sort failed");
+       RUNNER_ASSERT_MSG(CERTSVC_FAIL == certsvc_certificate_chain_sort(collection, 2),
+               "certsvc_certificate_chain_sort must be failed");
 }
 
 RUNNER_TEST_GROUP_INIT(T0200_CAPI_CERTIFICATE_VERIFY)
index 12fe993..5e60422 100644 (file)
@@ -46,15 +46,15 @@ static CertSvcInstance instance;
 static CertSvcString wrapper_certsvc_string_new(const char *cStr)
 {
        CertSvcString certsvcStr;
+       int retval;
 
-       if (!cStr) {
-               certsvcStr.privateHandler = NULL;
-               return certsvcStr;
-       }
+       if (cStr == NULL)
+               retval = certsvc_string_new(instance, NULL, 0, &certsvcStr);
+       else
+               retval = certsvc_string_new(instance, cStr, strlen(cStr), &certsvcStr);
 
-       RUNNER_ASSERT_MSG(
-               certsvc_string_new(instance, cStr, strlen(cStr), &certsvcStr) == CERTSVC_SUCCESS,
-               "Failed to certsvc_string_new");
+       RUNNER_ASSERT_MSG(retval == CERTSVC_SUCCESS,
+               "Failed to certsvc_string_new with retval: " << retval);
 
        return certsvcStr;
 }
index 3f7f9c7..417ec80 100644 (file)
@@ -635,21 +635,27 @@ public:
         size_t size,
         CertSvcString *output)
     {
-        if (!output) {
+        if (!output)
             return CERTSVC_WRONG_ARGUMENT;
-        }
 
-        size_t allocSize = size;
+        /* return struct for empty string */
+        if (size == 0 || str == NULL) {
+            output->privateHandler = NULL;
+            output->privateLength = 0;
+            output->privateInstance = instance;
 
-        if (allocSize == 0 || str[allocSize - 1] != 0)
-            allocSize++;
+            return CERTSVC_SUCCESS;
+        }
+
+        if (strlen(str) < size)
+            return CERTSVC_WRONG_ARGUMENT;
 
-        char *ptr = new char[allocSize];
+        char *ptr = new(std::nothrow) char[size + 1];
         if (ptr == NULL)
             return CERTSVC_BAD_ALLOC;
 
         memcpy(ptr, str, size);
-        ptr[allocSize - 1] = 0;
+        ptr[size] = '\0';
 
         output->privateHandler = ptr;
         output->privateLength = size;