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

Cross-compile error [-Werror=conversion] #7645

Closed
MAntoniak opened this issue Aug 27, 2021 · 11 comments
Closed

Cross-compile error [-Werror=conversion] #7645

MAntoniak opened this issue Aug 27, 2021 · 11 comments
Labels

Comments

@MAntoniak
Copy link
Contributor

@MAntoniak MAntoniak commented Aug 27, 2021

I am compiling libcurl on Windows using WSL on an ARM device. I have a problem with compiling libraries in version 7.78.0. Below I present the library configuration and a compilation error. Where could the problem be?

configure: Configured to build curl/libcurl:

  Host setup:       arm-unknown-linux-gnueabihf
  Install prefix:   /usr/local/tmp/lib/linux/arm
  Compiler:         arm-linux-gnueabihf-gcc
   CFLAGS:          -mthumb -mfpu=neon-vfpv4 -mfloat-abi=hard -mcpu=cortex-a7 -Werror-implicit-function-declaration -g -O2 -pedantic -Wall -W -Wpointer-arith -Wwrite-strings -Wunused -Wshadow -Winline -Wnested-externs -Wmissing-declarations -Wmissing-prototypes -Wno-long-long -Wbad-function-cast -Wfloat-equal -Wno-multichar -Wsign-compare -Wundef -Wno-format-nonliteral -Wendif-labels -Wstrict-prototypes -Wdeclaration-after-statement -Wold-style-definition -Wstrict-aliasing=3 -Wcast-align -Wtype-limits -Wold-style-declaration -Wmissing-parameter-type -Wempty-body -Wclobbered -Wignored-qualifiers -Wconversion -Wno-sign-conversion -Wvla -ftree-vrp -Wdouble-promotion -Wformat=2 -Warray-bounds=2 -Wshift-negative-value -Wshift-overflow=2 -Wnull-dereference -fdelete-null-pointer-checks -Wduplicated-cond -Wunused-const-variable -Wduplicated-branches -Wrestrict -Walloc-zero -Wformat-overflow=2 -Wformat-truncation=2 -Wimplicit-fallthrough=4 -Wno-system-headers -pthread
   CPPFLAGS:        -mthumb -mfpu=neon-vfpv4 -mfloat-abi=hard -mcpu=cortex-a7 -isystem /usr/local/tmp/lib/linux/arm/include
   LDFLAGS:         -mcpu=cortex-a7 -L/usr/local/armhf/lib -L/usr/local/tmp/lib/linux/arm/lib
   LIBS:            -lssl -lcrypto -ldl -lpthread

  curl version:     7.78.0
  SSL:              enabled (OpenSSL)
  SSH:              no      (--with-{libssh,libssh2})
  zlib:             no      (--with-zlib)
  brotli:           no      (--with-brotli)
  zstd:             no      (--with-zstd)
  GSS-API:          no      (--with-gssapi)
  GSASL:            no      (libgsasl not found)
  TLS-SRP:          no      (--enable-tls-srp)
  resolver:         POSIX threaded
  IPv6:             no      (--enable-ipv6)
  Unix sockets:     enabled
  IDN:              no      (--with-{libidn2,winidn})
  Build libcurl:    Shared=no, Static=yes
  Built-in manual:  no      (--enable-manual)
  --libcurl option: enabled (--disable-libcurl-option)
  Verbose errors:   enabled (--disable-verbose)
  Code coverage:    disabled
  SSPI:             no      (--enable-sspi)
  ca cert bundle:   no
  ca cert path:     no
  ca fallback:      no
  LDAP:             no      (--enable-ldap / --with-ldap-lib / --with-lber-lib)
  LDAPS:            no      (--enable-ldaps)
  RTSP:             no      (--enable-rtsp)
  RTMP:             no      (--with-librtmp)
  PSL:              no      (--with-libpsl)
  Alt-svc:          no
  HSTS:             enabled (--disable-hsts)
  HTTP1:            enabled (internal)
  HTTP2:            no      (--with-nghttp2, --with-hyper)
  HTTP3:            no      (--with-ngtcp2, --with-quiche)
  ECH:              no      (--enable-ech)
  Protocols:        HTTP HTTPS MQTT
  Features:         AsynchDNS HSTS Largefile SSL UnixSockets

