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

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