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

Closed
wants to merge 94 commits into from

Conversation

vszakats
Copy link
Member

@vszakats vszakats commented Apr 27, 2024

  • silence all -Wsign-conversion warnings by size_t and curl_off_t casts.
  • build: enable (non-fatal) -Wsign-conversion warnings (OpenSSF).
  • build: make -Wsign-conversion an error (OpenSSF).

Related fixes, including type changes or casts in interface boundaries:

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
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
```
D:/a/curl/curl/tests/unit/unit1604.c:322:37: error: implicit conversion changes signedness: 'SANITIZEcode' to 'int' [-Werror,-Wsign-conversion]
  322 |     received_ccstr = getcurlcodestr(res);
      |                      ~~~~~~~~~~~~~~ ^~~
D:/a/curl/curl/tests/unit/unit1604.c:324:45: error: implicit conversion changes signedness: 'SANITIZEcode' to 'int' [-Werror,-Wsign-conversion]
  324 |     expected_ccstr = getcurlcodestr(data[i].expected_result);
      |                      ~~~~~~~~~~~~~~ ~~~~~~~~^~~~~~~~~~~~~~~
```
Ref: https://github.com/curl/curl/actions/runs/9340164046/job/25705385260#step:13:795
```
../../lib/krb5.c:633:60: error: implicit conversion changes signedness: 'enum protection_level' to 'int' [-Werror,-Wsign-conversion]
  bytes = conn->mech->encode(conn->app_data, from, length, prot_level,
          ~~~~                                             ^~~~~~~~~~
../../lib/krb5.c:727:36: error: implicit conversion changes signedness: 'enum protection_level' to 'int' [-Werror,-Wsign-conversion]
                                   level, conn);
                                   ^~~~~
```
Ref: https://github.com/curl/curl/actions/runs/9340164051/job/25705383216?pr=13489#step:3:1411
```
../../../tests/libtest/sethostname.c: In function 'gethostname':
  ../../../tests/libtest/sethostname.c:34:35: error: conversion to 'size_t' {aka 'long unsigned int'} from 'int' may change the sign of the result [-Werror=sign-conversion]
     34 |     strncpy(name, force_hostname, namelen);
        |                                   ^~~~~~~
```
Ref: https://github.com/curl/curl/actions/runs/9340164051/job/25705383801?pr=13489#step:3:8501
@vszakats
Copy link
Member Author

vszakats commented Jun 6, 2024

-Wsign-conversion is unwanted in curl.

@vszakats vszakats closed this Jun 6, 2024
@vszakats vszakats deleted the signwarn-10 branch June 6, 2024 21:41
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