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

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