Use malloc/memset not calloc for WinCE portability
authorRichard Levitte <levitte@openssl.org>
Thu, 26 Oct 2017 18:49:47 +0000 (20:49 +0200)
committerRichard Levitte <levitte@openssl.org>
Thu, 26 Oct 2017 20:34:32 +0000 (22:34 +0200)
Fixes: #2539

Reviewed-by: Rich Salz <rsalz@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/4594)

crypto/LPdir_win.c

index 07e63fb4244ee8f5270911e520dfe331f6f9f9dd..4961254d9a3192c5e8460a978ade05da01026dad 100644 (file)
@@ -94,8 +94,23 @@ const char *LP_find_file(LP_DIR_CTX **ctx, const char *directory)
             TCHAR *wdir = NULL;
             /* len_0 denotes string length *with* trailing 0 */
             size_t index = 0, len_0 = strlen(extdir) + 1;
-
-            wdir = (TCHAR *)calloc(len_0, sizeof(TCHAR));
+            size_t amount;
+
+            /*
+             * Size check
+             * The reasoning is that absolutely worst case, each byte in
+             * extdir will take up one TCHAR each, so the maximum size in
+             * bytes that we can tolerate is MAX_PATH TCHARs...  not counting
+             * the ending NUL.
+             */
+            if ((len_0 - 1) > MAX_PATH * sizeof(TCHAR)) {
+                free(*ctx);
+                *ctx = NULL;
+                errno = EINVAL;
+                return 0;
+            }
+            amount = len_0 * sizeof(TCHAR);
+            wdir = (TCHAR *)malloc(amount);
             if (wdir == NULL) {
                 if (extdirbuf != NULL) {
                     free(extdirbuf);