[PATCH] crypt32: Avoid calling strlen on constants

Alex Henrie alexhenrie24 at gmail.com
Tue Feb 5 00:12:08 CST 2019


Signed-off-by: Alex Henrie <alexhenrie24 at gmail.com>
---
 dlls/crypt32/base64.c      |   2 +-
 dlls/crypt32/object.c      | 105 +++++++++++++------------------------
 dlls/crypt32/protectdata.c |   2 +-
 dlls/crypt32/rootstore.c   |   4 +-
 dlls/crypt32/str.c         |   4 +-
 5 files changed, 43 insertions(+), 74 deletions(-)

diff --git a/dlls/crypt32/base64.c b/dlls/crypt32/base64.c
index d158c1aceb..6d18f97811 100644
--- a/dlls/crypt32/base64.c
+++ b/dlls/crypt32/base64.c
@@ -872,7 +872,7 @@ static LONG Base64WithHeaderAndTrailerToBinaryW(LPCWSTR pszString,
     {
         return ERROR_INVALID_DATA;
     }
-    dataBegins += strlenW(CERT_DELIMITER_W);
+    dataBegins += ARRAY_SIZE(CERT_DELIMITER_W) - 1;
     if (*dataBegins == '\r') dataBegins++;
     if (*dataBegins == '\n') dataBegins++;
 
diff --git a/dlls/crypt32/object.c b/dlls/crypt32/object.c
index 1993a0a879..10df55cae7 100644
--- a/dlls/crypt32/object.c
+++ b/dlls/crypt32/object.c
@@ -904,7 +904,7 @@ static BOOL CRYPT_FormatBits(BYTE bits, const struct BitToString *map,
         if (bits & map[i].bit)
         {
             if (!localFirst)
-                bytesNeeded += strlenW(commaSpace) * sizeof(WCHAR);
+                bytesNeeded += 2 * sizeof(WCHAR); /* comma and space */
             localFirst = FALSE;
             bytesNeeded += strlenW(map[i].str) * sizeof(WCHAR);
         }
@@ -932,7 +932,7 @@ static BOOL CRYPT_FormatBits(BYTE bits, const struct BitToString *map,
                 if (!localFirst)
                 {
                     strcpyW(str, commaSpace);
-                    str += strlenW(commaSpace);
+                    str += 2;
                 }
                 localFirst = FALSE;
                 strcpyW(str, map[i].str);
@@ -1090,21 +1090,15 @@ static BOOL WINAPI CRYPT_FormatBasicConstraints2(DWORD dwCertEncodingType,
     {
         static const WCHAR pathFmt[] = { '%','d',0 };
         static BOOL stringsLoaded = FALSE;
+        static const DWORD sepLen = 2 * sizeof(WCHAR);
         DWORD bytesNeeded = sizeof(WCHAR); /* space for the NULL terminator */
         WCHAR pathLength[MAX_STRING_RESOURCE_LEN];
         LPCWSTR sep, subjectType;
-        DWORD sepLen;
 
         if (dwFormatStrType & CRYPT_FORMAT_STR_MULTI_LINE)
-        {
             sep = crlf;
-            sepLen = strlenW(crlf) * sizeof(WCHAR);
-        }
         else
-        {
             sep = commaSpace;
-            sepLen = strlenW(commaSpace) * sizeof(WCHAR);
-        }
 
         if (!stringsLoaded)
         {
@@ -1217,7 +1211,7 @@ static BOOL CRYPT_FormatAltNameEntry(DWORD dwFormatStrType, DWORD indentLevel,
     DWORD strType = CERT_X500_NAME_STR | CERT_NAME_STR_REVERSE_FLAG;
 
     if (dwFormatStrType & CRYPT_FORMAT_STR_MULTI_LINE)
-        bytesNeeded += indentLevel * strlenW(indent) * sizeof(WCHAR);
+        bytesNeeded += indentLevel * (ARRAY_SIZE(indent) - 1) * sizeof(WCHAR);
     switch (entry->dwAltNameChoice)
     {
     case CERT_ALT_NAME_RFC822_NAME:
@@ -1274,14 +1268,14 @@ static BOOL CRYPT_FormatAltNameEntry(DWORD dwFormatStrType, DWORD indentLevel,
                  entry->u.IPAddress.pbData[3]);
                 bytesNeeded += strlenW(ipAddrBuf) * sizeof(WCHAR);
                 /* indent again, for the mask line */
-                bytesNeeded += indentLevel * strlenW(indent) * sizeof(WCHAR);
+                bytesNeeded += indentLevel * (ARRAY_SIZE(indent) - 1) * sizeof(WCHAR);
                 sprintfW(maskBuf, ipAddrFmt,
                  entry->u.IPAddress.pbData[4],
                  entry->u.IPAddress.pbData[5],
                  entry->u.IPAddress.pbData[6],
                  entry->u.IPAddress.pbData[7]);
                 bytesNeeded += strlenW(maskBuf) * sizeof(WCHAR);
-                bytesNeeded += strlenW(crlf) * sizeof(WCHAR);
+                bytesNeeded += 2 * sizeof(WCHAR); /* carriage return and line feed */
             }
             else
             {
@@ -1331,7 +1325,7 @@ static BOOL CRYPT_FormatAltNameEntry(DWORD dwFormatStrType, DWORD indentLevel,
                 for (i = 0; i < indentLevel; i++)
                 {
                     strcpyW(str, indent);
-                    str += strlenW(indent);
+                    str += ARRAY_SIZE(indent) - 1;
                 }
             }
             strcpyW(str, buf);
@@ -1361,13 +1355,13 @@ static BOOL CRYPT_FormatAltNameEntry(DWORD dwFormatStrType, DWORD indentLevel,
                     strcpyW(str, ipAddrBuf);
                     str += strlenW(ipAddrBuf);
                     strcpyW(str, crlf);
-                    str += strlenW(crlf);
+                    str += 2;
                     if (dwFormatStrType & CRYPT_FORMAT_STR_MULTI_LINE)
                     {
                         for (i = 0; i < indentLevel; i++)
                         {
                             strcpyW(str, indent);
-                            str += strlenW(indent);
+                            str += ARRAY_SIZE(indent) - 1;
                         }
                     }
                     strcpyW(str, mask);
@@ -1386,21 +1380,15 @@ static BOOL CRYPT_FormatAltNameEntry(DWORD dwFormatStrType, DWORD indentLevel,
 static BOOL CRYPT_FormatAltNameInfo(DWORD dwFormatStrType, DWORD indentLevel,
  const CERT_ALT_NAME_INFO *name, LPWSTR str, DWORD *pcbStr)
 {
+    static const DWORD sepLen = 2 * sizeof(WCHAR);
     DWORD i, size, bytesNeeded = 0;
     BOOL ret = TRUE;
     LPCWSTR sep;
-    DWORD sepLen;
 
     if (dwFormatStrType & CRYPT_FORMAT_STR_MULTI_LINE)
-    {
         sep = crlf;
-        sepLen = strlenW(crlf) * sizeof(WCHAR);
-    }
     else
-    {
         sep = commaSpace;
-        sepLen = strlenW(commaSpace) * sizeof(WCHAR);
-    }
 
     for (i = 0; ret && i < name->cAltEntry; i++)
     {
@@ -1446,7 +1434,7 @@ static BOOL CRYPT_FormatAltNameInfo(DWORD dwFormatStrType, DWORD indentLevel,
     return ret;
 }
 
-static const WCHAR colonSep[] = { ':',' ',0 };
+static const WCHAR colonSpace[] = { ':',' ',0 };
 
 static BOOL WINAPI CRYPT_FormatAltName(DWORD dwCertEncodingType,
  DWORD dwFormatType, DWORD dwFormatStrType, void *pFormatStruct,
@@ -1481,12 +1469,12 @@ static BOOL CRYPT_FormatCertIssuer(DWORD dwFormatStrType,
     if (dwFormatStrType & CRYPT_FORMAT_STR_MULTI_LINE)
     {
         sep = colonCrlf;
-        sepLen = strlenW(colonCrlf) * sizeof(WCHAR);
+        sepLen = 3 * sizeof(WCHAR);
     }
     else
     {
-        sep = colonSep;
-        sepLen = strlenW(colonSep) * sizeof(WCHAR);
+        sep = colonSpace;
+        sepLen = 2 * sizeof(WCHAR);
     }
     bytesNeeded += sepLen;
     if (ret)
@@ -1531,21 +1519,15 @@ static BOOL WINAPI CRYPT_FormatAuthorityKeyId2(DWORD dwCertEncodingType,
     if ((ret = CryptDecodeObjectEx(dwCertEncodingType, X509_AUTHORITY_KEY_ID2,
      pbEncoded, cbEncoded, CRYPT_DECODE_ALLOC_FLAG, NULL, &info, &size)))
     {
+        static const DWORD sepLen = 2 * sizeof(WCHAR);
         DWORD bytesNeeded = sizeof(WCHAR); /* space for the NULL terminator */
         LPCWSTR sep;
-        DWORD sepLen;
         BOOL needSeparator = FALSE;
 
         if (dwFormatStrType & CRYPT_FORMAT_STR_MULTI_LINE)
-        {
             sep = crlf;
-            sepLen = strlenW(crlf) * sizeof(WCHAR);
-        }
         else
-        {
             sep = commaSpace;
-            sepLen = strlenW(commaSpace) * sizeof(WCHAR);
-        }
 
         if (info->KeyId.cbData)
         {
@@ -1721,7 +1703,7 @@ static BOOL WINAPI CRYPT_FormatAuthorityInfoAccess(DWORD dwCertEncodingType,
             }
             else
             {
-                headingSep = colonSep;
+                headingSep = colonSpace;
                 accessMethodSep = commaSpace;
                 locationSep = equal;
             }
@@ -1734,11 +1716,11 @@ static BOOL WINAPI CRYPT_FormatAuthorityInfoAccess(DWORD dwCertEncodingType,
                 bytesNeeded += strlenW(accessDescrNum) * sizeof(WCHAR);
                 bytesNeeded += sizeof(WCHAR); /* right bracket */
                 bytesNeeded += strlenW(aia) * sizeof(WCHAR);
-                bytesNeeded += strlenW(headingSep) * sizeof(WCHAR);
+                bytesNeeded += 2 * sizeof(WCHAR); /* separator */
                 /* Access method */
                 bytesNeeded += strlenW(accessMethod) * sizeof(WCHAR);
                 if (dwFormatStrType & CRYPT_FORMAT_STR_MULTI_LINE)
-                    bytesNeeded += strlenW(indent) * sizeof(WCHAR);
+                    bytesNeeded += (ARRAY_SIZE(indent) - 1) * sizeof(WCHAR);
                 if (!strcmp(info->rgAccDescr[i].pszAccessMethod,
                  szOID_PKIX_OCSP))
                     bytesNeeded += strlenW(ocsp) * sizeof(WCHAR);
@@ -1753,9 +1735,9 @@ static BOOL WINAPI CRYPT_FormatAuthorityInfoAccess(DWORD dwCertEncodingType,
                  * sizeof(WCHAR);
                 bytesNeeded += sizeof(WCHAR); /* right paren */
                 /* Delimiter between access method and location */
-                bytesNeeded += strlenW(accessMethodSep) * sizeof(WCHAR);
+                bytesNeeded += 2 * sizeof(WCHAR); /* separator */
                 if (dwFormatStrType & CRYPT_FORMAT_STR_MULTI_LINE)
-                    bytesNeeded += strlenW(indent) * sizeof(WCHAR);
+                    bytesNeeded += (ARRAY_SIZE(indent) - 1) * sizeof(WCHAR);
                 bytesNeeded += strlenW(accessLocation) * sizeof(WCHAR);
                 bytesNeeded += strlenW(locationSep) * sizeof(WCHAR);
                 ret = CRYPT_FormatAltNameEntry(dwFormatStrType, 2,
@@ -1764,7 +1746,7 @@ static BOOL WINAPI CRYPT_FormatAuthorityInfoAccess(DWORD dwCertEncodingType,
                     bytesNeeded += size - sizeof(WCHAR);
                 /* Need extra delimiter between access method entries */
                 if (i < info->cAccDescr - 1)
-                    bytesNeeded += strlenW(accessMethodSep) * sizeof(WCHAR);
+                    bytesNeeded += 2 * sizeof(WCHAR); /* separator */
             }
             if (ret)
             {
@@ -1798,7 +1780,7 @@ static BOOL WINAPI CRYPT_FormatAuthorityInfoAccess(DWORD dwCertEncodingType,
                         if (dwFormatStrType & CRYPT_FORMAT_STR_MULTI_LINE)
                         {
                             strcpyW(str, indent);
-                            str += strlenW(indent);
+                            str += ARRAY_SIZE(indent) - 1;
                         }
                         strcpyW(str, accessMethod);
                         str += strlenW(accessMethod);
@@ -1826,11 +1808,11 @@ static BOOL WINAPI CRYPT_FormatAuthorityInfoAccess(DWORD dwCertEncodingType,
                             *str = *oidPtr;
                         *str++ = ')';
                         strcpyW(str, accessMethodSep);
-                        str += strlenW(accessMethodSep);
+                        str += 2;
                         if (dwFormatStrType & CRYPT_FORMAT_STR_MULTI_LINE)
                         {
                             strcpyW(str, indent);
-                            str += strlenW(indent);
+                            str += ARRAY_SIZE(indent) - 1;
                         }
                         strcpyW(str, accessLocation);
                         str += strlenW(accessLocation);
@@ -1849,7 +1831,7 @@ static BOOL WINAPI CRYPT_FormatAuthorityInfoAccess(DWORD dwCertEncodingType,
                         if (i < info->cAccDescr - 1)
                         {
                             strcpyW(str, accessMethodSep);
-                            str += strlenW(accessMethodSep);
+                            str += 2;
                         }
                     }
                 }
@@ -1888,7 +1870,6 @@ static struct reason_map_entry reason_map[] = {
 static BOOL CRYPT_FormatReason(DWORD dwFormatStrType,
  const CRYPT_BIT_BLOB *reasonFlags, LPWSTR str, DWORD *pcbStr)
 {
-    static const WCHAR sep[] = { ',',' ',0 };
     static const WCHAR bitsFmt[] = { ' ','(','%','0','2','x',')',0 };
     static BOOL stringsLoaded = FALSE;
     unsigned int i, numReasons = 0;
@@ -1912,7 +1893,7 @@ static BOOL CRYPT_FormatReason(DWORD dwFormatStrType,
         {
             bytesNeeded += strlenW(reason_map[i].reason) * sizeof(WCHAR);
             if (numReasons++)
-                bytesNeeded += strlenW(sep) * sizeof(WCHAR);
+                bytesNeeded += 2 * sizeof(WCHAR); /* comma and space */
         }
     }
     sprintfW(bits, bitsFmt, reasonFlags->pbData[0]);
@@ -1936,8 +1917,8 @@ static BOOL CRYPT_FormatReason(DWORD dwFormatStrType,
                 str += strlenW(reason_map[i].reason);
                 if (i < ARRAY_SIZE(reason_map) - 1 && numReasons)
                 {
-                    strcpyW(str, sep);
-                    str += strlenW(sep);
+                    strcpyW(str, commaSpace);
+                    str += 2;
                 }
             }
         }
@@ -1996,7 +1977,7 @@ static BOOL WINAPI CRYPT_FormatCRLDistPoints(DWORD dwCertEncodingType,
         }
         else
         {
-            headingSep = colonSep;
+            headingSep = colonSpace;
             nameSep = colon;
         }
 
@@ -2016,7 +1997,7 @@ static BOOL WINAPI CRYPT_FormatCRLDistPoints(DWORD dwCertEncodingType,
                     bytesNeeded += strlenW(rdnName) * sizeof(WCHAR);
                 bytesNeeded += strlenW(nameSep) * sizeof(WCHAR);
                 if (dwFormatStrType & CRYPT_FORMAT_STR_MULTI_LINE)
-                    bytesNeeded += 2 * strlenW(indent) * sizeof(WCHAR);
+                    bytesNeeded += 2 * (ARRAY_SIZE(indent) - 1) * sizeof(WCHAR);
                 /* The indent level (3) is higher than when used as the issuer,
                  * because the name is subordinate to the name type (full vs.
                  * RDN.)
@@ -2053,9 +2034,9 @@ static BOOL WINAPI CRYPT_FormatCRLDistPoints(DWORD dwCertEncodingType,
                 bytesNeeded += strlenW(distPointNum) * sizeof(WCHAR);
                 bytesNeeded += sizeof(WCHAR); /* right bracket */
                 bytesNeeded += strlenW(crlDistPoint) * sizeof(WCHAR);
-                bytesNeeded += strlenW(headingSep) * sizeof(WCHAR);
+                bytesNeeded += 2 * sizeof(WCHAR); /* separator */
                 if (dwFormatStrType & CRYPT_FORMAT_STR_MULTI_LINE)
-                    bytesNeeded += strlenW(indent) * sizeof(WCHAR);
+                    bytesNeeded += (ARRAY_SIZE(indent) - 1) * sizeof(WCHAR);
             }
         }
         if (!haveAnEntry)
@@ -2109,7 +2090,7 @@ static BOOL WINAPI CRYPT_FormatCRLDistPoints(DWORD dwCertEncodingType,
                     if (dwFormatStrType & CRYPT_FORMAT_STR_MULTI_LINE)
                     {
                         strcpyW(str, indent);
-                        str += strlenW(indent);
+                        str += ARRAY_SIZE(indent) - 1;
                     }
                     if (distPoint->DistPointName.dwDistPointNameChoice !=
                      CRL_DIST_POINT_NO_NAME)
@@ -2123,9 +2104,9 @@ static BOOL WINAPI CRYPT_FormatCRLDistPoints(DWORD dwCertEncodingType,
                         if (dwFormatStrType & CRYPT_FORMAT_STR_MULTI_LINE)
                         {
                             strcpyW(str, indent);
-                            str += strlenW(indent);
+                            str += ARRAY_SIZE(indent) - 1;
                             strcpyW(str, indent);
-                            str += strlenW(indent);
+                            str += ARRAY_SIZE(indent) - 1;
                         }
                         if (distPoint->DistPointName.dwDistPointNameChoice ==
                          CRL_DIST_POINT_FULL_NAME)
@@ -2196,22 +2177,16 @@ static BOOL WINAPI CRYPT_FormatEnhancedKeyUsage(DWORD dwCertEncodingType,
     if ((ret = CryptDecodeObjectEx(dwCertEncodingType, X509_ENHANCED_KEY_USAGE,
      pbEncoded, cbEncoded, CRYPT_DECODE_ALLOC_FLAG, NULL, &usage, &size)))
     {
+        static const DWORD sepLen = 2 * sizeof(WCHAR);
         WCHAR unknown[MAX_STRING_RESOURCE_LEN];
         DWORD i;
         DWORD bytesNeeded = sizeof(WCHAR); /* space for the NULL terminator */
         LPCWSTR sep;
-        DWORD sepLen;
 
         if (dwFormatStrType & CRYPT_FORMAT_STR_MULTI_LINE)
-        {
             sep = crlf;
-            sepLen = strlenW(crlf) * sizeof(WCHAR);
-        }
         else
-        {
             sep = commaSpace;
-            sepLen = strlenW(commaSpace) * sizeof(WCHAR);
-        }
 
         LoadStringW(hInstance, IDS_USAGE_UNKNOWN, unknown, ARRAY_SIZE(unknown));
         for (i = 0; i < usage->cUsageIdentifier; i++)
@@ -2406,10 +2381,10 @@ static BOOL WINAPI CRYPT_FormatSpcFinancialCriteria(DWORD dwCertEncodingType,
      SPC_FINANCIAL_CRITERIA_STRUCT, pbEncoded, cbEncoded, 0, NULL, &criteria,
      &size)))
     {
+        static const DWORD sepLen = 2 * sizeof(WCHAR);
         static BOOL stringsLoaded = FALSE;
         DWORD bytesNeeded = sizeof(WCHAR);
         LPCWSTR sep;
-        DWORD sepLen;
 
         if (!stringsLoaded)
         {
@@ -2422,15 +2397,9 @@ static BOOL WINAPI CRYPT_FormatSpcFinancialCriteria(DWORD dwCertEncodingType,
             stringsLoaded = TRUE;
         }
         if (dwFormatStrType & CRYPT_FORMAT_STR_MULTI_LINE)
-        {
             sep = crlf;
-            sepLen = strlenW(crlf) * sizeof(WCHAR);
-        }
         else
-        {
             sep = commaSpace;
-            sepLen = strlenW(commaSpace) * sizeof(WCHAR);
-        }
         bytesNeeded += strlenW(financialCriteria) * sizeof(WCHAR);
         if (criteria.fFinancialInfoAvailable)
         {
diff --git a/dlls/crypt32/protectdata.c b/dlls/crypt32/protectdata.c
index 20e11f457c..355f1f6777 100644
--- a/dlls/crypt32/protectdata.c
+++ b/dlls/crypt32/protectdata.c
@@ -515,7 +515,7 @@ BOOL valid_protect_data(const struct protect_data_t *pInfo)
      * there to be reasonably sure we're using data created by the Wine
      * implementation of CryptProtectData.
      */
-    if (pInfo->info0.cbData!=strlen(crypt_magic_str)+1 ||
+    if (pInfo->info0.cbData != sizeof(crypt_magic_str) ||
         strcmp( (LPCSTR)pInfo->info0.pbData,crypt_magic_str) != 0)
     {
         ERR("info0 magic value not matched !\n");
diff --git a/dlls/crypt32/rootstore.c b/dlls/crypt32/rootstore.c
index e7ed4e9bea..7b0ba45a81 100644
--- a/dlls/crypt32/rootstore.c
+++ b/dlls/crypt32/rootstore.c
@@ -117,13 +117,13 @@ static BOOL import_base64_certs_from_fp(FILE *fp, HCERTSTORE store)
         static const char header[] = "-----BEGIN CERTIFICATE-----";
         static const char trailer[] = "-----END CERTIFICATE-----";
 
-        if (!strncmp(line, header, strlen(header)))
+        if (!strncmp(line, header, ARRAY_SIZE(header) - 1))
         {
             TRACE("begin new certificate\n");
             in_cert = TRUE;
             reset_buffer(&saved_cert);
         }
-        else if (!strncmp(line, trailer, strlen(trailer)))
+        else if (!strncmp(line, trailer, ARRAY_SIZE(trailer) - 1))
         {
             DWORD size;
 
diff --git a/dlls/crypt32/str.c b/dlls/crypt32/str.c
index 4843c4f5f1..2c0ccd3196 100644
--- a/dlls/crypt32/str.c
+++ b/dlls/crypt32/str.c
@@ -594,7 +594,8 @@ DWORD cert_name_to_str_with_indent(DWORD dwCertEncodingType, DWORD indentLevel,
      pName->cbData, CRYPT_DECODE_ALLOC_FLAG, NULL, &info, &bytes);
     if (bRet)
     {
-        DWORD i, j, sepLen, rdnSepLen;
+        static const DWORD sepLen = 2;
+        DWORD i, j, rdnSepLen;
         LPCWSTR sep, rdnSep;
         BOOL reverse = dwStrType & CERT_NAME_STR_REVERSE_FLAG;
         const CERT_RDN *rdn = info->rgRDN;
@@ -607,7 +608,6 @@ DWORD cert_name_to_str_with_indent(DWORD dwCertEncodingType, DWORD indentLevel,
             sep = crlfSep;
         else
             sep = commaSep;
-        sepLen = lstrlenW(sep);
         if (dwStrType & CERT_NAME_STR_NO_PLUS_FLAG)
             rdnSep = spaceSep;
         else
-- 
2.20.1




More information about the wine-devel mailing list