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

build: enable -Wsign-conversion warnings and fix/silence them (OpenSSF) #13489

Open
wants to merge 120 commits into
base: master
Choose a base branch
from

Conversation

vszakats
Copy link
Member

@vszakats vszakats commented Apr 27, 2024

  • silence all -Wsign-conversion warnings, mostly by casts and type changes.
  • disable -Wsign-conversion warning for FD_SET()/FD_ISSET() macros.
    These trigger on some systems due to the macros' implementation.
  • build: enable (non-fatal) -Wsign-conversion warnings (OpenSSF).
  • build: make -Wsign-conversion an error (OpenSSF).

Related fixes:

  • fix signedness in some printf masks.
  • tests: switch unit tests to return CURLcode.tests: make the unit test result type CURLcode #13600
  • mingw declares sin_family/sin6_family as short.
  • openssl: fix ctx_option_t type for openssl 1.1.x
  • sectransp: fix CURLcode/OSStatus mix-up.
  • tool_parsecfg: fix ParameterError mix-up.
  • fix assert in Curl_hash_add.

Follow-up to 3829759 #12489
Closes #13489

@vszakats vszakats marked this pull request as draft April 27, 2024 11:59
@github-actions github-actions bot added cmdline tool tests CI Continuous Integration labels Apr 27, 2024
@vszakats vszakats changed the title [WIP] build: build: enable -Wsign-conversion warnings [WIP] build: enable -Wsign-conversion warnings Apr 27, 2024
@vszakats vszakats force-pushed the signwarn-10 branch 2 times, most recently from 1faf8eb to b73157c Compare April 27, 2024 17:21
@vszakats
Copy link
Member Author

vszakats commented Apr 29, 2024

PR now passes all CI builds and tests.

It did help finding small issues, but I haven't investigated all possible ones, only the trivial ones.
Some of the fixed ones were type disagreements, error code confusions, wrong types assumed
at API boundaries, and wrong casts.

About half of the patch is addressing CURLcode/int mixups in unit tests. They now all
universally use CURLcode.

To my eyes the resulting code better expresses what's happening and more clearly describes
component boundaries. It also makes it easier to spot (or grep) potential issues arising from
switching between signed/unsigned size variables, IMO.

There is no exact number for the warnings fixed. There were about 450 in the first round and
in the ballpark of 900 total after addressing all, disregarding unit tests. Relative to the size of
the codebase, this number doesn't seem to be high and suggest a clean codebase.

Of these, around 140 were silenced with the (size_t)(ptr1 - ptr2) pattern, which we discussed
earlier.

It's possible that warnings are still present in code not compiled in CI and/or build combinations
triggering more.

Next steps?:

I'll force push now to cleanup to sub-commits. [DONE]

@vszakats vszakats marked this pull request as ready for review April 29, 2024 10:30
@vszakats vszakats changed the title [WIP] build: enable -Wsign-conversion warnings build: enable -Wsign-conversion warnings and fix/silence them all Apr 29, 2024
@vszakats vszakats changed the title build: enable -Wsign-conversion warnings and fix/silence them all build: enable -Wsign-conversion warnings and fix/silence them Apr 29, 2024
@vszakats vszakats force-pushed the signwarn-10 branch 2 times, most recently from 7364f36 to 98c57d8 Compare April 29, 2024 11:06
@vszakats vszakats changed the title build: enable -Wsign-conversion warnings and fix/silence them build: enable -Wsign-conversion warnings and fix/silence them (OpenSSF) Apr 29, 2024
@vszakats vszakats force-pushed the signwarn-10 branch 2 times, most recently from 8bf863f to 4dd290d Compare April 29, 2024 12:00
@vszakats
Copy link
Member Author

vszakats commented Apr 29, 2024

Separate PRs for the major curl components:

The above are the same sub-commits as here, except two controlling the warning option, which is unique to this one.

bagder added a commit that referenced this pull request Apr 30, 2024
To match the type used in 'set.happy_eyeballs_timeout'.

Ref: #13489
Copy link
Member

@bagder bagder left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just think that adding typecasts in hundreds of new places is not a big win. They just silence the warnings with code that is a tad bit harder to read, the same logic is still there.

The improvements done by changing variable types and prototypes are the interesting ones I think. A typecast is always a bit of a "I surrender" sign.

lib/getinfo.c Outdated Show resolved Hide resolved
lib/getinfo.c Outdated Show resolved Hide resolved
Documentation says it's `long`, but really is `unsigned long`.

```
vtls/openssl.c:3695:38: error: conversion to 'long unsigned int' from 'ctx_option_t' {aka 'long int'} may change the sign of the result [-Werror=sign-conversion]
 3695 |   SSL_CTX_set_options(octx->ssl_ctx, ctx_options);
      |                                      ^~~~~~~~~~~
```
Ref: https://github.com/curl/curl/actions/runs/8868596554/job/24348345926#step:31:490
temp:
```
C:/projects/curl/lib/vtls/x509asn1.c:961:12: error: cast from function call of type 'CURLcode' to non-matching type 'int' [-Werror=bad-function-cast]
  961 |     return (int)do_pubkey_field(data, certnum, "ecPublicKey", pubkey);
      |            ^
```
Ref: https://ci.appveyor.com/project/curlorg/curl/builds/49704256/job/hcietrd5e7qbu73c#L326
```
tool_getparam.c:2904:37: error: implicit conversion changes signedness: 'ParameterError' to 'int' [-Werror,-Wsign-conversion]
```
Ref: https://github.com/curl/curl/actions/runs/8861448063/job/24333320546?pr=13489#step:10:1257
vszakats added a commit that referenced this pull request May 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

None yet

2 participants