Making all in lib
  CC       libcurl_la-altsvc.lo
  CC       libcurl_la-amigaos.lo
  CC       libcurl_la-asyn-ares.lo
  CC       libcurl_la-asyn-thread.lo
  CC       libcurl_la-base64.lo
  CC       libcurl_la-bufref.lo
  CC       libcurl_la-c-hyper.lo
  CC       libcurl_la-conncache.lo
  CC       libcurl_la-connect.lo
  CC       libcurl_la-content_encoding.lo
  CC       libcurl_la-cookie.lo
  CC       libcurl_la-curl_addrinfo.lo
  CC       libcurl_la-curl_ctype.lo
  CC       libcurl_la-curl_des.lo
  CC       libcurl_la-curl_endian.lo
  CC       libcurl_la-curl_fnmatch.lo
  CC       libcurl_la-curl_get_line.lo
  CC       libcurl_la-curl_gethostname.lo
  CC       libcurl_la-curl_gssapi.lo
  CC       libcurl_la-curl_memrchr.lo
  CC       libcurl_la-curl_multibyte.lo
  CC       libcurl_la-curl_ntlm_core.lo
  CC       libcurl_la-curl_ntlm_wb.lo
  CC       libcurl_la-curl_path.lo
  CC       libcurl_la-curl_range.lo
  CC       libcurl_la-curl_rtmp.lo
  CC       libcurl_la-curl_sasl.lo
  CC       libcurl_la-curl_sspi.lo
  CC       libcurl_la-curl_threads.lo
  CC       libcurl_la-dict.lo
  CC       libcurl_la-doh.lo
  CC       libcurl_la-dotdot.lo
  CC       libcurl_la-dynbuf.lo
  CC       libcurl_la-easy.lo
  CC       libcurl_la-easygetopt.lo
  CC       libcurl_la-easyoptions.lo
  CC       libcurl_la-escape.lo
  CC       libcurl_la-file.lo
  CC       libcurl_la-fileinfo.lo
  CC       libcurl_la-formdata.lo
  CC       libcurl_la-ftp.lo
  CC       libcurl_la-ftplistparser.lo
  CC       libcurl_la-getenv.lo
  CC       libcurl_la-getinfo.lo
  CC       libcurl_la-gopher.lo
  CC       libcurl_la-hash.lo
  CC       libcurl_la-hmac.lo
  CC       libcurl_la-hostasyn.lo
  CC       libcurl_la-hostcheck.lo
  CC       libcurl_la-hostip.lo
  CC       libcurl_la-hostip4.lo
  CC       libcurl_la-hostip6.lo
  CC       libcurl_la-hostsyn.lo
  CC       libcurl_la-hsts.lo
  CC       libcurl_la-http.lo
  CC       libcurl_la-http2.lo
  CC       libcurl_la-http_chunks.lo
  CC       libcurl_la-http_digest.lo
  CC       libcurl_la-http_negotiate.lo
  CC       libcurl_la-http_ntlm.lo
  CC       libcurl_la-http_proxy.lo
  CC       libcurl_la-http_aws_sigv4.lo
  CC       libcurl_la-idn_win32.lo
  CC       libcurl_la-if2ip.lo
  CC       libcurl_la-imap.lo
  CC       libcurl_la-inet_ntop.lo
  CC       libcurl_la-inet_pton.lo
  CC       libcurl_la-krb5.lo
  CC       libcurl_la-ldap.lo
  CC       libcurl_la-llist.lo
  CC       libcurl_la-md4.lo
  CC       libcurl_la-md5.lo
  CC       libcurl_la-memdebug.lo
  CC       libcurl_la-mime.lo
  CC       libcurl_la-mprintf.lo
  CC       libcurl_la-mqtt.lo
  CC       libcurl_la-multi.lo
  CC       libcurl_la-netrc.lo
  CC       libcurl_la-non-ascii.lo
  CC       libcurl_la-nonblock.lo
  CC       libcurl_la-openldap.lo
  CC       libcurl_la-parsedate.lo
  CC       libcurl_la-pingpong.lo
  CC       libcurl_la-pop3.lo
  CC       libcurl_la-progress.lo
  CC       libcurl_la-psl.lo
  CC       libcurl_la-rand.lo
  CC       libcurl_la-rename.lo
  CC       libcurl_la-rtsp.lo
  CC       libcurl_la-select.lo
progress.c: In function ‘trspeed’:
progress.c:380:40: error: conversion from ‘curl_off_t’ {aka ‘long long int’} to ‘long double’ may change value [-Werror=conversion]
  380 |   return (curl_off_t)((long double)size/us * 1000000);
      |                                        ^
  CC       libcurl_la-sendf.lo
  CC       libcurl_la-setopt.lo
  CC       libcurl_la-sha256.lo
cc1: all warnings being treated as errors
make[2]: *** [Makefile:2569: libcurl_la-progress.lo] Error 1
make[2]: *** Waiting for unfinished jobs....
  CC       libcurl_la-share.lo
make[1]: *** [Makefile:1359: all] Error 2
make: *** [Makefile:1262: all-recursive] Error 1
Making install in lib
make[1]: Entering directory '/usr/local/src/curl-7.78.0/lib'
  CC       libcurl_la-progress.lo
progress.c: In function ‘trspeed’:
progress.c:380:40: error: conversion from ‘curl_off_t’ {aka ‘long long int’} to ‘long double’ may change value [-Werror=conversion]
  380 |   return (curl_off_t)((long double)size/us * 1000000);
      |                                        ^
cc1: all warnings being treated as errors
make[1]: *** [Makefile:2569: libcurl_la-progress.lo] Error 1
make[1]: Leaving directory '/usr/local/src/curl-7.78.0/lib'
make: *** [Makefile:1262: install-recursive] Error 1
@bagder bagder added the build label Aug 27, 2021
@bagder
Copy link
Member

@bagder bagder commented Aug 27, 2021

Does it make any difference if you change the long doubles to just double on that line?

@MAntoniak
Copy link
Contributor Author

@MAntoniak MAntoniak commented Aug 27, 2021

I'll try to answer that question next week.

@MAntoniak
Copy link
Contributor Author

@MAntoniak MAntoniak commented Aug 30, 2021

Unfortunately it doesn't help:

  CC       libcurl_la-smtp.lo
  CC       libcurl_la-socketpair.lo
  CC       libcurl_la-socks.lo
progress.c: In function ‘trspeed’:
progress.c:380:35: error: conversion from ‘curl_off_t’ {aka ‘long long int’} to ‘double’ may change value [-Werror=conversion]
  380 |   return (curl_off_t)((double)size/us * 1000000);
      |                                   ^
  CC       libcurl_la-socks_gssapi.lo
  CC       libcurl_la-socks_sspi.lo
  CC       libcurl_la-speedcheck.lo
  CC       libcurl_la-splay.lo
  CC       libcurl_la-strcase.lo
  CC       libcurl_la-strdup.lo
  CC       libcurl_la-strerror.lo
cc1: all warnings being treated as errors
make[2]: *** [Makefile:2569: libcurl_la-progress.lo] Error 1
make[2]: *** Waiting for unfinished jobs....
make[1]: *** [Makefile:1359: all] Error 2
make: *** [Makefile:1262: all-recursive] Error 1

I forgot to write that this error does not occur when compiling version 7.74.0

@bagder
Copy link
Member

@bagder bagder commented Aug 30, 2021

I forgot to write that this error does not occur when compiling version 7.74.0

The trspeed() function (in which this warning occurs) didn't exist back then.

This is probably due to some limitation in the floating point handling your compiler/platform impose.

@bagder
Copy link
Member

@bagder bagder commented Aug 31, 2021

You provide the -Wconversion option that apparently causes this error. I don't see how curl itself adds this warning. This looks like a case of you breaking the build for yourself.

Wrong. We also use that in debug builds without problems on my native x86-64 builds.

@MAntoniak
Copy link
Contributor Author

@MAntoniak MAntoniak commented Aug 31, 2021

I made this change in the code (similar to what was before):

static curl_off_t trspeed(curl_off_t size, /* number of bytes */
                          curl_off_t us)   /* microseconds */
{
  curl_off_t timespent;

  if(us < 1)
    return size * 1000000;

  if(size < CURL_OFF_T_MAX/1000) {
    timespent = (curl_off_t)(us+500)/1000;
    return (size * 1000 / (timespent>0?timespent:1));
  }
  else {
    timespent = (curl_off_t)(us+500000)/1000000;
    return (size / (timespent>0?timespent:1));
  }
}

After it, the compilation was successful.

@bagder
Copy link
Member

@bagder bagder commented Aug 31, 2021

This particular suggestion makes it never use microseconds at all in the calculations so it would then probably make more sense to pass in milliseconds to begin with.

Or perhaps we should divide by microseconds if the size is "large" ?

@bagder
Copy link
Member

@bagder bagder commented Aug 31, 2021

Something like this:

static curl_off_t trspeed(curl_off_t size, /* number of bytes */
                          curl_off_t us)   /* microseconds */
{
  if(us < 1)
    return size * 1000000;

  if(size < CURL_OFF_T_MAX/1000000)
    return (size * 1000000) / us;
  else if(size < CURL_OFF_T_MAX/1000)
    return (size * 1000) / (us / 1000);
  else
    return size / (us / 1000000);
}

bagder added a commit that referenced this issue Aug 31, 2021
and compiler warnings for data conversions.

Reported-by: Michał Antoniak
Fixes #7645
@MAntoniak
Copy link
Contributor Author

@MAntoniak MAntoniak commented Aug 31, 2021

static curl_off_t trspeed(curl_off_t size, /* number of bytes */
                          curl_off_t us)   /* microseconds */
{
  if(us < 1)
    return size * 1000000;

  if(size < CURL_OFF_T_MAX/1000000)
    return (size * 1000000) / us;
  else if(size < CURL_OFF_T_MAX/1000)
    return (size * 1000) / (us / 1000);
  else
    return size / (us / 1000000);
}

It works for me. Thanks for PR.

@bagder
Copy link
Member

@bagder bagder commented Aug 31, 2021

I think we can probably also optimize away the middle check (/1000) without any particular downside.

@MAntoniak
Copy link
Contributor Author

@MAntoniak MAntoniak commented Aug 31, 2021

I think we can probably also optimize away the middle check (/1000) without any particular downside.

Indeed, I've checked the idea and also it works fine.

@bagder bagder closed this in c905459 Sep 1, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

2 participants