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

libusb: add stdio.h header #1327

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

libusb: add stdio.h header #1327

wants to merge 1 commit into from

Conversation

neheb
Copy link
Contributor

@neheb neheb commented Oct 16, 2023

Needed for printf functions. Not always included implicitly.

Copy link
Member

@Youw Youw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd say that relying on implicit behavior in general is one of the sources of eternal evil.

@Youw
Copy link
Member

Youw commented Oct 16, 2023

Out of curiosity: which compiler did catch this (or what was special about the build environment otherwise)?

@neheb
Copy link
Contributor Author

neheb commented Oct 16, 2023

MinGW specifically

@mcuee mcuee added the Misc Whitespace, typo, coding style, etc label Oct 16, 2023
@mcuee
Copy link
Member

mcuee commented Oct 16, 2023

MinGW specifically

Just wondering which version of MinGW? Take note we do not really support MinGW.org 32bit compiler (but minor PR will be reviewed and accepted if appropriate).

We recommend MSYS2 distribution of mingw32/mingw64 (and accept PRs to improve MSYS2 environment like ucrt64, clang32, clang64 and clangarm64).

https://www.msys2.org/docs/environments/

  Name Prefix Toolchain Architecture C Library C++ Library
  UCRT64 /ucrt64 gcc x86_64 ucrt libstdc++
  CLANG64 /clang64 llvm x86_64 ucrt libc++
  CLANGARM64 /clangarm64 llvm aarch64 ucrt libc++
  CLANG32 /clang32 llvm i686 ucrt libc++
  MINGW64 /mingw64 gcc x86_64 msvcrt libstdc++
  MINGW32 /mingw32 gcc i686 msvcrt libstdc++

@neheb
Copy link
Contributor Author

neheb commented Oct 17, 2023

Ubuntu MinGW 64 cross compile.

@mcuee
Copy link
Member

mcuee commented Oct 17, 2023

Ubuntu MinGW 64 cross compile.

I see. Then this PR is good to me.

@tormodvolden
Copy link
Contributor

tormodvolden commented Oct 17, 2023

I also always cross-compile on Ubuntu (20.04, gcc 9.3) or Debian (gcc 12.2). But I don't see the warning. Do you add extra flags? Build log extract?

@Youw
Copy link
Member

Youw commented Oct 17, 2023

I'm pretty sure this is compiler-version specific. I've seen it multiple times when after a compiler upgrade some of the standard headers are no longer implicitly inlcluded - and that is perfectly in align with The Standard.

@tormodvolden
Copy link
Contributor

@Youw That's why I find "Ubuntu MinGW 64" too unprecise. Anyway, my questions to @neheb remain.

Another thing, I am not sure the one in tests/libusb_testlib.h is useful.

@tormodvolden
Copy link
Contributor

tormodvolden commented Oct 17, 2023

And if it is needed in libusb/libusbi.h if ENABLE_LOGGING is not set?

I need to understand this better before I can approve the patch.

@neheb
Copy link
Contributor Author

neheb commented Oct 17, 2023

Caught me.

So the real motivation for this change is to try to fix the following warnings:

In file included from ../subprojects/libusb-1.0.26/libusb/core.c:23:
../subprojects/libusb-1.0.26/libusb/core.c: In function ‘libusb_wrap_sys_device’:
../subprojects/libusb-1.0.26/libusb/core.c:1249:16: warning: unknown conversion type character ‘l’ in format [-Wformat=]
 1249 |  usbi_dbg(ctx, "wrap_sys_device 0x%" PRIxPTR, (uintptr_t)sys_dev);
      |                ^~~~~~~~~~~~~~~~~~~~~
../subprojects/libusb-1.0.26/libusb/libusbi.h:315:67: note: in definition of macro ‘_usbi_log’
  315 | #define _usbi_log(ctx, level, ...) usbi_log(ctx, level, __func__, __VA_ARGS__)
      |                                                                   ^~~~~~~~~~~
../subprojects/libusb-1.0.26/libusb/core.c:1249:2: note: in expansion of macro ‘usbi_dbg’
 1249 |  usbi_dbg(ctx, "wrap_sys_device 0x%" PRIxPTR, (uintptr_t)sys_dev);
      |  ^~~~~~~~
../subprojects/libusb-1.0.26/libusb/core.c:1249:16: warning: too many arguments for format [-Wformat-extra-args]
 1249 |  usbi_dbg(ctx, "wrap_sys_device 0x%" PRIxPTR, (uintptr_t)sys_dev);
      |                ^~~~~~~~~~~~~~~~~~~~~
