From f85047dab9f000b625864eb930e11f650831ef7c Mon Sep 17 00:00:00 2001 From: Simon Rozman Date: Tue, 19 Nov 2024 10:44:14 +0100 Subject: [PATCH] COM: Test rather than assert We generally don't care when our functions are called with invalid arguments (validation should be performed on arguments input, not every use). However, we do use asserts to validate arguments in Debug builds. Unfortunately, this makes Code Analysis unhappy in the Release builds. Let's make an exception on this one, since the check is not expensive compared to the rest of the function. Signed-off-by: Simon Rozman --- include/WinStd/COM.h | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/include/WinStd/COM.h b/include/WinStd/COM.h index cde7aee7..8088e7ac 100644 --- a/include/WinStd/COM.h +++ b/include/WinStd/COM.h @@ -1472,12 +1472,14 @@ namespace winstd /// inline VARIANT BuildVBARRAY(_In_ VARTYPE vt, _In_opt_ LPCVOID array, _In_ ULONG columns, _In_ ULONG rows) { + const size_t n = columns * rows; + if (!array && n) + throw std::invalid_argument("array is NULL"); + LPSAFEARRAY sa; - size_t n; if (columns == 1) { // Make vector when one column only. sa = SafeArrayCreateVector(VT_VARIANT, 0, rows); - n = rows; } else { // Make 2-dimensional array when more columns. @@ -1486,11 +1488,9 @@ namespace winstd { rows, 0 } }; sa = SafeArrayCreate(VT_VARIANT, 2, dim); - n = columns * rows; } if (!sa) throw std::bad_alloc(); - assert(array || !n); // Support VARIANT types that may be used for SAFEARRAY // Source: https://learn.microsoft.com/en-us/windows/win32/api/wtypes/ne-wtypes-varenum#remarks @@ -1557,7 +1557,8 @@ namespace winstd template VARIANT BuildVBARRAY(_In_reads_opt_(rows) const T* array, _In_ ULONG rows) { - assert(array || !rows); + if (!array && rows) + throw std::invalid_argument("array is NULL"); LPSAFEARRAY sa; if constexpr (columns == 1) {