Skip to content
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

Replaced problematic strlen if possible #1226

Merged
merged 2 commits into from
Dec 24, 2020
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
1 change: 1 addition & 0 deletions azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ jobs:
sudo update-alternatives --set php-cgi /usr/bin/php-cgi$(phpver)
sudo update-alternatives --set phar.phar /usr/bin/phar.phar$(phpver)
sudo update-alternatives --set php-config /usr/bin/php-config$(phpver)
sudo phpdismod xdebug
php -version
displayName: 'Use PHP version $(phpver)'

Expand Down
4 changes: 2 additions & 2 deletions source/pdo_sqlsrv/pdo_init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -279,15 +279,15 @@ namespace {
zend_class_entry* zend_class = php_pdo_get_dbh_ce();

SQLSRV_ASSERT( zend_class != NULL, "REGISTER_PDO_SQLSRV_CLASS_CONST_LONG: php_pdo_get_dbh_ce failed");
zend_declare_class_constant_long(zend_class, const_cast<char*>(name), strlen(name), value);
zend_declare_class_constant_long(zend_class, const_cast<char*>(name), strnlen_s(name), value);
}

void REGISTER_PDO_SQLSRV_CLASS_CONST_STRING( _In_z_ char const* name, _In_z_ char const* value )
{
zend_class_entry* zend_class = php_pdo_get_dbh_ce();

SQLSRV_ASSERT( zend_class != NULL, "REGISTER_PDO_SQLSRV_CLASS_CONST_STRING: php_pdo_get_dbh_ce failed");
zend_declare_class_constant_string(zend_class, const_cast<char*>(name), strlen(name), const_cast<char*>(value));
zend_declare_class_constant_string(zend_class, const_cast<char*>(name), strnlen_s(name), const_cast<char*>(value));
}

// array of pdo constants.
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 @@ char EXCEPTION_PROPERTY_ERRORINFO[] = "errorInfo";
const int MAX_DIGITS = 11; // +-2 billion = 10 digits + 1 for the sign if negative

// the warning message is not the error message alone; it must take WARNING_TEMPLATE above into consideration without the formats
const int WARNING_MIN_LENGTH = static_cast<const int>( strlen( WARNING_TEMPLATE ) - strlen( "%1!s!%2!d!%3!s!" ));
const int WARNING_MIN_LENGTH = static_cast<const int>( strnlen_s( WARNING_TEMPLATE ) - strnlen_s( "%1!s!%2!d!%3!s!" ));

// Returns a sqlsrv_error for a given error code.
sqlsrv_error_const* get_error_message( _In_opt_ unsigned int sqlsrv_error_code);
Expand Down
6 changes: 3 additions & 3 deletions source/shared/core_stmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2356,7 +2356,7 @@ void format_decimal_numbers(_In_ SQLSMALLINT decimals_places, _In_ SQLSMALLINT f

char buffer[50] = " "; // A buffer with two blank spaces, as leeway
int offset = 1 + is_negative;
int src_length = strlen(src);
int src_length = strnlen_s(src);

if (add_leading_zero) {
buffer[offset++] = '0';
Expand Down Expand Up @@ -3054,7 +3054,7 @@ void adjustDecimalPrecision(_Inout_ zval* param_z, _In_ SQLSMALLINT decimal_digi
return; // decimal point not found
}

int src_length = strlen(src);
int src_length = strnlen_s(src);
int num_decimals = src_length - (pt - src) - 1;
if (num_decimals <= decimal_digits) {
return; // no need to adjust number of decimals
Expand Down Expand Up @@ -3151,7 +3151,7 @@ void adjustDecimalPrecision(_Inout_ zval* param_z, _In_ SQLSMALLINT decimal_digi
buffer[0] = '-';
}

zend_string* zstr = zend_string_init(buffer, strlen(buffer), 0);
zend_string* zstr = zend_string_init(buffer, strnlen_s(buffer), 0);
zend_string_release(Z_STR_P(param_z));
ZVAL_NEW_STR(param_z, zstr);
}
Expand Down
19 changes: 15 additions & 4 deletions source/shared/localizationimpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -667,16 +667,22 @@ size_t SystemLocale::Utf8To16Strict( const char *src, SSIZE_T cchSrc, WCHAR *des

size_t SystemLocale::ToUtf16( UINT srcCodePage, const char * src, SSIZE_T cchSrc, WCHAR * dest, size_t cchDest, DWORD * pErrorCode )
{
if (cchSrc < 0) {
if (NULL != pErrorCode)
*pErrorCode = ERROR_INVALID_PARAMETER;
return 0;
}

srcCodePage = ExpandSpecialCP( srcCodePage );
if ( dest )
{
if ( srcCodePage == CP_UTF8 )
{
return SystemLocale::Utf8To16( src, cchSrc < 0 ? (1+strlen(src)) : cchSrc, dest, cchDest, pErrorCode );
return SystemLocale::Utf8To16( src, cchSrc, dest, cchDest, pErrorCode );
}
else if ( srcCodePage == 1252 )
{
return SystemLocale::CP1252ToUtf16( src, cchSrc < 0 ? (1+strlen(src)) : cchSrc, dest, cchDest, pErrorCode );
return SystemLocale::CP1252ToUtf16( src, cchSrc, dest, cchDest, pErrorCode );
}
}
EncodingConverter cvt( CP_UTF16, srcCodePage );
Expand All @@ -693,16 +699,21 @@ size_t SystemLocale::ToUtf16( UINT srcCodePage, const char * src, SSIZE_T cchSrc

size_t SystemLocale::ToUtf16Strict( UINT srcCodePage, const char * src, SSIZE_T cchSrc, WCHAR * dest, size_t cchDest, DWORD * pErrorCode )
{
if (cchSrc < 0) {
if (NULL != pErrorCode)
*pErrorCode = ERROR_INVALID_PARAMETER;
return 0;
}
srcCodePage = ExpandSpecialCP( srcCodePage );
if ( dest )
{
if ( srcCodePage == CP_UTF8 )
{
return SystemLocale::Utf8To16Strict( src, cchSrc < 0 ? (1+strlen(src)) : cchSrc, dest, cchDest, pErrorCode );
return SystemLocale::Utf8To16Strict( src, cchSrc, dest, cchDest, pErrorCode );
}
else if ( srcCodePage == 1252 )
{
return SystemLocale::CP1252ToUtf16( src, cchSrc < 0 ? (1+strlen(src)) : cchSrc, dest, cchDest, pErrorCode );
return SystemLocale::CP1252ToUtf16( src, cchSrc, dest, cchDest, pErrorCode );
}
}
EncodingConverter cvt( CP_UTF16, srcCodePage );
Expand Down