clang-tidy: avoid assigments in if expressions#21256
clang-tidy: avoid assigments in if expressions#21256vszakats wants to merge 4 commits intocurl:masterfrom
if expressions#21256Conversation
```
/home/runner/work/curl/curl/lib/vtls/x509asn1.c:163:15: error: an assignment within an 'if' condition is bug-prone [bugprone-assignment-in-if-condition,-warnings-as-errors]
163 | else if(!(b &= 0x7F)) {
| ~~^~~~~~~
/home/runner/work/curl/curl/lib/vtls/x509asn1.c:163:15: note: if it should be an assignment, move it out of the 'if' condition
/home/runner/work/curl/curl/lib/vtls/x509asn1.c:163:15: note: if it is meant to be an equality check, change '=' to '=='
```
There was a problem hiding this comment.
Pull request overview
This PR tidies up a couple of code paths to avoid assignments inside if conditions, and enables the corresponding clang-tidy check to enforce the pattern going forward.
Changes:
- Refactor ASN.1 element length parsing to avoid
b &= ...inside anifcondition. - Refactor GnuTLS
load_file()error handling to avoid assignments within a compoundif. - Enable
bugprone-assignment-in-if-conditionin.clang-tidy.yml.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
lib/vtls/x509asn1.c |
Reworks ASN.1 length parsing control flow to avoid assignment-in-condition. |
lib/vtls/gtls.c |
Rewrites load_file() checks into stepwise validation with goto out. |
.clang-tidy.yml |
Enables clang-tidy check to flag assignment in if conditions. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| filelen = ftell(f); | ||
| if(filelen < 0) | ||
| goto out; | ||
| if(fseek(f, 0, SEEK_SET) != 0) | ||
| goto out; | ||
| ptr = curlx_malloc((size_t)filelen); | ||
| if(!ptr) |
There was a problem hiding this comment.
filelen is a long from ftell(), but it gets cast to size_t for allocation/read and later stored into loaded_file.size (a gnutls_datum_t size field, typically unsigned int). If filelen exceeds UINT_MAX (or SIZE_MAX), this will truncate the reported size and/or attempt an oversized allocation. Consider adding an explicit upper-bound check (e.g., fail/return empty when filelen > (long)UINT_MAX) before curlx_malloc().
if expressionsif expressions
Used for issuer certs. Limit the size at `CURL_MAX_INPUT_LENGTH`, 8MB. Bug: #21256 (comment) Closes #21257
Also enable check in clang-tidy.
Cherry-picked from #20794
https://github.com/curl/curl/pull/21256/files?w=1