Blame SOURCES/Fixed-format-string-for-Stream_CheckAndLogRequiredLe.patch

1069f6
From 6ed2f7d1a379f69cca102e8166d20eb5ed38652b Mon Sep 17 00:00:00 2001
1069f6
From: akallabeth <akallabeth@posteo.net>
1069f6
Date: Fri, 22 Apr 2022 16:27:21 +0200
1069f6
Subject: [PATCH] Fixed format string for Stream_CheckAndLogRequiredLength
1069f6
1069f6
__LINE__ requires %d and not %PRIuz
1069f6
1069f6
(cherry picked from commit 74c1a006e940308b0653427d25a87ea5a24cb573)
1069f6
---
1069f6
 winpr/include/winpr/stream.h  | 14 ++++++++
1069f6
 winpr/libwinpr/utils/stream.c | 65 +++++++++++++++++++++++++++++++++++
1069f6
 2 files changed, 79 insertions(+)
1069f6
1069f6
diff --git a/winpr/include/winpr/stream.h b/winpr/include/winpr/stream.h
1069f6
index f351eaa15..ed637f034 100644
1069f6
--- a/winpr/include/winpr/stream.h
1069f6
+++ b/winpr/include/winpr/stream.h
1069f6
@@ -27,6 +27,8 @@
1069f6
 #include <winpr/wtypes.h>
1069f6
 #include <winpr/endian.h>
1069f6
 #include <winpr/synch.h>
1069f6
+#include <winpr/wlog.h>
1069f6
+#include <winpr/debug.h>
1069f6
 
1069f6
 #ifdef __cplusplus
1069f6
 extern "C"
1069f6
@@ -56,6 +57,19 @@ extern "C"
1069f6
 	WINPR_API void Stream_StaticInit(wStream* s, BYTE* buffer, size_t size);
1069f6
 	WINPR_API void Stream_Free(wStream* s, BOOL bFreeBuffer);
1069f6
 
1069f6
+#define Stream_CheckAndLogRequiredLength(tag, s, len)                                     \
1069f6
+	Stream_CheckAndLogRequiredLengthEx(tag, WLOG_WARN, s, len, "%s(%s:%d)", __FUNCTION__, \
1069f6
+	                                   __FILE__, __LINE__)
1069f6
+	WINPR_API BOOL Stream_CheckAndLogRequiredLengthEx(const char* tag, DWORD level, wStream* s,
1069f6
+	                                                  UINT64 len, const char* fmt, ...);
1069f6
+	WINPR_API BOOL Stream_CheckAndLogRequiredLengthExVa(const char* tag, DWORD level, wStream* s,
1069f6
+	                                                    UINT64 len, const char* fmt, va_list args);
1069f6
+	WINPR_API BOOL Stream_CheckAndLogRequiredLengthWLogEx(wLog* log, DWORD level, wStream* s,
1069f6
+	                                                      UINT64 len, const char* fmt, ...);
1069f6
+	WINPR_API BOOL Stream_CheckAndLogRequiredLengthWLogExVa(wLog* log, DWORD level, wStream* s,
1069f6
+	                                                        UINT64 len, const char* fmt,
1069f6
+	                                                        va_list args);
1069f6
+
1069f6
 	static INLINE void Stream_Seek(wStream* s, size_t _offset)
1069f6
 	{
1069f6
 		s->pointer += (_offset);
1069f6
diff --git a/winpr/libwinpr/utils/stream.c b/winpr/libwinpr/utils/stream.c
1069f6
index 1271981b7..cc119c771 100644
1069f6
--- a/winpr/libwinpr/utils/stream.c
1069f6
+++ b/winpr/libwinpr/utils/stream.c
1069f6
@@ -132,3 +132,68 @@ void Stream_Free(wStream* s, BOOL bFreeBuffer)
1069f6
 			free(s);
1069f6
 	}
1069f6
 }
1069f6
+
1069f6
+BOOL Stream_CheckAndLogRequiredLengthEx(const char* tag, DWORD level, wStream* s, UINT64 len,
1069f6
+                                        const char* fmt, ...)
1069f6
+{
1069f6
+	const size_t actual = Stream_GetRemainingLength(s);
1069f6
+
1069f6
+	if (actual < len)
1069f6
+	{
1069f6
+		va_list args;
1069f6
+
1069f6
+		va_start(args, fmt);
1069f6
+		Stream_CheckAndLogRequiredLengthExVa(tag, level, s, len, fmt, args);
1069f6
+		va_end(args);
1069f6
+
1069f6
+		return FALSE;
1069f6
+	}
1069f6
+	return TRUE;
1069f6
+}
1069f6
+
1069f6
+BOOL Stream_CheckAndLogRequiredLengthExVa(const char* tag, DWORD level, wStream* s, UINT64 len,
1069f6
+                                          const char* fmt, va_list args)
1069f6
+{
1069f6
+	const size_t actual = Stream_GetRemainingLength(s);
1069f6
+
1069f6
+	if (actual < len)
1069f6
+		return Stream_CheckAndLogRequiredLengthWLogExVa(WLog_Get(tag), level, s, len, fmt, args);
1069f6
+	return TRUE;
1069f6
+}
1069f6
+
1069f6
+BOOL Stream_CheckAndLogRequiredLengthWLogEx(wLog* log, DWORD level, wStream* s, UINT64 len,
1069f6
+                                            const char* fmt, ...)
1069f6
+{
1069f6
+	const size_t actual = Stream_GetRemainingLength(s);
1069f6
+
1069f6
+	if (actual < len)
1069f6
+	{
1069f6
+		va_list args;
1069f6
+
1069f6
+		va_start(args, fmt);
1069f6
+		Stream_CheckAndLogRequiredLengthWLogExVa(log, level, s, len, fmt, args);
1069f6
+		va_end(args);
1069f6
+
1069f6
+		return FALSE;
1069f6
+	}
1069f6
+	return TRUE;
1069f6
+}
1069f6
+
1069f6
+BOOL Stream_CheckAndLogRequiredLengthWLogExVa(wLog* log, DWORD level, wStream* s, UINT64 len,
1069f6
+                                              const char* fmt, va_list args)
1069f6
+{
1069f6
+	const size_t actual = Stream_GetRemainingLength(s);
1069f6
+
1069f6
+	if (actual < len)
1069f6
+	{
1069f6
+		char prefix[1024] = { 0 };
1069f6
+
1069f6
+		vsnprintf(prefix, sizeof(prefix), fmt, args);
1069f6
+
1069f6
+		WLog_Print(log, level, "[%s] invalid length, got %" PRIuz ", require at least %" PRIu64,
1069f6
+		           prefix, actual, len);
1069f6
+		winpr_log_backtrace_ex(log, level, 20);
1069f6
+		return FALSE;
1069f6
+	}
1069f6
+	return TRUE;
1069f6
+}
1069f6
-- 
1069f6
2.38.1
1069f6