../subprojects/libusb-1.0.26/libusb/libusbi.h:315:67: note: in definition of macro ‘_usbi_log’
  315 | #define _usbi_log(ctx, level, ...) usbi_log(ctx, level, __func__, __VA_ARGS__)
      |                                                                   ^~~~~~~~~~~
../subprojects/libusb-1.0.26/libusb/core.c:1249:2: note: in expansion of macro ‘usbi_dbg’
 1249 |  usbi_dbg(ctx, "wrap_sys_device 0x%" PRIxPTR, (uintptr_t)sys_dev);
      |  ^~~~~~~~
../subprojects/libusb-1.0.26/libusb/core.c:1264:17: warning: unknown conversion type character ‘l’ in format [-Wformat=]
 1264 |   usbi_dbg(ctx, "wrap_sys_device 0x%" PRIxPTR " returns %d", (uintptr_t)sys_dev, r);
      |                 ^~~~~~~~~~~~~~~~~~~~~
../subprojects/libusb-1.0.26/libusb/libusbi.h:315:67: note: in definition of macro ‘_usbi_log’
  315 | #define _usbi_log(ctx, level, ...) usbi_log(ctx, level, __func__, __VA_ARGS__)
      |                                                                   ^~~~~~~~~~~
../subprojects/libusb-1.0.26/libusb/core.c:1264:3: note: in expansion of macro ‘usbi_dbg’
 1264 |   usbi_dbg(ctx, "wrap_sys_device 0x%" PRIxPTR " returns %d", (uintptr_t)sys_dev, r);
      |   ^~~~~~~~
../subprojects/libusb-1.0.26/libusb/core.c:1264:17: warning: format ‘%d’ expects argument of type ‘int’, but argument 5 has type ‘long long unsigned int’ [-Wformat=]
 1264 |   usbi_dbg(ctx, "wrap_sys_device 0x%" PRIxPTR " returns %d", (uintptr_t)sys_dev, r);
      |                 ^~~~~~~~~~~~~~~~~~~~~                        ~~~~~~~~~~~~~~~~~~
      |                                                              |
      |                                                              long long unsigned int
../subprojects/libusb-1.0.26/libusb/libusbi.h:315:67: note: in definition of macro ‘_usbi_log’
  315 | #define _usbi_log(ctx, level, ...) usbi_log(ctx, level, __func__, __VA_ARGS__)
      |                                                                   ^~~~~~~~~~~
../subprojects/libusb-1.0.26/libusb/core.c:1264:3: note: in expansion of macro ‘usbi_dbg’
 1264 |   usbi_dbg(ctx, "wrap_sys_device 0x%" PRIxPTR " returns %d", (uintptr_t)sys_dev, r);
      |   ^~~~~~~~
../subprojects/libusb-1.0.26/libusb/core.c:1264:17: warning: too many arguments for format [-Wformat-extra-args]
 1264 |   usbi_dbg(ctx, "wrap_sys_device 0x%" PRIxPTR " returns %d", (uintptr_t)sys_dev, r);
      |                 ^~~~~~~~~~~~~~~~~~~~~
../subprojects/libusb-1.0.26/libusb/libusbi.h:315:67: note: in definition of macro ‘_usbi_log’
  315 | #define _usbi_log(ctx, level, ...) usbi_log(ctx, level, __func__, __VA_ARGS__)
      |                                                                   ^~~~~~~~~~~
../subprojects/libusb-1.0.26/libusb/core.c:1264:3: note: in expansion of macro ‘usbi_dbg’
 1264 |   usbi_dbg(ctx, "wrap_sys_device 0x%" PRIxPTR " returns %d", (uintptr_t)sys_dev, r);
      |   ^~~~~~~~
[22/33] Compiling C object subprojects/libusb-1.0.26/libusb-1.0.dll.p/libusb_os_windows_winusb.c.obj
In file included from ../subprojects/libusb-1.0.26/libusb/os/windows_winusb.c:33:
../subprojects/libusb-1.0.26/libusb/os/windows_winusb.c: In function ‘_hid_get_report’:
../subprojects/libusb-1.0.26/libusb/os/windows_winusb.c:3594:30: warning: unknown conversion type character ‘l’ in format [-Wformat=]
 3594 |   usbi_warn(DEVICE_CTX(dev), "invalid size (%"PRIuPTR")", (uintptr_t)size);
      |                              ^~~~~~~~~~~~~~~~~
