Skip to content

fix(security): resolve CodeQL SM02986 char*/wchar_t* cast warnings#1602

Merged
jahnvi480 merged 1 commit into
devfrom
jahnvi/codeql_s360
Apr 21, 2026
Merged

fix(security): resolve CodeQL SM02986 char*/wchar_t* cast warnings#1602
jahnvi480 merged 1 commit into
devfrom
jahnvi/codeql_s360

Conversation

@jahnvi480
Copy link
Copy Markdown
Contributor

This pull request improves the clarity and safety of type casting and buffer handling when converting between character encodings, especially around UTF-16 data. The changes add explanatory comments to document why certain casts are safe and clarify the intent behind buffer manipulations. No functional logic has changed, but the code is now safer to maintain and easier to understand.

Encoding and Buffer Handling Clarifications:

  • Added detailed comments explaining the rationale and safety of casting between char*, void*, and SQLWCHAR* (UTF-16) in string conversion and buffer allocation functions. This is done in functions such as convert_input_str_to_utf16, process_string_param, finalize_output_string, convert_string_from_utf16_inplace, and utf16_string_from_mbcs_string in core_stmt.cpp and core_util.cpp.

  • Clarified buffer handling and casting in the stream reading logic, ensuring that reinterpretation of buffers as UTF-16 is safe due to ODBC guarantees. (core_stream.cpp)

ODBC Error Handling:

  • Added comments to clarify that casting freshly allocated error message buffers to SQLWCHAR* is safe in ODBC error retrieval. (core_util.cpp)

Fixes 10 instances of CodeQL security finding SM02986 ('Cast from
char* to wchar_t*') across three shared source files.

Changes:
- core_util.cpp: Use intermediate SQLWCHAR* variable in
  convert_string_from_utf16_inplace to avoid inline cast; add
  clarifying comments for two void*->SQLWCHAR* malloc casts.
- core_stream.cpp: Extract single LPCWSTR intermediate variable before
  #ifdef branches in sqlsrv_stream_read (CP_UTF8 path) with comment
  explaining ODBC fills the buffer as SQL_C_WCHAR even though allocated
  as char*.
- core_stmt.cpp: Remove redundant reinterpret_cast in
  convert_input_str_to_utf16 (str is already char*); add typed
  intermediate variable for SQLPOINTER buffer cast in
  process_string_param; add typed intermediate for SQLWCHAR* cast in
  process_output_string; add clarifying comments for intentional
  SQLWCHAR*->char* binary storage casts.

No behavioral changes. All casts are semantically correct; changes
improve readability and satisfy the static analysis rule.

Verified: BUILD_EXIT_CODE=0 for both sqlsrv and pdo_sqlsrv extensions
against PHP 8.4.14 (VS2022 x64 ZTS).
@jahnvi480 jahnvi480 enabled auto-merge (squash) April 20, 2026 05:36
@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 20, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 85.75%. Comparing base (9e82915) to head (e5e847e).
⚠️ Report is 1 commits behind head on dev.

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##              dev    #1602   +/-   ##
=======================================
  Coverage   85.74%   85.75%           
=======================================
  Files          23       23           
  Lines        7221     7223    +2     
=======================================
+ Hits         6192     6194    +2     
  Misses       1029     1029           
Files with missing lines Coverage Δ
source/shared/core_stmt.cpp 93.52% <100.00%> (+<0.01%) ⬆️
source/shared/core_stream.cpp 86.86% <100.00%> (ø)
source/shared/core_util.cpp 89.58% <100.00%> (+0.03%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@jahnvi480 jahnvi480 merged commit c31c795 into dev Apr 21, 2026
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants