airspy / airspyhf

Code repository for AirspyHF+
BSD 3-Clause "New" or "Revised" License
147 stars 40 forks source link

Mixing 'cdecl' and 'stdcall' #39

Closed gvanem closed 2 years ago

gvanem commented 2 years ago

When building with clang-cl and for 32-bit (x86), there is this important warning:

libairspyHF/src/airspyhf.c(514,81): warning: cast between incompatible calling conventions 'cdecl' and 'stdcall'; calls through this pointer

      may abort at runtime [-Wcast-calling-convention]
  ...= prepare_transfers(device, LIBUSB_ENDPOINT_IN | AIRSPYHF_ENDPOINT_IN, (libusb_transfer_cb_fn)airspyhf_libusb_transfer_callback);
                                                                            ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
libairspyHF/src/airspyhf.c(407,13): note: consider defining 'airspyhf_libusb_transfer_callback' with the 'stdcall' calling convention
static void airspyhf_libusb_transfer_callback(struct libusb_transfer* usb_transfer)
            ^
            __RPC_CALLEE

No warning in 64-bit mode since stdcall == cdecl AFAIK. Ref: https://en.wikipedia.org/wiki/X86_calling_conventions#Microsoft_x64_calling_convention

But with this patch, there's no warning in 32-bit mode:

--- a/libairspyhf/src/airspyhf.c 2022-08-30 08:29:38
+++ b/libairspyhf/src/airspyhf.c 2022-08-30 09:39:44
@@ -404,7 +404,7 @@
        return NULL;
 }

-static void airspyhf_libusb_transfer_callback(struct libusb_transfer* usb_transfer)
+static void LIBUSB_CALL airspyhf_libusb_transfer_callback(struct libusb_transfer* usb_transfer)
 {
        airspyhf_complex_int16_t *temp;
        airspyhf_device_t* device = (airspyhf_device_t*) usb_transfer->user_data;
touil commented 2 years ago

Who in his right mind would set stdcall globally for a C compiler? I merged LIBUSB_CALL to avoid any confusion in libairspyhf, but something else will likely break elsewhere.

gvanem commented 2 years ago

Who in his right mind would set stdcall globally for a C compiler?

Not me. Look at the libusb header for the details: https://github.com/libusb/libusb/blob/master/libusb/libusb.h#L98

And:

  • The one consideration that you must apply in your software is to mark
  • all functions which you use as libusb callbacks with this LIBUSB_CALL
  • annotation, so that they too get compiled for the correct calling
    • convention.
touil commented 2 years ago

Whatever works.