../subprojects/libusb-1.0.26/libusb/libusbi.h:315:67: note: in definition of macro ‘_usbi_log’
  315 | #define _usbi_log(ctx, level, ...) usbi_log(ctx, level, __func__, __VA_ARGS__)
      |                                                                   ^~~~~~~~~~~
../subprojects/libusb-1.0.26/libusb/os/windows_winusb.c:3594:3: note: in expansion of macro ‘usbi_warn’
 3594 |   usbi_warn(DEVICE_CTX(dev), "invalid size (%"PRIuPTR")", (uintptr_t)size);
      |   ^~~~~~~~~
../subprojects/libusb-1.0.26/libusb/os/windows_winusb.c:3594:30: warning: too many arguments for format [-Wformat-extra-args]
 3594 |   usbi_warn(DEVICE_CTX(dev), "invalid size (%"PRIuPTR")", (uintptr_t)size);
      |                              ^~~~~~~~~~~~~~~~~
../subprojects/libusb-1.0.26/libusb/libusbi.h:315:67: note: in definition of macro ‘_usbi_log’
  315 | #define _usbi_log(ctx, level, ...) usbi_log(ctx, level, __func__, __VA_ARGS__)
      |                                                                   ^~~~~~~~~~~
../subprojects/libusb-1.0.26/libusb/os/windows_winusb.c:3594:3: note: in expansion of macro ‘usbi_warn’
 3594 |   usbi_warn(DEVICE_CTX(dev), "invalid size (%"PRIuPTR")", (uintptr_t)size);
      |   ^~~~~~~~~
../subprojects/libusb-1.0.26/libusb/os/windows_winusb.c: In function ‘_hid_set_report’:
../subprojects/libusb-1.0.26/libusb/os/windows_winusb.c:3648:30: warning: unknown conversion type character ‘l’ in format [-Wformat=]
 3648 |   usbi_warn(DEVICE_CTX(dev), "invalid size (%"PRIuPTR")", (uintptr_t)size);
      |                              ^~~~~~~~~~~~~~~~~
../subprojects/libusb-1.0.26/libusb/libusbi.h:315:67: note: in definition of macro ‘_usbi_log’
  315 | #define _usbi_log(ctx, level, ...) usbi_log(ctx, level, __func__, __VA_ARGS__)
      |                                                                   ^~~~~~~~~~~
../subprojects/libusb-1.0.26/libusb/os/windows_winusb.c:3648:3: note: in expansion of macro ‘usbi_warn’
 3648 |   usbi_warn(DEVICE_CTX(dev), "invalid size (%"PRIuPTR")", (uintptr_t)size);
      |   ^~~~~~~~~
../subprojects/libusb-1.0.26/libusb/os/windows_winusb.c:3648:30: warning: too many arguments for format [-Wformat-extra-args]
 3648 |   usbi_warn(DEVICE_CTX(dev), "invalid size (%"PRIuPTR")", (uintptr_t)size);
      |                              ^~~~~~~~~~~~~~~~~
../subprojects/libusb-1.0.26/libusb/libusbi.h:315:67: note: in definition of macro ‘_usbi_log’
  315 | #define _usbi_log(ctx, level, ...) usbi_log(ctx, level, __func__, __VA_ARGS__)
      |                                                                   ^~~~~~~~~~~
../subprojects/libusb-1.0.26/libusb/os/windows_winusb.c:3648:3: note: in expansion of macro ‘usbi_warn’
 3648 |   usbi_warn(DEVICE_CTX(dev), "invalid size (%"PRIuPTR")", (uintptr_t)size);
      |   ^~~~~~~~~

The issue is that MinGW has a macro defined in stdio.h which switches between printf and gnu_printf. Example of how a different project handles this:

https://github.com/Exiv2/exiv2/blob/main/src/image_int.hpp#L15

This PR was meant as a first step to fixing that.

@tormodvolden
Copy link
Contributor

So if we do like exiv and conditionally use __MINGW_PRINTF_FORMAT the warnings go away?

Which version of gcc are you using? Do you use non-default configuration options or flags?

@neheb
Copy link
Contributor Author

neheb commented Oct 17, 2023

x86_64-w64-mingw32-g++ --version
x86_64-w64-mingw32-g++ (GCC) 10-posix 20220113

and yes. currently the printf attribute is set with configure.ac, which would have to be modified.

