Skip to content

ws: -Wtype-limits warning in ws_payload_remain() #19017

Description

@vszakats

I did this

Built curl for MS-DOS in CI, where the build reaches this line due to SIZEOF_OFF_T <= SIZEOF_SIZE_T (4 <= 4):

lib/ws.c: In function 'ws_payload_remain':
lib/ws.c:658:35: error: comparison is always false due to limited range of data type [-Werror=type-limits]
  658 |   if((curl_off_t)payload_buffered < 0)
      |                                   ^

https://github.com/curl/curl/actions/runs/18420572155/job/52493664744?pr=18039#step:9:27

Ref: a4d3c4e #18965

I expected the following

Clean build.

curl/libcurl version

master

operating system

MS-DOS (maybe others with SIZEOF_OFF_T <= SIZEOF_SIZE_T)

Activity

  1. changed the title [-]ws: `-Wtype-limits` warning in `ws_payload_remain`[/-] [+]ws: `-Wtype-limits` warning in `ws_payload_remain()`[/+] on Oct 11, 2025
  2. bagder commented on Oct 11, 2025

    @bagder
    Member

    lib/ws.c:658:35: error: comparison is always false due to limited range of data type [-Werror=type-limits]

    What is curl_off_t on this machine? I think it needs to be a signed type and then I can't understand why this warns!

  3. vszakats commented on Oct 11, 2025

    @vszakats
    MemberAuthor

    It should be a long long according to system.h. I could not spot a platform where it's unsigned. Found the warning odd also. (Haven't run this build locally yet)

  4. bagder commented on Oct 12, 2025

    @bagder
    Member

    We could try to rewrite it to avoid the typecast:

    diff --git a/lib/ws.c b/lib/ws.c
    index 6c8f07e177..fa6602a152 100644
    --- a/lib/ws.c
    +++ b/lib/ws.c
    @@ -653,11 +653,11 @@ static curl_off_t ws_payload_remain(curl_off_t payload_total,
     {
       curl_off_t remain = payload_total - payload_offset;
       if((payload_total < 0) || (payload_offset < 0) || (remain < 0))
         return -1;
     #if SIZEOF_OFF_T <= SIZEOF_SIZE_T
    -  if((curl_off_t)payload_buffered < 0)
    +  if(payload_buffered > SIZE_MAX/2)
         return -1;
     #endif
       if(remain < (curl_off_t)payload_buffered)
         return -1;
       return remain - (curl_off_t)payload_buffered;
  5. jay commented on Oct 12, 2025

    @jay
    Member

    It's implementation defined to cast an unsigned type to a signed if it doesn't fit. So for all we know this compiler will set a too large positive number to something positive like 0 or fffff whatever, which is unusual, but OTOH we're compiling for DOS and I don't know where else that warning would come from.

    Regardless the guard looks wrong because curl_off_t is never less than 8 bytes anywhere, so the check shouldn't be against SIZEOF_OFF_T but rather SIZEOF_CURL_OFF_T. As it happens once that is fixed it would never be true for DOS or Windows targets because #if 8 <= 4 is false so payload_buffered should always fit in a curl_off_t

    #if SIZEOF_CURL_OFF_T <= SIZEOF_SIZE_T
      if(payload_buffered > (size_t)CURL_OFF_T_MAX)
        return -1;
    #endif

    edit: Well I should have figured this out half an hour ago when I posted but I think that warning comes because the guard was wrong so "limited range of data type" is true in that the size_t always fits in the curl_off_t.

  6. added a commit that references this issue on Oct 12, 2025
  7. added a commit that references this issue on Oct 14, 2025
  8. vszakats commented on Oct 14, 2025

    @vszakats
    MemberAuthor

    Thanks @jay!

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions