Skip to content

Commit

Permalink
Merge pull request #825 from david-puglielli/string-initialisation-nulls
Browse files Browse the repository at this point in the history
String initialisation to nulls
  • Loading branch information
david-puglielli authored Aug 1, 2018
2 parents c533ffa + 41a7caf commit 026c5d1
Show file tree
Hide file tree
Showing 17 changed files with 207 additions and 207 deletions.
40 changes: 20 additions & 20 deletions source/pdo_sqlsrv/pdo_dbh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@ struct pdo_dbh_methods pdo_sqlsrv_dbh_methods = {
pdo_sqlsrv_dbh* driver_dbh = reinterpret_cast<pdo_sqlsrv_dbh*>( dbh->driver_data ); \
driver_dbh->set_func( __FUNCTION__ ); \
int length = strlen( __FUNCTION__ ) + strlen( ": entering" ); \
char func[length+1]; \
char func[length+1] = {'\0'}; \
strcpy_s( func, sizeof( __FUNCTION__ ), __FUNCTION__ ); \
strcat_s( func, length+1, ": entering" ); \
LOG( SEV_NOTICE, func ); \
Expand Down Expand Up @@ -1270,7 +1270,7 @@ char * pdo_sqlsrv_dbh_last_id( _Inout_ pdo_dbh_t *dbh, _In_z_ const char *name,

try {

char last_insert_id_query[ LAST_INSERT_ID_QUERY_MAX_LEN ] = {'\0'};
char last_insert_id_query[LAST_INSERT_ID_QUERY_MAX_LEN] = {'\0'};
if( name == NULL ) {
strcpy_s( last_insert_id_query, sizeof( last_insert_id_query ), LAST_INSERT_ID_QUERY );
}
Expand Down Expand Up @@ -1412,13 +1412,13 @@ int pdo_sqlsrv_dbh_quote( _Inout_ pdo_dbh_t* dbh, _In_reads_(unquoted_len) const
if ( encoding == SQLSRV_ENCODING_BINARY ) {
// convert from char* to hex digits using os
std::basic_ostringstream<char> os;
for ( size_t index = 0; index < unquoted_len && unquoted[ index ] != '\0'; ++index ) {
for ( size_t index = 0; index < unquoted_len && unquoted[index] != '\0'; ++index ) {
// if unquoted is < 0 or > 255, that means this is a non-ascii character. Translation from non-ascii to binary is not supported.
// return an empty terminated string for now
if (( int )unquoted[ index ] < 0 || ( int )unquoted[ index ] > 255) {
if (( int )unquoted[index] < 0 || ( int )unquoted[index] > 255) {
*quoted_len = 0;
*quoted = reinterpret_cast<char*>( sqlsrv_malloc( *quoted_len, sizeof( char ), 1 ));
( *quoted )[ 0 ] = '\0';
( *quoted )[0] = '\0';
return 1;
}
// when an int is < 16 and is appended to os, its hex representation which starts
Expand All @@ -1427,7 +1427,7 @@ int pdo_sqlsrv_dbh_quote( _Inout_ pdo_dbh_t* dbh, _In_reads_(unquoted_len) const
if (( int )unquoted[index] < 16 ) {
os << '0';
}
os << std::hex << ( int )unquoted[ index ];
os << std::hex << ( int )unquoted[index];
}
std::basic_string<char> str_hex = os.str();
// each character is represented by 2 digits of hex
Expand All @@ -1439,13 +1439,13 @@ int pdo_sqlsrv_dbh_quote( _Inout_ pdo_dbh_t* dbh, _In_reads_(unquoted_len) const
*quoted = reinterpret_cast<char*>( sqlsrv_malloc( *quoted_len, sizeof( char ), 1 ));
unsigned int out_current = 0;
// insert '0x'
( *quoted )[ out_current++ ] = '0';
( *quoted )[ out_current++ ] = 'x';
for ( size_t index = 0; index < unquoted_str_len && unquoted_str[ index ] != '\0'; ++index ) {
( *quoted )[ out_current++ ] = unquoted_str[ index ];
( *quoted )[out_current++] = '0';
( *quoted )[out_current++] = 'x';
for ( size_t index = 0; index < unquoted_str_len && unquoted_str[index] != '\0'; ++index ) {
( *quoted )[out_current++] = unquoted_str[index];
}
// null terminator
( *quoted )[ out_current ] = '\0';
( *quoted )[out_current] = '\0';
sqlsrv_free( unquoted_str );
return 1;
}
Expand All @@ -1457,7 +1457,7 @@ int pdo_sqlsrv_dbh_quote( _Inout_ pdo_dbh_t* dbh, _In_reads_(unquoted_len) const
quotes_needed = 3;
}
for ( size_t index = 0; index < unquoted_len; ++index ) {
if ( unquoted[ index ] == '\'' ) {
if ( unquoted[index] == '\'' ) {
++quotes_needed;
}
}
Expand All @@ -1468,24 +1468,24 @@ int pdo_sqlsrv_dbh_quote( _Inout_ pdo_dbh_t* dbh, _In_reads_(unquoted_len) const

// insert N if the encoding is UTF8
if ( encoding == SQLSRV_ENCODING_UTF8 ) {
( *quoted )[ out_current++ ] = 'N';
( *quoted )[out_current++] = 'N';
}
// insert initial quote
( *quoted )[ out_current++ ] = '\'';
( *quoted )[out_current++] = '\'';

for ( size_t index = 0; index < unquoted_len; ++index ) {
if ( unquoted[ index ] == '\'' ) {
( *quoted )[ out_current++ ] = '\'';
( *quoted )[ out_current++ ] = '\'';
if ( unquoted[index] == '\'' ) {
( *quoted )[out_current++] = '\'';
( *quoted )[out_current++] = '\'';
}
else {
( *quoted )[ out_current++ ] = unquoted[ index ];
( *quoted )[out_current++] = unquoted[index];
}
}

// trailing quote and null terminator
( *quoted )[ out_current++ ] = '\'';
( *quoted )[ out_current ] = '\0';
( *quoted )[out_current++] = '\'';
( *quoted )[out_current] = '\0';

return 1;
}
Expand Down
6 changes: 3 additions & 3 deletions source/pdo_sqlsrv/pdo_init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,10 @@ PHP_MINIT_FUNCTION(pdo_sqlsrv)
g_pdo_errors_ht = reinterpret_cast<HashTable*>( pemalloc( sizeof( HashTable ), 1 ));
::zend_hash_init( g_pdo_errors_ht, 50, NULL, pdo_error_dtor /*pDestructor*/, 1 );

for( int i = 0; PDO_ERRORS[ i ].error_code != -1; ++i ) {
for( int i = 0; PDO_ERRORS[i].error_code != -1; ++i ) {

void* zr = ::zend_hash_index_update_mem( g_pdo_errors_ht, PDO_ERRORS[ i ].error_code,
&( PDO_ERRORS[ i ].sqlsrv_error ), sizeof( PDO_ERRORS[ i ].sqlsrv_error ) );
void* zr = ::zend_hash_index_update_mem( g_pdo_errors_ht, PDO_ERRORS[i].error_code,
&( PDO_ERRORS[i].sqlsrv_error ), sizeof( PDO_ERRORS[i].sqlsrv_error ) );
if( zr == NULL ) {

LOG( SEV_ERROR, "Failed to insert data into PDO errors hashtable." );
Expand Down
48 changes: 24 additions & 24 deletions source/pdo_sqlsrv/pdo_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ bool string_parser::discard_white_spaces()
return false;
}

while( this->is_white_space( this->orig_str[ pos ] )) {
while( this->is_white_space( this->orig_str[pos] )) {

if( !next() )
return false;
Expand Down Expand Up @@ -148,13 +148,13 @@ void conn_string_parser::validate_key( _In_reads_(key_len) const char *key, _Ino
{
int new_len = discard_trailing_white_spaces( key, key_len );

for( int i=0; PDO_CONN_OPTS[ i ].conn_option_key != SQLSRV_CONN_OPTION_INVALID; ++i )
for( int i=0; PDO_CONN_OPTS[i].conn_option_key != SQLSRV_CONN_OPTION_INVALID; ++i )
{
// discard the null terminator.
if( new_len == ( PDO_CONN_OPTS[ i ].sqlsrv_len - 1 ) && !strncasecmp( key, PDO_CONN_OPTS[ i ].sqlsrv_name, new_len )) {
if( new_len == ( PDO_CONN_OPTS[i].sqlsrv_len - 1 ) && !strncasecmp( key, PDO_CONN_OPTS[i].sqlsrv_name, new_len )) {

this->current_key = PDO_CONN_OPTS[ i ].conn_option_key;
this->current_key_name = PDO_CONN_OPTS[ i ].sqlsrv_name;
this->current_key = PDO_CONN_OPTS[i].conn_option_key;
this->current_key_name = PDO_CONN_OPTS[i].sqlsrv_name;
return;
}
}
Expand All @@ -164,7 +164,7 @@ void conn_string_parser::validate_key( _In_reads_(key_len) const char *key, _Ino
key_name = static_cast<char*>( sqlsrv_malloc( new_len + 1 ));
memcpy_s( key_name, new_len + 1 ,key, new_len );

key_name[ new_len ] = '\0';
key_name[new_len] = '\0';

THROW_PDO_ERROR( this->ctx, PDO_SQLSRV_ERROR_INVALID_DSN_KEY, static_cast<char*>( key_name ) );
}
Expand Down Expand Up @@ -232,15 +232,15 @@ void conn_string_parser:: parse_conn_string( TSRMLS_D )
start_pos = this->pos;

// read the key name
while( this->orig_str[ pos ] != '=' ) {
while( this->orig_str[pos] != '=' ) {

if( !next() ) {

THROW_PDO_ERROR( this->ctx, PDO_SQLSRV_ERROR_DSN_STRING_ENDED_UNEXPECTEDLY ); //EOS
}
}

this->validate_key( &( this->orig_str[ start_pos ] ), ( pos - start_pos ) TSRMLS_CC );
this->validate_key( &( this->orig_str[start_pos] ), ( pos - start_pos ) TSRMLS_CC );

state = Value;

Expand All @@ -249,13 +249,13 @@ void conn_string_parser:: parse_conn_string( TSRMLS_D )

case Value:
{
SQLSRV_ASSERT(( this->orig_str[ pos ] == '=' ), "conn_string_parser:: parse_conn_string: "
SQLSRV_ASSERT(( this->orig_str[pos] == '=' ), "conn_string_parser:: parse_conn_string: "
"Equal was expected" );

next(); // skip "="

// if EOS encountered after 0 or more spaces OR semi-colon encountered.
if( !discard_white_spaces() || this->orig_str[ pos ] == ';' ) {
if( !discard_white_spaces() || this->orig_str[pos] == ';' ) {

add_key_value_pair( NULL, 0 TSRMLS_CC );

Expand All @@ -265,13 +265,13 @@ void conn_string_parser:: parse_conn_string( TSRMLS_D )
}
else {

// this->orig_str[ pos ] == ';'
// this->orig_str[pos] == ';'
state = NextKeyValuePair;
}
}

// if LCB
else if( this->orig_str[ pos ] == '{' ) {
else if( this->orig_str[pos] == '{' ) {

start_pos = this->pos; // starting character is LCB
state = ValueContent1;
Expand All @@ -289,7 +289,7 @@ void conn_string_parser:: parse_conn_string( TSRMLS_D )

case ValueContent1:
{
while ( this->orig_str[ pos ] != '}' ) {
while ( this->orig_str[pos] != '}' ) {

if ( ! next() ) {

Expand All @@ -305,21 +305,21 @@ void conn_string_parser:: parse_conn_string( TSRMLS_D )

case ValueContent2:
{
while( this->orig_str[ pos ] != ';' ) {
while( this->orig_str[pos] != ';' ) {

if( ! next() ) {

break; //EOS
}
}

if( !this->is_eos() && this->orig_str[ pos ] == ';' ) {
if( !this->is_eos() && this->orig_str[pos] == ';' ) {

// semi-colon encountered, so go to next key-value pair
state = NextKeyValuePair;
}

add_key_value_pair( &( this->orig_str[ start_pos ] ), this->pos - start_pos TSRMLS_CC );
add_key_value_pair( &( this->orig_str[start_pos] ), this->pos - start_pos TSRMLS_CC );

SQLSRV_ASSERT((( state == NextKeyValuePair ) || ( this->is_eos() )),
"conn_string_parser::parse_conn_string: Invalid state encountered " );
Expand All @@ -334,14 +334,14 @@ void conn_string_parser:: parse_conn_string( TSRMLS_D )
if( !next() ) {

// EOS
add_key_value_pair( &( this->orig_str[ start_pos ] ), this->pos - start_pos TSRMLS_CC );
add_key_value_pair( &( this->orig_str[start_pos] ), this->pos - start_pos TSRMLS_CC );
break;
}

SQLSRV_ASSERT( !this->is_eos(), "conn_string_parser::parse_conn_string: Unexpected EOS encountered" );

// if second RCB encountered than go back to ValueContent1
if( this->orig_str[ pos ] == '}' ) {
if( this->orig_str[pos] == '}' ) {

if( !next() ) {

Expand All @@ -356,20 +356,20 @@ void conn_string_parser:: parse_conn_string( TSRMLS_D )
int end_pos = this->pos;

// discard any trailing white-spaces.
if( this->is_white_space( this->orig_str[ pos ] )) {
if( this->is_white_space( this->orig_str[pos] )) {

if( ! this->discard_white_spaces() ) {

//EOS
add_key_value_pair( &( this->orig_str[ start_pos ] ), end_pos - start_pos TSRMLS_CC );
add_key_value_pair( &( this->orig_str[start_pos] ), end_pos - start_pos TSRMLS_CC );
break;
}
}

// if semi-colon than go to next key-value pair
if ( this->orig_str[ pos ] == ';' ) {
if ( this->orig_str[pos] == ';' ) {

add_key_value_pair( &( this->orig_str[ start_pos ] ), end_pos - start_pos TSRMLS_CC );
add_key_value_pair( &( this->orig_str[start_pos] ), end_pos - start_pos TSRMLS_CC );
state = NextKeyValuePair;
break;
}
Expand All @@ -380,7 +380,7 @@ void conn_string_parser:: parse_conn_string( TSRMLS_D )
}
case NextKeyValuePair:
{
SQLSRV_ASSERT(( this->orig_str[ pos ] == ';' ),
SQLSRV_ASSERT(( this->orig_str[pos] == ';' ),
"conn_string_parser::parse_conn_string: semi-colon was expected." );

// Call next() to skip the semi-colon.
Expand All @@ -390,7 +390,7 @@ void conn_string_parser:: parse_conn_string( TSRMLS_D )
break;
}

if( this->orig_str[ pos ] == ';' ) {
if( this->orig_str[pos] == ';' ) {

// a second semi-colon is error case.
THROW_PDO_ERROR( this->ctx, PDO_SQLSRV_ERROR_EXTRA_SEMI_COLON_IN_DSN_STRING, this->pos );
Expand Down
20 changes: 10 additions & 10 deletions source/pdo_sqlsrv/pdo_stmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ void stmt_option_fetch_numeric:: operator()( _Inout_ sqlsrv_stmt* stmt, stmt_opt
pdo_sqlsrv_stmt* driver_stmt = reinterpret_cast<pdo_sqlsrv_stmt*>( stmt->driver_data ); \
driver_stmt->set_func( __FUNCTION__ ); \
int length = strlen( __FUNCTION__ ) + strlen( ": entering" ); \
char func[length+1]; \
char func[length+1] = {'\0'}; \
strcpy_s( func, sizeof( __FUNCTION__ ), __FUNCTION__ ); \
strcat_s( func, length+1, ": entering" ); \
LOG( SEV_NOTICE, func ); \
Expand Down Expand Up @@ -651,13 +651,13 @@ int pdo_sqlsrv_stmt_fetch( _Inout_ pdo_stmt_t *stmt, _In_ enum pdo_fetch_orienta
if (NULL== (bind_data = reinterpret_cast<pdo_bound_param_data*>(zend_hash_index_find_ptr(stmt->bound_columns, i))) &&
(NULL == (bind_data = reinterpret_cast<pdo_bound_param_data*>(zend_hash_find_ptr(stmt->bound_columns, stmt->columns[i].name))))) {

driver_stmt->bound_column_param_types[ i ] = PDO_PARAM_ZVAL;
driver_stmt->bound_column_param_types[i] = PDO_PARAM_ZVAL;
continue;
}

if( bind_data->param_type != PDO_PARAM_ZVAL ) {

driver_stmt->bound_column_param_types[ i ] = bind_data->param_type;
driver_stmt->bound_column_param_types[i] = bind_data->param_type;
bind_data->param_type = PDO_PARAM_ZVAL;
}
}
Expand Down Expand Up @@ -744,10 +744,10 @@ int pdo_sqlsrv_stmt_get_col_data( _Inout_ pdo_stmt_t *stmt, _In_ int colno,

// if a column is bound to a type different than the column type, figure out a way to convert it to the
// type they want
if( stmt->bound_columns && driver_stmt->bound_column_param_types[ colno ] != PDO_PARAM_ZVAL ) {
if( stmt->bound_columns && driver_stmt->bound_column_param_types[colno] != PDO_PARAM_ZVAL ) {

sqlsrv_php_type.typeinfo.type = pdo_type_to_sqlsrv_php_type( driver_stmt,
driver_stmt->bound_column_param_types[ colno ]
driver_stmt->bound_column_param_types[colno]
TSRMLS_CC );

pdo_bound_param_data* bind_data = NULL;
Expand All @@ -764,8 +764,8 @@ int pdo_sqlsrv_stmt_get_col_data( _Inout_ pdo_stmt_t *stmt, _In_ int colno,
throw pdo::PDOException();
}

CHECK_CUSTOM_ERROR( driver_stmt->bound_column_param_types[ colno ] != PDO_PARAM_STR
&& driver_stmt->bound_column_param_types[ colno ] != PDO_PARAM_LOB, driver_stmt,
CHECK_CUSTOM_ERROR( driver_stmt->bound_column_param_types[colno] != PDO_PARAM_STR
&& driver_stmt->bound_column_param_types[colno] != PDO_PARAM_LOB, driver_stmt,
PDO_SQLSRV_ERROR_COLUMN_TYPE_DOES_NOT_SUPPORT_ENCODING, colno + 1 ) {

throw pdo::PDOException();
Expand Down Expand Up @@ -991,7 +991,7 @@ int pdo_sqlsrv_stmt_get_col_meta( _Inout_ pdo_stmt_t *stmt, _In_ zend_long colno
add_assoc_long( return_value, "flags", 0 );

// get the name of the data type
char field_type_name[ SQL_SERVER_IDENT_SIZE_MAX ];
char field_type_name[SQL_SERVER_IDENT_SIZE_MAX] = {'\0'};
SQLSMALLINT out_buff_len;
SQLLEN not_used;
core::SQLColAttribute( driver_stmt, (SQLUSMALLINT) colno + 1, SQL_DESC_TYPE_NAME, field_type_name,
Expand All @@ -1017,13 +1017,13 @@ int pdo_sqlsrv_stmt_get_col_meta( _Inout_ pdo_stmt_t *stmt, _In_ zend_long colno
}

// add the table name of the field. All the tests so far show this to always be "", but we adhere to the PDO spec
char table_name[ SQL_SERVER_IDENT_SIZE_MAX ];
char table_name[SQL_SERVER_IDENT_SIZE_MAX] = {'\0'};
SQLLEN field_type_num;
core::SQLColAttribute( driver_stmt, (SQLUSMALLINT) colno + 1, SQL_DESC_TABLE_NAME, table_name, SQL_SERVER_IDENT_SIZE_MAX,
&out_buff_len, &field_type_num TSRMLS_CC );
add_assoc_string( return_value, "table", table_name );

if( stmt->columns && stmt->columns[ colno ].param_type == PDO_PARAM_ZVAL ) {
if( stmt->columns && stmt->columns[colno].param_type == PDO_PARAM_ZVAL ) {
add_assoc_long( return_value, "pdo_type", pdo_type );
}

Expand Down
2 changes: 1 addition & 1 deletion source/pdo_sqlsrv/pdo_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const int WARNING_MIN_LENGTH = static_cast<const int>( strlen( WARNING_TEMPLATE

// buffer used to hold a formatted log message prior to actually logging it.
const int LOG_MSG_SIZE = 2048;
char log_msg[ LOG_MSG_SIZE ];
char log_msg[LOG_MSG_SIZE] = {'\0'};

// internal error that says that FormatMessage failed
SQLCHAR INTERNAL_FORMAT_ERROR[] = "An internal error occurred. FormatMessage failed writing an error message.";
Expand Down
Loading

0 comments on commit 026c5d1

Please sign in to comment.