@neheb
Copy link
Contributor Author

neheb commented Oct 18, 2023

added configure.ac change thanks to ChatGPT. I don't really speak Autotools.

@neheb
Copy link
Contributor Author

neheb commented Oct 18, 2023

I have no idea why there are 4 different build systems here. Sounds horrible to maintain.

libusb/libusbi.h Outdated
@@ -30,6 +30,7 @@
#include <inttypes.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdio.h>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

with the chanes to configure.ac do we still need these?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any comments?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes this is still needed.

@tormodvolden
Copy link
Contributor

Because of the unclear copyright situation I am not comfortable with merging stuff coming straight out of ChatGPT or code copilot etc (as long as I know it, which I of course cannot unless people are frank about it) into a GPL project. To "clean room" design this (I haven't looked at the suggested change), can someone explain what needs to be done in configure.ac and I'll try to implement it and compare afterwards? Or someone who did look at it confirm it is 100% trivial and copyright safe.

@neheb
Copy link
Contributor Author

neheb commented Oct 19, 2023

It’s not a direct copy. It’s changed from the original output, as most things have to be with ChatGPT.

edit: anyway, check if __MINGW_PRINTF_FORMAT is defined and if it is, use it for PRINTF_FORMAT.

@tormodvolden
Copy link
Contributor

Such a test could be something along:

@@ -414,6 +414,13 @@ if test "x$backend" = xwindows; then
        CFLAGS="${saved_CFLAGS}"
 fi
 
+dnl MinGW being awkward here
+if test "x$backend" = xwindows; then
+       AC_CHECK_DECL([__MINGW_PRINTF_FORMAT],
+               [AC_DEFINE([PRINTF_FORMAT(a, b)], [__attribute__ ((__format__ (__MINGW_PRINTF_FORMAT, a, b)))], [Define to the attribute for enabling parameter checks on printf-like functions.])]
+               [], [[#include <stdio.h>]])
+fi
+
 SHARED_CFLAGS="-Wall -Wextra -Wshadow -Wunused -Wwrite-strings -Werror=format-security -Werror=implicit-function-declaration -Werror=implicit-int -Werror=init-self -Werror=missing-prototypes -Werror=strict-prototypes -Werror=undef -Werror=uninitialized"
 
 AM_CPPFLAGS="${EXTRA_CPPFLAGS}"

But I am not sure this is the correct way to deal with the issue, even if it would work in your case. The __MINGW_PRINTF_FORMAT macro is something internal to MinGW and not something we should have to deal with. If this is a needed workaround for a known bug in MinGW, it could be considered, but then we have to know more about this.

What is needed to reproduce your problem?

@neheb
Copy link
Contributor Author

neheb commented Nov 5, 2023

This macro is needed as libusb implements its own formatting functions based on printf. I posted the compilation warnings earlier.

@neheb
Copy link
Contributor Author

neheb commented Nov 21, 2023

anything else missing here?

@Youw
Copy link
Member

Youw commented Nov 21, 2023

#1327 (comment)

Using __MINGW_PRINTF_FORMAT, which is what the headers use.

add stdio.h header to get it defined.

Needed for printf functions. Not always included implicitly.

Signed-off-by: Rosen Penev <rosenp@gmail.com>
@tormodvolden
Copy link
Contributor

I think there is not enough explanation here. Please don't be afraid of being verbose and adding context, background and examples. I think you are using some special flags, but you have not mentioned which and why.

My remarks above remain. E.g. stdio.h is conditionally included in libusbi.h

Also, can this be related to https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95130 or https://github.com/msys2/MINGW-packages/blob/master/mingw-w64-gcc/0300-override-builtin-printf-format.patch ?

@neheb
Copy link
Contributor Author

neheb commented Nov 27, 2023

There are no special flags being passed. Context is that if -Wformat is passed, these warnings show up.

Those links seem unrelated.

@hjelmn
Copy link
Member

hjelmn commented Nov 28, 2023

Not comfortable including any ML-generated code in this project. The original source is unknown and it is possible it does not comply with our licensing or libusb may not comply with it. ChatGPT is less AI than it is a huge copyright infringement tool :p.

That aside, why are we using m4 to define this instead of just defining it in the header where it is used? That removes the need for any m4 changes.

@hjelmn
Copy link
Member

hjelmn commented Nov 28, 2023

Opened #1352 to remove the unnecessary m4. Should make it easier to write your patch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Misc Whitespace, typo, coding style, etc
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants