Skip to content

chore: change memset to memset_s/SecureZeroMemory #105

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
6 changes: 3 additions & 3 deletions bind.c
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,7 @@ extend_parameter_bindings(APDFields *self, int num_params)
self->allocated = 0;
return;
}
memset(&new_bindings[self->allocated], 0, sizeof(ParameterInfoClass) * (num_params - self->allocated));
pg_memset(&new_bindings[self->allocated], 0, sizeof(ParameterInfoClass) * (num_params - self->allocated));

self->parameters = new_bindings;
self->allocated = num_params;
Expand Down Expand Up @@ -555,7 +555,7 @@ extend_iparameter_bindings(IPDFields *self, int num_params)
self->allocated = 0;
return;
}
memset(&new_bindings[self->allocated], 0,
pg_memset(&new_bindings[self->allocated], 0,
sizeof(ParameterImplClass) * (num_params - self->allocated));

self->parameters = new_bindings;
Expand Down Expand Up @@ -981,7 +981,7 @@ extend_putdata_info(PutDataInfo *self, int num_params, BOOL shrink)
self->allocated = 0;
return;
}
memset(&new_pdata[self->allocated], 0,
pg_memset(&new_pdata[self->allocated], 0,
sizeof(PutDataClass) * (num_params - self->allocated));

self->pdata = new_pdata;
Expand Down
8 changes: 4 additions & 4 deletions connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -305,21 +305,21 @@ CC_initialize(ConnectionClass *rv, BOOL lockinit)
clear_size = sizeof(ConnectionClass);
#endif /* WIN_MULTITHREAD_SUPPORT */

memset(rv, 0, clear_size);
pg_memset(rv, 0, clear_size);
rv->status = CONN_NOT_CONNECTED;
rv->transact_status = CONN_IN_AUTOCOMMIT; /* autocommit by default */
rv->unnamed_prepared_stmt = NULL;

rv->stmts = (StatementClass **) malloc(sizeof(StatementClass *) * STMT_INCREMENT);
if (!rv->stmts)
goto cleanup;
memset(rv->stmts, 0, sizeof(StatementClass *) * STMT_INCREMENT);
pg_memset(rv->stmts, 0, sizeof(StatementClass *) * STMT_INCREMENT);

rv->num_stmts = STMT_INCREMENT;
rv->descs = (DescriptorClass **) malloc(sizeof(DescriptorClass *) * STMT_INCREMENT);
if (!rv->descs)
goto cleanup;
memset(rv->descs, 0, sizeof(DescriptorClass *) * STMT_INCREMENT);
pg_memset(rv->descs, 0, sizeof(DescriptorClass *) * STMT_INCREMENT);

rv->num_descs = STMT_INCREMENT;

Expand Down Expand Up @@ -1250,7 +1250,7 @@ CC_add_statement(ConnectionClass *self, StatementClass *stmt)
else
{
self->stmts = newstmts;
memset(&self->stmts[self->num_stmts], 0, sizeof(StatementClass *) * STMT_INCREMENT);
pg_memset(&self->stmts[self->num_stmts], 0, sizeof(StatementClass *) * STMT_INCREMENT);

stmt->hdbc = self;
self->stmts[self->num_stmts] = stmt;
Expand Down
2 changes: 1 addition & 1 deletion connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ enum {
coli->refcnt = 0; \
coli->acc_time = 0; \
}
#define col_info_initialize(coli) (memset(coli, 0, sizeof(COL_INFO)))
#define col_info_initialize(coli) (pg_memset(coli, 0, sizeof(COL_INFO)))

/* Translation DLL entry points */
#ifdef WIN32
Expand Down
16 changes: 8 additions & 8 deletions convert.c
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,7 @@ interval2istruct(SQLSMALLINT ctype, int precision, const char *str, SQL_INTERVAL
BOOL sign;
SQLINTERVAL itype = interval2itype(ctype);

memset(st, 0, sizeof(SQL_INTERVAL_STRUCT));
pg_memset(st, 0, sizeof(SQL_INTERVAL_STRUCT));
if ((scnt = sscanf(str, "%d-%d", &years, &mons)) >=2)
{
if (SQL_IS_YEAR_TO_MONTH == itype)
Expand Down Expand Up @@ -1324,7 +1324,7 @@ copy_and_convert_field(StatementClass *stmt,
*pIndicatorBindRow = 0;
}

memset(&std_time, 0, sizeof(SIMPLE_TIME));
pg_memset(&std_time, 0, sizeof(SIMPLE_TIME));

MYLOG(0, "field_type = %d, fctype = %d, value = '%s', cbValueMax=" FORMAT_LEN "\n", field_type, fCType, (value == NULL) ? "<NULL>" : value, cbValueMax);

Expand Down Expand Up @@ -1359,7 +1359,7 @@ MYLOG(0, "null_cvt_date_string=%d\n", conn->connInfo.cvt_null_date_string);
case SQL_C_DEFAULT:
if (rgbValueBindRow && cbValueMax >= sizeof(DATE_STRUCT))
{
memset(rgbValueBindRow, 0, cbValueMax);
pg_memset(rgbValueBindRow, 0, cbValueMax);
if (pcbValueBindRow)
*pcbValueBindRow = sizeof(DATE_STRUCT);
}
Expand All @@ -1369,7 +1369,7 @@ MYLOG(0, "null_cvt_date_string=%d\n", conn->connInfo.cvt_null_date_string);
#ifdef UNICODE_SUPPORT
case SQL_C_WCHAR:
if (rgbValueBindRow && cbValueMax >= WCLEN)
memset(rgbValueBindRow, 0, WCLEN);
pg_memset(rgbValueBindRow, 0, WCLEN);
else
result = COPY_RESULT_TRUNCATED;
break;
Expand Down Expand Up @@ -3721,7 +3721,7 @@ inner_process_tokens(QueryParse *qp, QueryBuild *qb)
{
TABLE_INFO ti, *pti = &ti;

memset(&ti, 0, sizeof(ti));
pg_memset(&ti, 0, sizeof(ti));
NAME_TO_NAME(ti.schema_name, conn->schemaIns);
NAME_TO_NAME(ti.table_name, conn->tableIns);
getCOLIfromTI(func, conn, qb->stmt, 0, &pti);
Expand Down Expand Up @@ -4063,7 +4063,7 @@ build_libpq_bind_params(StatementClass *stmt,
*paramValues = malloc(sizeof(char *) * num_params);
if (*paramValues == NULL)
goto cleanup;
memset(*paramValues, 0, sizeof(char *) * num_params);
pg_memset(*paramValues, 0, sizeof(char *) * num_params);
*paramLengths = malloc(sizeof(int) * num_params);
if (*paramLengths == NULL)
goto cleanup;
Expand Down Expand Up @@ -4332,7 +4332,7 @@ parse_to_numeric_struct(const char *wv, SQL_NUMERIC_STRUCT *ns, BOOL *overflow)
ns->precision = nlen;

/* Convert the decimal digits to binary */
memset(ns->val, 0, sizeof(ns->val));
pg_memset(ns->val, 0, sizeof(ns->val));
for (dig = 0; dig < nlen; dig++)
{
UInt4 carry;
Expand Down Expand Up @@ -4721,7 +4721,7 @@ MYLOG(DETAIL_LOG_LEVEL, "ipara=%p paramName=%s paramType=%d %d proc_return=%d\n"
send_buf = NULL;
param_string[0] = '\0';
cbuf[0] = '\0';
memset(&st, 0, sizeof(st));
pg_memset(&st, 0, sizeof(st));

ivstruct = (SQL_INTERVAL_STRUCT *) buffer;
/* Convert input C type to a neutral format */
Expand Down
26 changes: 13 additions & 13 deletions descriptor.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

void TI_Constructor(TABLE_INFO *self, const ConnectionClass *conn)
{
memset(self, 0, sizeof(TABLE_INFO));
pg_memset(self, 0, sizeof(TABLE_INFO));
TI_set_updatable(self);
}
void TI_Destructor(TABLE_INFO **ti, int count)
Expand Down Expand Up @@ -83,7 +83,7 @@ void FI_Constructor(FIELD_INFO *self, BOOL reuse)
MYLOG(DETAIL_LOG_LEVEL, "entering reuse=%d\n", reuse);
if (reuse)
FI_Destructor(&self, 1, FALSE);
memset(self, 0, sizeof(FIELD_INFO));
pg_memset(self, 0, sizeof(FIELD_INFO));
self->nullable = TRUE;
self->columnkey = -1;
self->typmod = -1;
Expand Down Expand Up @@ -124,7 +124,7 @@ void TI_Create_IH(TABLE_INFO *ti)
InheritanceClass *ih;

ih = (InheritanceClass *) malloc(sizeof(InheritanceClass) + (alloc - 1) * sizeof(ih->inf[0]));
memset(ih, 0, sizeof(InheritanceClass));
pg_memset(ih, 0, sizeof(InheritanceClass));
ih->allocated = alloc;
ti->ih = ih;
}
Expand Down Expand Up @@ -201,7 +201,7 @@ const pgNAME TI_Ins_IH(TABLE_INFO *ti, OID tableoid, const char *fullName)

void DC_Constructor(DescriptorClass *self, BOOL embedded, StatementClass *stmt)
{
memset(self, 0, sizeof(DescriptorClass));
pg_memset(self, 0, sizeof(DescriptorClass));
self->deschd.embedded = embedded;
}

Expand Down Expand Up @@ -294,22 +294,22 @@ void InitializeEmbeddedDescriptor(DescriptorClass *self, StatementClass *stmt,
switch (desc_type)
{
case SQL_ATTR_APP_ROW_DESC:
memset(&(self->ardf), 0, sizeof(ARDFields));
pg_memset(&(self->ardf), 0, sizeof(ARDFields));
InitializeARDFields(&(self->ardf));
stmt->ard = self;
break;
case SQL_ATTR_APP_PARAM_DESC:
memset(&(self->apdf), 0, sizeof(APDFields));
pg_memset(&(self->apdf), 0, sizeof(APDFields));
InitializeAPDFields(&(self->apdf));
stmt->apd = self;
break;
case SQL_ATTR_IMP_ROW_DESC:
memset(&(self->irdf), 0, sizeof(IRDFields));
pg_memset(&(self->irdf), 0, sizeof(IRDFields));
stmt->ird = self;
stmt->ird->irdf.stmt = stmt;
break;
case SQL_ATTR_IMP_PARAM_DESC:
memset(&(self->ipdf), 0, sizeof(IPDFields));
pg_memset(&(self->ipdf), 0, sizeof(IPDFields));
stmt->ipd = self;
break;
}
Expand All @@ -321,7 +321,7 @@ void InitializeEmbeddedDescriptor(DescriptorClass *self, StatementClass *stmt,
void
InitializeARDFields(ARDFields *opt)
{
memset(opt, 0, sizeof(ARDFields));
pg_memset(opt, 0, sizeof(ARDFields));
opt->size_of_rowset = 1;
opt->bind_size = 0; /* default is to bind by column */
opt->size_of_rowset_odbc2 = 1;
Expand All @@ -332,7 +332,7 @@ InitializeARDFields(ARDFields *opt)
void
InitializeAPDFields(APDFields *opt)
{
memset(opt, 0, sizeof(APDFields));
pg_memset(opt, 0, sizeof(APDFields));
opt->paramset_size = 1;
opt->param_bind_type = 0; /* default is to bind by column */
opt->paramset_size_dummy = 1; /* dummy setting */
Expand All @@ -343,7 +343,7 @@ BindInfoClass *ARD_AllocBookmark(ARDFields *ardopts)
if (!ardopts->bookmark)
{
ardopts->bookmark = (BindInfoClass *) malloc(sizeof(BindInfoClass));
memset(ardopts->bookmark, 0, sizeof(BindInfoClass));
pg_memset(ardopts->bookmark, 0, sizeof(BindInfoClass));
}
return ardopts->bookmark;
}
Expand Down Expand Up @@ -373,7 +373,7 @@ char CC_add_descriptor(ConnectionClass *self, DescriptorClass *desc)
return FALSE;
self->descs = descs;

memset(&self->descs[self->num_descs], 0, sizeof(DescriptorClass *) *
pg_memset(&self->descs[self->num_descs], 0, sizeof(DescriptorClass *) *
DESC_INCREMENT);
DC_get_conn(desc) = self;
self->descs[self->num_descs] = desc;
Expand All @@ -399,7 +399,7 @@ PGAPI_AllocDesc(HDBC ConnectionHandle,
desc = (DescriptorClass *) malloc(sizeof(DescriptorClass));
if (desc)
{
memset(desc, 0, sizeof(DescriptorClass));
pg_memset(desc, 0, sizeof(DescriptorClass));
DC_get_conn(desc) = conn;
if (CC_add_descriptor(conn, desc))
*DescriptorHandle = desc;
Expand Down
8 changes: 4 additions & 4 deletions dlg_specific.c
Original file line number Diff line number Diff line change
Expand Up @@ -1783,7 +1783,7 @@ CC_conninfo_init(ConnInfo *conninfo, UInt4 option)

if (0 != (CLEANUP_FOR_REUSE & option))
CC_conninfo_release(conninfo);
memset(conninfo, 0, sizeof(ConnInfo));
pg_memset(conninfo, 0, sizeof(ConnInfo));

conninfo->allow_keyset = -1;
conninfo->lf_conversion = -1;
Expand Down Expand Up @@ -1818,7 +1818,7 @@ CC_conninfo_init(ConnInfo *conninfo, UInt4 option)

void init_globals(GLOBAL_VALUES *glbv)
{
memset(glbv, 0, sizeof(*glbv));
pg_memset(glbv, 0, sizeof(*glbv));
glbv->debug = -1;
glbv->commlog = -1;
}
Expand All @@ -1828,7 +1828,7 @@ void init_globals(GLOBAL_VALUES *glbv)

void copy_globals(GLOBAL_VALUES *to, const GLOBAL_VALUES *from)
{
memset(to, 0, sizeof(*to));
pg_memset(to, 0, sizeof(*to));
/***
memcpy(to, from, sizeof(GLOBAL_VALUES));
SET_NAME_DIRECTLY(to->drivername, NULL);
Expand Down Expand Up @@ -1866,7 +1866,7 @@ void finalize_globals(GLOBAL_VALUES *glbv)
void
CC_copy_conninfo(ConnInfo *ci, const ConnInfo *sci)
{
memset(ci, 0,sizeof(ConnInfo));
pg_memset(ci, 0,sizeof(ConnInfo));

CORR_STRCPY(dsn);
CORR_STRCPY(desc);
Expand Down
2 changes: 1 addition & 1 deletion drvconn.c
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ PGAPI_DriverConnect(HDBC hdbc,

/* initialize pg_version */
CC_initialize_pg_version(conn);
memset(salt, 0, sizeof(salt));
pg_memset(salt, 0, sizeof(salt));

#ifdef WIN32
dialog:
Expand Down
2 changes: 1 addition & 1 deletion environ.c
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ ER_Constructor(SDWORD errnumber, const char *msg)
error = (PG_ErrorInfo *) malloc(sizeof(PG_ErrorInfo) + aladd);
if (error)
{
memset(error, 0, sizeof(PG_ErrorInfo));
pg_memset(error, 0, sizeof(PG_ErrorInfo));
error->status = errnumber;
error->errsize = errsize;
if (errsize > 0)
Expand Down
4 changes: 2 additions & 2 deletions info.c
Original file line number Diff line number Diff line change
Expand Up @@ -1341,7 +1341,7 @@ PGAPI_GetFunctions(HDBC hdbc,

if (fFunction == SQL_API_ALL_FUNCTIONS)
{
memset(pfExists, 0, sizeof(pfExists[0]) * 100);
pg_memset(pfExists, 0, sizeof(pfExists[0]) * 100);

/* ODBC core functions */
pfExists[SQL_API_SQLALLOCCONNECT] = TRUE;
Expand Down Expand Up @@ -5805,7 +5805,7 @@ PGAPI_TablePrivileges(HSTMT hstmt,
}
for (i = 0; i < tablecount; i++)
{
memset(useracl, 0, usercount * sizeof(char[ACLMAX]));
pg_memset(useracl, 0, usercount * sizeof(char[ACLMAX]));
acl = (char *) QR_get_value_backend_text(wres, i, 2);
if (acl && acl[0] == '{')
user = acl + 1;
Expand Down
2 changes: 1 addition & 1 deletion inouealc.c
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ void *pgdebug_memset(void *out, int c, size_t len)
return NULL;
}
out_check(out, len, __FUNCTION__);
return memset(out, c, len);
return memset_s(out, len, c, len);
}

void debug_memory_check(void)
Expand Down
2 changes: 1 addition & 1 deletion odbcapi30.c
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,7 @@ MYLOG(DETAIL_LOG_LEVEL, "lie=%d\n", ci->drivers.lie);
CC_clear_error(conn);
if (fFunction != SQL_API_ODBC3_ALL_FUNCTIONS)
return SQL_ERROR;
memset(pfExists, 0, sizeof(UWORD) * SQL_API_ODBC3_ALL_FUNCTIONS_SIZE);
pg_memset(pfExists, 0, sizeof(UWORD) * SQL_API_ODBC3_ALL_FUNCTIONS_SIZE);

/* SQL_FUNC_ESET(pfExists, SQL_API_SQLALLOCCONNECT); 1 deprecated */
/* SQL_FUNC_ESET(pfExists, SQL_API_SQLALLOCENV); 2 deprecated */
Expand Down
2 changes: 1 addition & 1 deletion parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,7 @@ static BOOL allocateFields(IRDFields *irdflds, size_t sizeRequested)
irdflds->allocated = irdflds->nfields = 0;
return FALSE;
}
memset(&fi[irdflds->allocated], 0, incr_size);
pg_memset(&fi[irdflds->allocated], 0, incr_size);
irdflds->fi = fi;
irdflds->allocated = (SQLSMALLINT) alloc_size;

Expand Down
33 changes: 19 additions & 14 deletions psqlodbc.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,25 +104,30 @@ void debug_memory_check(void);
#ifdef WIN32
#undef strdup
#endif /* WIN32 */
#define malloc pgdebug_alloc
#define realloc pgdebug_realloc
#define calloc pgdebug_calloc
#define strdup pgdebug_strdup
#define free pgdebug_free
#define strcpy pgdebug_strcpy
#define strncpy pgdebug_strncpy
#define malloc pgdebug_alloc
#define realloc pgdebug_realloc
#define calloc pgdebug_calloc
#define strdup pgdebug_strdup
#define free pgdebug_free
#define strcpy pgdebug_strcpy
#define strncpy pgdebug_strncpy
/* #define strncpy_null pgdebug_strncpy_null */
#define memcpy pgdebug_memcpy
#define memset pgdebug_memset
#define memcpy pgdebug_memcpy
#define pg_memset(dest, ch, count) pgdebug_memset(dest, ch, count)
#else /* _MEMORY_DEBUG_ */
#ifdef WIN32
#undef strdup
#endif /* WIN32 */
#define malloc pg_malloc
#define realloc pg_realloc
#define calloc pg_calloc
#define strdup pg_strdup
#define free pg_free
#define malloc pg_malloc
#define realloc pg_realloc
#define calloc pg_calloc
#define strdup pg_strdup
#define free pg_free
#ifdef WIN32
#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 /* _MEMORY_DEBUG_ */

#ifdef WIN32
Expand Down
Loading