warmcat / libwebsockets

canonical libwebsockets.org networking library

Home Page:https://libwebsockets.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Build error [-Werror=conversion]

jvasanthan opened this issue · comments

Hi,
I am cross-compiling v4.3.3 (tried v4.3-stable as well) and I get the following errors. The warnings are treated as errors.

libwebsockets/lib/plat/unix/unix-sockets.c:438:10: error: conversion to 'short unsigned int' from 'unsigned int' may alter its value [-Werror=conversion] ucs += ntohs(*p16++);

libwebsockets/lib/plat/unix/unix-sockets.c:450:21: error: conversion to 'short unsigned int' from 'unsigned int' may alter its value [-Werror=conversion] sll.sll_protocol = htons(0x800);

Please recommend how to fix the issue.

What's your platform / compiler? I can't reproduce these with gcc13.

Does this help?

diff --git a/lib/plat/unix/unix-sockets.c b/lib/plat/unix/unix-sockets.c
index e06e83b3..b72098ab 100644
--- a/lib/plat/unix/unix-sockets.c
+++ b/lib/plat/unix/unix-sockets.c
@@ -479,7 +479,7 @@ lws_plat_rawudp_broadcast(uint8_t *p, const uint8_t *canned, size_t canned_len,
        p[3] = (uint8_t)(n);
 
        while (p16 < (uint16_t *)(p + 20))
-               ucs += ntohs(*p16++);
+               ucs += (uint32_t)ntohs(*p16++);
 
        ucs += ucs >> 16;
        ucs ^= 0xffff;
@@ -491,7 +491,7 @@ lws_plat_rawudp_broadcast(uint8_t *p, const uint8_t *canned, size_t canned_len,
 
        memset(&sll, 0, sizeof(sll));
        sll.sll_family = AF_PACKET;
-       sll.sll_protocol = htons(0x800);
+       sll.sll_protocol = (uint16_t)htons(0x800);
        sll.sll_halen = 6;
        sll.sll_ifindex = (int)if_nametoindex(iface);
        memset(sll.sll_addr, 0xff, 6);

Hi Andy,
I am using cross compiler and unfortunately its older gcc version - 6.3.0
tried your patch, didn't work. I tried to cast it in different ways none worked. I had to use -DDISABLE_WERROR to compile.
Somewhere the "unsigned short int" got promoted to" unsigned int", it could have happened inside the byte-swaping code.

I have the same issue building lws v4.3.3 with gcc 5.4 (mips32 cross toolchain)

open-source/libwebsockets/build/src/project_libwebsockets/lib/plat/unix/unix-sockets.c:438:10: error: conversion to 'short unsigned int' from 'unsigned int' may alter its value [-Werror=conversion]
   ucs += ntohs(*p16++);
          ^
open-source/libwebsockets/build/src/project_libwebsockets/lib/plat/unix/unix-sockets.c:438:10: error: conversion to 'short unsigned int' from 'unsigned int' may alter its value [-Werror=conversion]
open-source/libwebsockets/build/src/project_libwebsockets/lib/plat/unix/unix-sockets.c:450:21: error: conversion to 'short unsigned int' from 'unsigned int' may alter its value [-Werror=conversion]
  sll.sll_protocol = htons(0x800);
                     ^
open-source/libwebsockets/build/src/project_libwebsockets/lib/plat/unix/unix-sockets.c:450:21: error: conversion to 'short unsigned int' from 'unsigned int' may alter its value [-Werror=conversion]
\

It worked fine on lws v4.2.2

I'm not sure what can be done about this, but perhaps one clue is that I don't see any change in this code between v4.2.2 and v4.3.3, whereas it's reported it "worked fine on v4.2.2" with gcc 5.4. I added a patch elaborating the casting manually but it feels as suggested, this is "happening inside the byte swapping code" on these specific old toolchains.