From 3865b5db022bd126a88ee397fcf53af66be31a4e Mon Sep 17 00:00:00 2001 From: "Favian (Ian) Samatha" Date: Fri, 11 Apr 2025 11:55:51 -0700 Subject: [PATCH 1/2] fix: revert to memset if memset_s is not available --- psqlodbc.h | 10 ++++++++++ test/src/common.h | 11 +++++++++++ 2 files changed, 21 insertions(+) diff --git a/psqlodbc.h b/psqlodbc.h index b42ecb5e..3b6e20f6 100644 --- a/psqlodbc.h +++ b/psqlodbc.h @@ -50,6 +50,12 @@ #include #endif /* WIN32 */ +/* For memset_s */ +#ifdef __STDC_LIB_EXT1__ +#define __STDC_WANT_LIB_EXT1__ 1 +#include +#endif /* __STDC_LIB_EXT1__ */ + #ifdef __INCLUDE_POSTGRES_FE_H__ /* currently not defined */ /* * Unfortunately #including postgres_fe.h causes various trobles. @@ -134,7 +140,11 @@ void debug_memory_check(void); #ifdef WIN32 #define pg_memset(dest, ch, count) SecureZeroMemory(dest, count) #else +#ifdef __STDC_LIB_EXT1__ #define pg_memset(dest, ch, count) memset_s(dest, count, ch, count) +#else +#define pg_memset(dest, ch, count) memset(dest, ch, count) +#endif /* __STDC_LIB_EXT1__ */ #endif /* WIN32 */ #endif /* _MEMORY_DEBUG_ */ diff --git a/test/src/common.h b/test/src/common.h index 7bd8e2c2..d94097e9 100644 --- a/test/src/common.h +++ b/test/src/common.h @@ -19,10 +19,21 @@ #ifdef WIN32 #define snprintf _snprintf +#endif /* WIN32 */ + +/* use safe memset if available */ +#ifndef pg_memset // May already be defined by psqlodbc.h in some tests +#ifdef WIN32 #define pg_memset(dest, ch, count) SecureZeroMemory(dest, count) #else +#ifdef __STDC_LIB_EXT1__ +#define __STDC_WANT_LIB_EXT1__ 1 +#include #define pg_memset(dest, ch, count) memset_s(dest, count, ch, count) +#endif /* __STDC_LIB_EXT1__ */ +#define pg_memset(dest, ch, count) memset(dest, ch, count) // default to memset if functionality isn't available #endif /* WIN32 */ +#endif /* pg_memset */ extern SQLHENV env; extern SQLHDBC conn; From 7c6c23874f5141deed04d4f549b08490b4444d1e Mon Sep 17 00:00:00 2001 From: "Favian (Ian) Samatha" Date: Fri, 11 Apr 2025 17:02:09 -0700 Subject: [PATCH 2/2] remove memset from tests --- test/src/bulkoperations-test.c | 4 ++-- test/src/common.h | 16 +--------------- test/src/diagnostic-test.c | 2 +- test/src/numeric-test.c | 2 +- test/src/odbc-escapes-test.c | 16 ++++++++-------- test/src/result-conversions-test.c | 2 +- 6 files changed, 14 insertions(+), 28 deletions(-) diff --git a/test/src/bulkoperations-test.c b/test/src/bulkoperations-test.c index ececf8d1..37e035d4 100644 --- a/test/src/bulkoperations-test.c +++ b/test/src/bulkoperations-test.c @@ -52,8 +52,8 @@ int main(int argc, char **argv) SQLLEN indColvalues1[3]; SQLLEN indColvalues2[3]; - pg_memset(bookmark, 0x7F, sizeof(bookmark)); - pg_memset(saved_bookmarks, 0xF7, sizeof(saved_bookmarks)); + memset(bookmark, 0x7F, sizeof(bookmark)); + memset(saved_bookmarks, 0xF7, sizeof(saved_bookmarks)); test_connect_ext("UpdatableCursors=1;Fetch=1"); diff --git a/test/src/common.h b/test/src/common.h index d94097e9..4de0a41b 100644 --- a/test/src/common.h +++ b/test/src/common.h @@ -19,21 +19,7 @@ #ifdef WIN32 #define snprintf _snprintf -#endif /* WIN32 */ - -/* use safe memset if available */ -#ifndef pg_memset // May already be defined by psqlodbc.h in some tests -#ifdef WIN32 -#define pg_memset(dest, ch, count) SecureZeroMemory(dest, count) -#else -#ifdef __STDC_LIB_EXT1__ -#define __STDC_WANT_LIB_EXT1__ 1 -#include -#define pg_memset(dest, ch, count) memset_s(dest, count, ch, count) -#endif /* __STDC_LIB_EXT1__ */ -#define pg_memset(dest, ch, count) memset(dest, ch, count) // default to memset if functionality isn't available -#endif /* WIN32 */ -#endif /* pg_memset */ +#endif extern SQLHENV env; extern SQLHDBC conn; diff --git a/test/src/diagnostic-test.c b/test/src/diagnostic-test.c index 4c75726d..14374269 100644 --- a/test/src/diagnostic-test.c +++ b/test/src/diagnostic-test.c @@ -36,7 +36,7 @@ int main(int argc, char **argv) /* * Test a very long error message. */ - pg_memset(buf, 'x', sizeof(buf) - 10); + memset(buf, 'x', sizeof(buf) - 10); sprintf(buf + sizeof(buf) - 10, "END"); rc = SQLExecDirect(hstmt, (SQLCHAR *) buf, SQL_NTS); print_diag("SQLExecDirect", SQL_HANDLE_STMT, hstmt); diff --git a/test/src/numeric-test.c b/test/src/numeric-test.c index ccd05c45..c127592e 100644 --- a/test/src/numeric-test.c +++ b/test/src/numeric-test.c @@ -33,7 +33,7 @@ build_numeric_struct(SQL_NUMERIC_STRUCT *numericparam, int len; /* parse the hex-encoded value */ - pg_memset(numericparam, 0, sizeof(SQL_NUMERIC_STRUCT)); + memset(numericparam, 0, sizeof(SQL_NUMERIC_STRUCT)); numericparam->sign = sign; numericparam->precision = precision; diff --git a/test/src/odbc-escapes-test.c b/test/src/odbc-escapes-test.c index 2759ecb4..a5e6976e 100644 --- a/test/src/odbc-escapes-test.c +++ b/test/src/odbc-escapes-test.c @@ -142,14 +142,14 @@ static void escape_test(HSTMT hstmt) executeQuery(hstmt); prepareQuery(hstmt, "{ ? = call length('foo') }"); - pg_memset(outbuf1, 0, sizeof(outbuf1)); + memset(outbuf1, 0, sizeof(outbuf1)); bindOutParamString(hstmt, 1, NULL, outbuf1, sizeof(outbuf1) - 1, 0); executeQuery(hstmt); printf("OUT param: %s\n", outbuf1); /* It's preferable to cast VARIADIC any fields */ prepareQuery(hstmt, "{ ? = call concat(?::text, ?::text) }"); - pg_memset(outbuf1, 0, sizeof(outbuf1)); + memset(outbuf1, 0, sizeof(outbuf1)); bindOutParamString(hstmt, 1, NULL, outbuf1, sizeof(outbuf1) - 1, 0); bindParamString(hstmt, 2, NULL, "foo"); bindParamString(hstmt, 3, NULL, "bar"); @@ -172,22 +172,22 @@ static void escape_test(HSTMT hstmt) /**** call procedure with out and i-o parameters ****/ prepareQuery(hstmt, "{call a_b_c_d_e(?, ?, ?, ?, ?)}"); - pg_memset(outbuf1, 0, sizeof(outbuf1)); + memset(outbuf1, 0, sizeof(outbuf1)); bindOutParamString(hstmt, 1, NULL, outbuf1, sizeof(outbuf1) - 1, 0); bindParamString(hstmt, 2, NULL, "2017-02-23 11:34:46"); strcpy(outbuf3, "4"); bindOutParamString(hstmt, 3, NULL, outbuf3, sizeof(outbuf3) - 1, 1); bindParamString(hstmt, 4, NULL, "3.4"); - pg_memset(outbuf5, 0, sizeof(outbuf5)); + memset(outbuf5, 0, sizeof(outbuf5)); bindOutParamString(hstmt, 5, NULL, outbuf5, sizeof(outbuf5) - 1, 0); executeQuery(hstmt); printf("OUT params: %s : %s : %s\n", outbuf1, outbuf3, outbuf5); /**** call procedure parameters by name (e,a,b,c,d) ****/ prepareQuery(hstmt, "{call a_b_c_d_e(?, ?, ?, ?, ?)}"); - pg_memset(outbuf5, 0, sizeof(outbuf5)); + memset(outbuf5, 0, sizeof(outbuf5)); bindOutParamString(hstmt, 1, "e", outbuf5, sizeof(outbuf5) - 1, 0); - pg_memset(outbuf1, 0, sizeof(outbuf1)); + memset(outbuf1, 0, sizeof(outbuf1)); bindOutParamString(hstmt, 2, "a", outbuf1, sizeof(outbuf1) - 1, 0); bindParamString(hstmt, 3, "b", "2017-02-23 11:34:46"); strcpy(outbuf3, "4"); @@ -202,9 +202,9 @@ static void escape_test(HSTMT hstmt) strcpy(outbuf3, "4"); bindOutParamString(hstmt, 2, "c", outbuf3, sizeof(outbuf3) - 1, 1); bindParamString(hstmt, 3, "d", "3.4"); - pg_memset(outbuf5, 0, sizeof(outbuf5)); + memset(outbuf5, 0, sizeof(outbuf5)); bindOutParamString(hstmt, 4, "e", outbuf5, sizeof(outbuf5) - 1, 0); - pg_memset(outbuf1, 0, sizeof(outbuf1)); + memset(outbuf1, 0, sizeof(outbuf1)); bindOutParamString(hstmt, 5, "a", outbuf1, sizeof(outbuf1) - 1, 0); executeQuery(hstmt); printf("OUT params: %s : %s : %s\n", outbuf1, outbuf3, outbuf5); diff --git a/test/src/result-conversions-test.c b/test/src/result-conversions-test.c index bc2bef33..4af85a8c 100644 --- a/test/src/result-conversions-test.c +++ b/test/src/result-conversions-test.c @@ -476,7 +476,7 @@ test_conversion(const char *pgtype, const char *pgvalue, int sqltype, const char if (resultbuf == NULL) resultbuf = malloc(500); - pg_memset(resultbuf, 0xFF, 500); + memset(resultbuf, 0xFF, 500); fixed_len = get_sql_type_size(sqltype); if (fixed_len != -1)