GeezerVison is showing github.com — Address CodeQL issues (#1615) · dlevy-msft-sql/msphpsql@4f8f2d6
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboard +10Lines changed: 10 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +queries: | | 2 | + # Exclude all external PHP SDK source code | | 3 | + - exclude: | | 4 | + path: | | 5 | + - "buildscripts/php-sdk" | | 6 | + # Include only the SQLSRV and PDO_SQLSRV source files within the PHP SDK, where we drop them for compilation | | 7 | + - include: | | 8 | + path: | | 9 | + - "buildscripts/php-sdk/**/sqlsrv" | | 10 | + - "buildscripts/php-sdk/**/pdo_sqlsrv" |
Collapse file Copy file name to clipboardExpand all lines: source/shared/core_sqlsrv.h +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -1462,7 +1462,7 @@ struct sqlsrv_param | 1462 | 1462 |
| 1463 | 1463 | void copy_param_meta_ae(_Inout_ zval* param_z, _In_ param_meta_data& meta); // Only used when Always Encrypted is enabled | 1464 | 1464 |
| 1465 | | - virtual ~sqlsrv_param(){ release_data(); } | | 1465 | + virtual ~sqlsrv_param(){ sqlsrv_param::release_data(); } | 1466 | 1466 | virtual void release_data(); | 1467 | 1467 |
| 1468 | 1468 | bool derive_string_types_sizes(_In_ zval* param_z); | @@ -1539,7 +1539,7 @@ struct sqlsrv_param_tvp : public sqlsrv_param | 1539 | 1539 | { | 1540 | 1540 | ZVAL_UNDEF(&placeholder_z); | 1541 | 1541 | } | 1542 | | - virtual ~sqlsrv_param_tvp() { release_data(); } | | 1542 | + virtual ~sqlsrv_param_tvp() { sqlsrv_param_tvp::release_data(); } | 1543 | 1543 | virtual void release_data(); | 1544 | 1544 | virtual void bind_param(_Inout_ sqlsrv_stmt* stmt); | 1545 | 1545 | virtual void process_param(_Inout_ sqlsrv_stmt* stmt, _Inout_ zval* param_z); | |
Collapse file Copy file name to clipboardExpand all lines: source/shared/core_stmt.cpp +4-3Lines changed: 4 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -2911,9 +2911,10 @@ void sqlsrv_param_inout::finalize_output_string() | 2911 | 2911 | char* outString = NULL; | 2912 | 2912 | SQLLEN outLen = 0; | 2913 | 2913 |
| 2914 | | - // When encoding is UTF-8 or SYSTEM, ODBC returns data as UTF-16 wide characters | 2915 | | - // in the output parameter buffer. ODBC guarantees proper alignment and valid UTF-16 data. | 2916 | | - const SQLWCHAR* wide_str = reinterpret_cast<const SQLWCHAR*>(str); | | 2914 | + // For UTF-8/SYSTEM encoding, the output parameter buffer was bound with SQL_C_WCHAR via | | 2915 | + // SQLBindParameter, so ODBC wrote UTF-16 data into it. The reinterpret_cast is necessary | | 2916 | + // because the ODBC API binds output params to a generic char* buffer. | | 2917 | + const SQLWCHAR* wide_str = reinterpret_cast<const SQLWCHAR*>(str); // CodeQL [SM02986] buffer contains UTF-16 data from ODBC output parameter bound with SQL_C_WCHAR | 2917 | 2918 | bool result = convert_string_from_utf16(encoding, wide_str, int(str_len / sizeof(SQLWCHAR)), &outString, outLen); | 2918 | 2919 | CHECK_CUSTOM_ERROR(!result, stmt, SQLSRV_ERROR_OUTPUT_PARAM_ENCODING_TRANSLATE, get_last_error_message(), NULL) { | 2919 | 2920 | throw core::CoreException(); | |
Collapse file Copy file name to clipboardExpand all lines: source/shared/core_stream.cpp +4-4Lines changed: 4 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -200,10 +200,10 @@ size_t sqlsrv_stream_read(_Inout_ php_stream* stream, _Out_writes_bytes_(count) | 200 | 200 | throw core::CoreException(); | 201 | 201 | } | 202 | 202 |
| 203 | | -// When encoding is UTF-8, SQLGetData fills temp_buf with UTF-16 wide character data (SQL_C_WCHAR). | 204 | | - // The buffer is allocated as char* but contains properly aligned UTF-16 data from ODBC. | 205 | | - // Reinterpret as LPCWSTR for conversion functions. This is safe because ODBC guarantees proper alignment. | 206 | | - const LPCWSTR wide_buffer = reinterpret_cast<LPCWSTR>( temp_buf.get() ); | | 203 | + // temp_buf contains UTF-16 data written by the ODBC driver via SQLGetData with SQL_C_WCHAR | | 204 | + // (set at line 88 when ss->encoding == CP_UTF8). The char* buffer is a generic allocation; | | 205 | + // the reinterpret_cast is necessary because the ODBC API uses SQLPOINTER (void*) buffers. | | 206 | + const LPCWSTR wide_buffer = reinterpret_cast<LPCWSTR>( temp_buf.get() ); // CodeQL [SM02986] buffer contains UTF-16 data from ODBC SQLGetData with SQL_C_WCHAR | 207 | 207 | #ifndef _WIN32 | 208 | 208 | int enc_len = SystemLocale::FromUtf16( ss->encoding, wide_buffer, | 209 | 209 | static_cast<int>(read >> 1), buf, static_cast<int>(count), NULL, NULL ); | |
Collapse file Copy file name to clipboardExpand all lines: source/shared/core_util.cpp +5-3Lines changed: 5 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -106,9 +106,11 @@ bool convert_string_from_utf16_inplace( _In_ SQLSRV_ENCODING encoding, _Inout_up | 106 | 106 | char* outString = NULL; | 107 | 107 | SQLLEN outLen = 0; | 108 | 108 |
| 109 | | - // The string buffer contains UTF-16 encoded data. Reinterpret as SQLWCHAR* for conversion. | 110 | | - // This is safe because the caller ensures the buffer contains valid UTF-16 data. | 111 | | - const SQLWCHAR* wide_str = reinterpret_cast<const SQLWCHAR*>(*string); | | 109 | + // The buffer contains UTF-16 data written by the ODBC driver via SQLGetData with SQL_C_WCHAR. | | 110 | + // The only caller (get_field_as_string in core_stmt.cpp) guards this call with | | 111 | + // "if (c_type == SQL_C_WCHAR)", ensuring the buffer was populated as UTF-16 by the driver. | | 112 | + // The reinterpret_cast is necessary because the ODBC API uses a generic char* buffer. | | 113 | + const SQLWCHAR* wide_str = reinterpret_cast<const SQLWCHAR*>(*string); // CodeQL [SM02986] buffer is ensured to contain UTF-16 data | 112 | 114 | bool result = convert_string_from_utf16( encoding, wide_str, int(len / sizeof(SQLWCHAR)), &outString, outLen ); | 113 | 115 |
| 114 | 116 | if (result) | |
Collapse file Copy file name to clipboardExpand all lines: test/tools/mock_tds_server.py +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -1341,7 +1341,7 @@ def generate_self_signed_cert(cert_path, key_path): | 1341 | 1341 | .public_key(key.public_key()) | 1342 | 1342 | .serial_number(x509.random_serial_number()) | 1343 | 1343 | .not_valid_before(datetime.datetime.now(datetime.timezone.utc)) | 1344 | | - .not_valid_after(datetime.datetime.now(datetime.timezone.utc) + datetime.timedelta(days=365)) | | 1344 | + .not_valid_after(datetime.datetime.now(datetime.timezone.utc) + datetime.timedelta(days=30)) | 1345 | 1345 | .sign(key, hashes.SHA256()) | 1346 | 1346 | ) | 1347 | 1347 | with open(key_path, "wb") as f: | |
|