Skip to content

msvc: drop exception, make BIT() a bitfield with Visual Studio - #20142

Closed
vszakats wants to merge 13 commits into
curl:masterfrom
vszakats:msvcbit
Closed

vszakats wants to merge 13 commits into
curl:masterfrom
vszakats:msvcbit

Conversation

@vszakats

@vszakats vszakats commented Jan 1, 2026 •

Copy link
Copy Markdown
Member

Add casts to bool, or use bit type in local variables, where
neccessary to avoid MSVC compiler warnings C4242.

Note: There may remain places needing the above updates, where not
tested in CI, and missed in manual review.

Also:

  • urldata: convert struct field connect_only to bitfield to match its
    counterpart in another struct.
  • rename curl-specific bit type to curl_bit.

Also tried switching over bool arguments to bit, to avoid casts.
I expected it to work, but in practice it resulted in GCC warnings,
possibly where the bitfields ended up using different integer types
depending on how many of them were declared next to each other. That's
my guess at least.

@vszakats
vszakats marked this pull request as draft January 1, 2026 15:39
@vszakats vszakats added the feature-window A merge of this requires an open feature window label Jan 2, 2026
@vszakats vszakats changed the title msvc: try building without BIT() hack msvc: make BIT() a bitfield Jan 2, 2026
@vszakats vszakats added the Windows Windows-specific label Jan 2, 2026
@vszakats vszakats changed the title msvc: make BIT() a bitfield msvc: drop exception, make BIT() a bitfield Jan 2, 2026
@vszakats
vszakats marked this pull request as ready for review January 2, 2026 03:01
@github-actions github-actions Bot added the CI Continuous Integration label Jan 2, 2026
@vszakats vszakats changed the title msvc: drop exception, make BIT() a bitfield msvc: drop exception, make BIT() a bitfield with Visual Studio Jan 2, 2026
@vszakats
vszakats force-pushed the msvcbit branch 9 times, most recently from a7f1a2a to daddef4 Compare January 15, 2026 12:20
…penssl passes without cast)"

This reverts commit bd4447b.
src\tool_cb_wrt.c(247,23): error C2220: the following warning is treated as an error
src\tool_cb_wrt.c(247,23): warning C4242: 'initializing': conversion from 'bit' to 'bool', possible loss of data
src\tool_operate.c(1141,32): warning C4242: 'initializing': conversion from 'bit' to 'bool', possible loss of data
src\tool_operate.c(1142,28): warning C4242: 'initializing': conversion from 'bit' to 'bool', possible loss of data
src\tool_operate.c(2148,32): warning C4242: 'initializing': conversion from 'bit' to 'bool', possible loss of data
src\tool_operate.c(2149,28): warning C4242: 'initializing': conversion from 'bit' to 'bool', possible loss of data

https://ci.appveyor.com/project/curlorg/curl/builds/53311787/job/yfpcwxuuyl2qq14v
@vszakats vszakats closed this in 85c841c Jan 17, 2026
@vszakats
vszakats deleted the msvcbit branch January 17, 2026 10:47
vszakats added a commit that referenced this pull request Oct 16, 2026
…ol type

Setting the `CURLWS_NOAUTOPONG` flag via the `CURLOPT_WS_OPTIONS`
`curl_easy_setopt()` API failed to enable this flag in libcurl builds
using a compiler without a native C99 bool type. This in turn caused
2700, 2709, 2711, 2712 WebSocket tests to fail.

The initial WebSocket code assigned `arg & CURLWS_RAW_MODE` to a
bit-field, after casting the expression to `bool`. This worked because
`CURLWS_RAW_MODE` equals 1. Later this line was cloned when adding
support for `CURLWS_NOAUTOPONG`, but this new bit flag's value was 2.
Casting it to `bool` and then assigning it to a bit-field worked as
expected, as long as the bool type was the C99 one. On compilers without
the C99 bool type, the interim cast did not work, and when assigning the
value 2 to the bit-field, it ended up storing zero/false
unconditionally.

Such compilers are VS2012 and VS2010. For them, another trigger was a
subsequent update that enabled bit-field support for MSVC compilers.

Fix by replacing the `(bool)` cast with `!!`. Also for
`CURLWS_RAW_MODE`, to not rely on the literal value of this macro, and
to avoid reusing this pattern in future code.

There were no other places in the codebase with this pattern. The rest
use either `!!` or ternary expression for this type of conversion.

Discovered while trying to enable tests for MSVC builds in AppVeyor CI,
causing test failures with VS2012 and VS2010.

Refs:
https://en.cppreference.com/c/language/arithmetic_types#Boolean_type
https://en.cppreference.com/c/language/conversion
https://godbolt.org/z/3h5zMfePq

Bug: #23306 (comment)
Bug: #23306 (comment)
Follow-up to 85c841c #20142
Follow-up to c0df01f #16744
Follow-up to 664249d #8995

Cherry-picked from #23306

Closes #23428
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CI Continuous Integration cmdline tool feature-window A merge of this requires an open feature window tests Windows Windows-specific

Development

Successfully merging this pull request may close these issues.

1 participant