Skip to content

fix: use memset if memset_s is not available #107

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions psqlodbc.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@
#include <stdbool.h>
#endif /* WIN32 */

/* For memset_s */
#ifdef __STDC_LIB_EXT1__
#define __STDC_WANT_LIB_EXT1__ 1
#include <string.h>
#endif /* __STDC_LIB_EXT1__ */

#ifdef __INCLUDE_POSTGRES_FE_H__ /* currently not defined */
/*
* Unfortunately #including postgres_fe.h causes various trobles.
Expand Down Expand Up @@ -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_ */

Expand Down
4 changes: 2 additions & 2 deletions test/src/bulkoperations-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down
5 changes: 1 addition & 4 deletions test/src/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@

#ifdef WIN32
#define snprintf _snprintf
#define pg_memset(dest, ch, count) SecureZeroMemory(dest, count)
#else
#define pg_memset(dest, ch, count) memset_s(dest, count, ch, count)
#endif /* WIN32 */
#endif

extern SQLHENV env;
extern SQLHDBC conn;
Expand Down
2 changes: 1 addition & 1 deletion test/src/diagnostic-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion test/src/numeric-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
16 changes: 8 additions & 8 deletions test/src/odbc-escapes-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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");
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion test/src/result-conversions-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down