Blame SOURCES/winpr-crt-Fix-wcs-cmp-and-wcs-len-checks.patch

1069f6
From fb9d753af70b449dd7a17898d46fd57822a08dc1 Mon Sep 17 00:00:00 2001
1069f6
From: akallabeth <akallabeth@posteo.net>
1069f6
Date: Thu, 10 Nov 2022 14:21:22 +0100
1069f6
Subject: [PATCH] [winpr, crt] Fix wcs*cmp and wcs*len checks
1069f6
1069f6
(cherry picked from commit b60fac1a0470fe83e8d0b448f0fd7e9e6d6a0f96)
1069f6
---
1069f6
 winpr/libwinpr/crt/string.c | 30 +++++++++++++++++++-----------
1069f6
 1 file changed, 19 insertions(+), 11 deletions(-)
1069f6
1069f6
diff --git a/winpr/libwinpr/crt/string.c b/winpr/libwinpr/crt/string.c
1069f6
index c25ffa279..5dcf4b3f1 100644
1069f6
--- a/winpr/libwinpr/crt/string.c
1069f6
+++ b/winpr/libwinpr/crt/string.c
1069f6
@@ -26,6 +26,7 @@
1069f6
 #include <wctype.h>
1069f6
 
1069f6
 #include <winpr/crt.h>
1069f6
+#include <assert.h>
1069f6
 #include <winpr/endian.h>
1069f6
 
1069f6
 /* String Manipulation (CRT): http://msdn.microsoft.com/en-us/library/f0151s4x.aspx */
1069f6
@@ -80,21 +81,28 @@ int _strnicmp(const char* string1, const char* string2, size_t count)
1069f6
 
1069f6
 int _wcscmp(const WCHAR* string1, const WCHAR* string2)
1069f6
 {
1069f6
-	WCHAR value1, value2;
1069f6
+	assert(string1);
1069f6
+	assert(string2);
1069f6
 
1069f6
-	while (*string1 && (*string1 == *string2))
1069f6
+	while (TRUE)
1069f6
 	{
1069f6
-		string1++;
1069f6
-		string2++;
1069f6
+		const WCHAR w1 = *string1++;
1069f6
+		const WCHAR w2 = *string2++;
1069f6
+
1069f6
+		if (w1 != w2)
1069f6
+			return (int)w1 - w2;
1069f6
+		else if ((w1 == '\0') || (w2 == '\0'))
1069f6
+			return (int)w1 - w2;
1069f6
 	}
1069f6
 
1069f6
-	Data_Read_UINT16(string1, value1);
1069f6
-	Data_Read_UINT16(string2, value2);
1069f6
-	return (int)value1 - value2;
1069f6
+	return 0;
1069f6
 }
1069f6
 
1069f6
 int _wcsncmp(const WCHAR* string1, const WCHAR* string2, size_t count)
1069f6
 {
1069f6
+	assert(string1);
1069f6
+	assert(string2);
1069f6
+
1069f6
 	for (size_t x = 0; x < count; x++)
1069f6
 	{
1069f6
 		const WCHAR a = string1[x];
1069f6
@@ -102,6 +110,8 @@ int _wcsncmp(const WCHAR* string1, const WCHAR* string2, size_t count)
1069f6
 
1069f6
 		if (a != b)
1069f6
 			return (int)a - b;
1069f6
+		else if ((a == '\0') || (b == '\0'))
1069f6
+			return (int)a - b;
1069f6
 	}
1069f6
 	return 0;
1069f6
 }
1069f6
@@ -112,8 +122,7 @@ size_t _wcslen(const WCHAR* str)
1069f6
 {
1069f6
 	const WCHAR* p = (const WCHAR*)str;
1069f6
 
1069f6
-	if (!p)
1069f6
-		return 0;
1069f6
+	assert(p);
1069f6
 
1069f6
 	while (*p)
1069f6
 		p++;
1069f6
@@ -127,8 +136,7 @@ size_t _wcsnlen(const WCHAR* str, size_t max)
1069f6
 {
1069f6
 	size_t x;
1069f6
 
1069f6
-	if (!str)
1069f6
-		return 0;
1069f6
+	assert(str);
1069f6
 
1069f6
 	for (x = 0; x < max; x++)
1069f6
 	{
1069f6
-- 
1069f6
2.37.1
1069f6