Skip to content

mbedtls: fix using garbage value (reported by clang-tidy) - #1729

Closed
vszakats wants to merge 1 commit into
libssh2:masterfrom
vszakats:mbedtls-garbage-value
Closed

vszakats wants to merge 1 commit into
libssh2:masterfrom
vszakats:mbedtls-garbage-value

Conversation

@vszakats

@vszakats vszakats commented Oct 18, 2025 •

Copy link
Copy Markdown
Member

In _libssh2_mbedtls_pub_priv_key() on a NON-error code path, a stack
variable was checked without initializing it first.

I found it interesting that clang-tidy did not find this when building
against the system mbedtls (2.x) with 2.x compatibility code still in.
Then it did find it when using a manual build of mbedtls 3.1.0 with
2.x compatibility code deleted from libssh2. Being such a trivial error
I wonder why no compiler ever detected it as a regular warning.

linux (clang-tidy, amd64, mbedTLS-prev [3.1.0], cmake, ON):

src/mbedtls.c:744:8: error: Branch condition evaluates to a garbage value [clang-analyzer-core.uninitialized.Branch,-warnings-as-errors]
  744 |     if(ret) {
      |        ^

Ref: https://github.com/libssh2/libssh2/actions/runs/18620615649/job/53091295760#step:22:44

Follow-up to 186f1a2 #132
Cherry-picked from #1727

In `_libssh2_mbedtls_pub_priv_key()` on a NON-error code path, a stack
variable was checked without initializing it first.

I found it interesting that clang-tidy did not find this when building
against the system mbedtls (2.x) with 2.x compatibility code still it.
Then it did find it when using a manual build of mbedtls 3.1.0 with
2.x compatibility code deleted from libssh2. Being such a trivial error
I wonder why no compiler ever detected it as a regular warning.

linux (clang-tidy, amd64, mbedTLS-prev [3.1.0], cmake, ON):
```
/home/runner/work/libssh2/libssh2/src/mbedtls.c:744:8: error: Branch condition evaluates to a garbage value [clang-analyzer-core.uninitialized.Branch,-warnings-as-errors]
  744 |     if(ret) {
      |        ^
```
Ref: https://github.com/libssh2/libssh2/actions/runs/18620615649/job/53091295760#step:22:44

Follow-up to 186f1a2 libssh2#132
Cherry-picked from libssh2#1727
@vszakats

Copy link
Copy Markdown
Member Author

FTR the full clang-tidy error report:

/home/runner/work/libssh2/libssh2/src/mbedtls.c:744:8: error: Branch condition evaluates to a garbage value [clang-analyzer-core.uninitialized.Branch,-warnings-as-errors]
  744 |     if(ret) {
      |        ^
/home/runner/work/libssh2/libssh2/src/mbedtls.c:777:8: note: Assuming 'ret' is 0
  777 |     if(ret) {
      |        ^~~
/home/runner/work/libssh2/libssh2/src/mbedtls.c:777:5: note: Taking false branch
  777 |     if(ret) {
      |     ^
/home/runner/work/libssh2/libssh2/src/mbedtls.c:783:11: note: Calling '_libssh2_mbedtls_pub_priv_key'
  783 |     ret = _libssh2_mbedtls_pub_priv_key(session, method, method_len,
      |           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  784 |                                         pubkeydata, pubkeydata_len, &pkey);
      |                                         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/runner/work/libssh2/libssh2/src/mbedtls.c:718:5: note: 'ret' declared without an initial value
  718 |     int ret;
      |     ^~~~~~~
/home/runner/work/libssh2/libssh2/src/mbedtls.c:721:8: note: Assuming the condition is false
  721 |     if(mbedtls_pk_get_type(pkey) != MBEDTLS_PK_RSA) {
      |        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/runner/work/libssh2/libssh2/src/mbedtls.c:721:5: note: Taking false branch
  721 |     if(mbedtls_pk_get_type(pkey) != MBEDTLS_PK_RSA) {
      |     ^
/home/runner/work/libssh2/libssh2/src/mbedtls.c:730:8: note: Assuming 'mth' is non-null
  730 |     if(mth) {
      |        ^~~
/home/runner/work/libssh2/libssh2/src/mbedtls.c:730:5: note: Taking true branch
  730 |     if(mth) {
      |     ^
/home/runner/work/libssh2/libssh2/src/mbedtls.c:739:9: note: 'key' is non-null
  739 |     if(!key) {
      |         ^~~
/home/runner/work/libssh2/libssh2/src/mbedtls.c:739:5: note: Taking false branch
  739 |     if(!key) {
      |     ^
/home/runner/work/libssh2/libssh2/src/mbedtls.c:744:8: note: Branch condition evaluates to a garbage value
  744 |     if(ret) {
      |        ^~~
4 warnings generated.

@vszakats vszakats closed this in 16ce0ec Oct 18, 2025
@vszakats
vszakats deleted the mbedtls-garbage-value branch October 18, 2025 21:59
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.

1 participant