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

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