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

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