Skip to content

Commit

Permalink
Add an invalid data type to avoid not initialized pointer to functions
Browse files Browse the repository at this point in the history
Always initialize TDSCOLUMN->funcs to avoid NULL pointer dereferences.

Signed-off-by: Frediano Ziglio <freddy77@gmail.com>
  • Loading branch information
freddy77 committed Apr 29, 2017
1 parent f0400f5 commit 6522b32
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 0 deletions.
9 changes: 9 additions & 0 deletions include/freetds/data.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@
TDS_EXTRA_CHECK(tds_ ## name ## _check) \
}

tds_func_get_info tds_invalid_get_info;
tds_func_row_len tds_invalid_row_len;
tds_func_get_data tds_invalid_get;
tds_func_put_info_len tds_invalid_put_info_len;
tds_func_put_info tds_invalid_put_info;
tds_func_put_data tds_invalid_put;
tds_func_check tds_invalid_check;

tds_func_get_info tds_generic_get_info;
tds_func_row_len tds_generic_row_len;
tds_func_get_data tds_generic_get;
Expand Down Expand Up @@ -101,6 +109,7 @@ tds_func_check tds_sybblob_check;
# define TDS_DEFINE_DEFAULT_FUNCS(name) \
const TDSCOLUMNFUNCS tds_ ## name ## _funcs = TDS_COMMON_FUNCS(name)

TDS_DEFINE_DEFAULT_FUNCS(invalid);
TDS_DEFINE_DEFAULT_FUNCS(generic);
TDS_DEFINE_DEFAULT_FUNCS(numeric);
TDS_DEFINE_DEFAULT_FUNCS(variant);
Expand Down
6 changes: 6 additions & 0 deletions src/odbc/odbc_data.c
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,11 @@ data_sybblob_set_type_info(TDSCOLUMN * col, struct _drecord *drec, SQLINTEGER od
}
}

static void
data_invalid_set_type_info(TDSCOLUMN * col, struct _drecord *drec, SQLINTEGER odbc_ver)
{
}

void
odbc_set_sql_type_info(TDSCOLUMN * col, struct _drecord *drec, SQLINTEGER odbc_ver)
{
Expand All @@ -443,6 +448,7 @@ odbc_set_sql_type_info(TDSCOLUMN * col, struct _drecord *drec, SQLINTEGER odbc_v
TDS_COMMON_FUNCS(name), \
data_ ## name ## _set_type_info, \
}
TDS_DEFINE_FUNCS(invalid);
TDS_DEFINE_FUNCS(generic);
TDS_DEFINE_FUNCS(numeric);
TDS_DEFINE_FUNCS(variant);
Expand Down
43 changes: 43 additions & 0 deletions src/tds/data.c
Original file line number Diff line number Diff line change
Expand Up @@ -1674,6 +1674,42 @@ tds_sybblob_put(TDSSOCKET *tds, TDSCOLUMN *col, int bcp7)
return TDS_SUCCESS;
}

TDSRET
tds_invalid_get_info(TDSSOCKET * tds, TDSCOLUMN * col)
{
return TDS_FAIL;
}

TDS_INT
tds_invalid_row_len(TDSCOLUMN *col)
{
return 0;
}

TDSRET
tds_invalid_get(TDSSOCKET * tds, TDSCOLUMN * col)
{
return TDS_FAIL;
}

TDSRET
tds_invalid_put_info(TDSSOCKET * tds, TDSCOLUMN * col)
{
return TDS_FAIL;
}

unsigned
tds_invalid_put_info_len(TDSSOCKET * tds, TDSCOLUMN * col)
{
return 0;
}

TDSRET
tds_invalid_put(TDSSOCKET *tds, TDSCOLUMN *col, int bcp7)
{
return TDS_FAIL;
}

#if ENABLE_EXTRA_CHECKS
int
tds_generic_check(const TDSCOLUMN *col)
Expand Down Expand Up @@ -1752,6 +1788,12 @@ tds_sybblob_check(const TDSCOLUMN *col)
{
return 1;
}

int
tds_invalid_check(const TDSCOLUMN *col)
{
return 1;
}
#endif


Expand All @@ -1766,6 +1808,7 @@ TDS_DECLARE_FUNCS(msdatetime);
TDS_DECLARE_FUNCS(clrudt);
TDS_DECLARE_FUNCS(sybbigtime);
TDS_DECLARE_FUNCS(sybblob);
TDS_DECLARE_FUNCS(invalid);
#include <freetds/popvis.h>

static const TDSCOLUMNFUNCS *
Expand Down
3 changes: 3 additions & 0 deletions src/tds/mem.c
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ tds_get_dynid(TDSCONNECTION * conn, char *id)
return id;
}

extern const TDSCOLUMNFUNCS tds_invalid_funcs;

static TDSCOLUMN *
tds_alloc_column(void)
{
Expand All @@ -127,6 +129,7 @@ tds_alloc_column(void)
tds_dstr_init(&col->table_name);
tds_dstr_init(&col->column_name);
tds_dstr_init(&col->table_column_name);
col->funcs = &tds_invalid_funcs;

Cleanup:
return col;
Expand Down

0 comments on commit 6522b32

Please sign in to comment.