Ticket #2 (assigned defect)
Opened 4 years ago
Last modified 3 weeks ago
ISO C++ forbids zero-size array 'iso_packet_desc'
| Reported by: | dsd | Owned by: | hjelmn |
|---|---|---|---|
| Milestone: | libusb/libusbx 1.2.0 | Component: | libusb-1.0 |
| Keywords: | Cc: | ||
| Blocked By: | Blocks: |
Description
from: https://sourceforge.net/tracker/?func=detail&aid=2740374&group_id=1674&atid=101674
libusb.h causes errors in C++ projects that use GCC's -pedantic switch:
error: ISO C++ forbids zero-size array 'iso_packet_desc'
One solution is to make the array size 1 and account for that change in all places that use sizeof(struct libusb_transfer), e.g.: int alloc_size = sizeof(struct usbi_transfer) + sizeof(struct libusb_transfer) + (sizeof(struct libusb_iso_packet_descriptor) * (iso_packets-1)) + os_alloc_size;
OK. Then I have a possible solution which I don't completely dislike: use an #ifdef to make the array size 1 when using a C++ compiler, but leave the other situations as-is. Outside of the header file, no libusb changes are needed since libusb will always be compiled with a C compiler. What do you think?
Change History
comment:1 Changed 2 years ago by xiaofan
comment:2 Changed 23 months ago by hansdegoede
Using 0 sized arrays at the end of a struct for a variable sized array is quite a normal thing to-do now a days in plain C.
Since this is only a warning and only happens with -pedantic, and likely only with older gcc versions I suggest we just close this one. Also note that the proposed fix would be an ABI change which is a no no.
comment:3 Changed 3 weeks ago by hjelmn
I agree with Hans. I am setting a timeout of a week then I will close this ticket as wontfix. If there is any interest in fixing this (though I don't think this is a defect) let me know.
comment:4 follow-up: ↓ 5 Changed 3 weeks ago by LudovicRousseau
Two possible fix:
https://github.com/mmuman/contiki/commit/1d900be802e20ceda323bc44b753a449853f4278
https://github.com/LudovicRousseau/libusbx/commit/53134e90f2f8fc516cc9794d79e754703da9e894
I prefer the first implementation.
comment:5 in reply to: ↑ 4 Changed 3 weeks ago by hjelmn
- Milestone set to 1.0.16
- Owner set to hjelmn
- Status changed from new to assigned
Ugh, neither option is very clean. I agree the first implementation is better. I will adapt it for libusb and push it to a branch on my github tree.
Since this doesn't break API compatibility I will go ahead and mark this for 1.0.16.
comment:6 Changed 3 weeks ago by hjelmn
Hmm, we already handle this in the same way as https://github.com/LudovicRousseau/libusbx/commit/53134e90f2f8fc516cc9794d79e754703da9e894 in libusb.h so there is no reason to not do it the same way for libusbi.h. I will just pull the patch from your tree.
Just wondering how to reproduce this one and wonder if this is valid or not.
mcuee@ubuntu:~/Desktop/build/libusb1/test$ g++ -pedantic test.cpp -o test -lusb-1.0
mcuee@ubuntu:~/Desktop/build/libusb1/test$ cat test.cpp
#include <libusb-1.0/libusb.h>
#include <iostream>
using namespace std;
int main()
{
using libusb.h
";
}
mcuee@ubuntu:~/Desktop/build/libusb1/test$ g++ -pedantic test.cpp -o test -lusb-1.0
mcuee@ubuntu:~/Desktop/build/libusb1/test$ gcc -v
Using built-in specs.
Target: i486-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 4.4.3-4ubuntu5' --with-bugurl=file:///usr/share/doc/gcc-4.4/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --enable-shared --enable-multiarch --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.4 --program-suffix=-4.4 --enable-nls --enable-clocale=gnu --enable-libstdcxx-debug --enable-plugin --enable-objc-gc --enable-targets=all --disable-werror --with-arch-32=i486 --with-tune=generic --enable-checking=release --build=i486-linux-gnu --host=i486-linux-gnu --target=i486-linux-gnu
Thread model: posix
gcc version 4.4.3 (Ubuntu 4.4.3-4ubuntu5)