Zero-sized arrays

82 views
Skip to first unread message

Scott Graham

unread,
Feb 3, 2015, 8:32:26 PM2/3/15
to crashp...@chromium.org
Hi crashpad-ians,

minidump/ contains a bunch of structures that are defined with a trailing zero-sized array.

On Windows/MSVC there's 3 problems that I've encountered with this so far from least bothersome to most bothersome. Soliciting suggestions (sorry for the long mail):


1. There's a warning emitted that the copy operators won't be correct, and it can't be disabled on the command line (i.e. build/common.gypi). This appears to be a compiler bug. https://connect.microsoft.com/VisualStudio/feedback/details/1114440 . The pragma equivalent works though, so just requires ugly additions to the code, not a huge problem.


2. The zero length array cannot appear in other than last position, e.g:

d:\src\x>type x.cc
#pragma warning(disable: 4200)
struct X {
  int wee[0];
  int y;
};

d:\src\x>cl /nologo /c /W4 x.cc
x.cc
x.cc(4) : error C2229: struct 'X' has an illegal zero-sized array

This pattern occurs in minidump in a bunch of the Writer classes. Note that the error is emitted for the "int y;" line, which hints at the problem. It feels that array[0] have an invalid size, not zero size, so it can calculate the offset for subsequent elements.

These can be avoided by reordering members. e.g. in 
MinidumpLocationDescriptorListWriter, the "MinidumpLocationDescriptorList location_descriptor_list_base_" member can be put in the last position and then it doesn't complain. This doesn't seem too bad.


3. Base classes cannot contain zero-sized arrays.

d:\src\x>type x.cc
#pragma warning(disable: 4200)
struct X {
  int y;
  int wee[0];
};

struct Z final : public X {
};

d:\src\x>cl /nologo /c /W4 x.cc
x.cc
x.cc(7) : error C2503: 'X' : base classes cannot contain zero-sized arrays

Note that this happens even when there's no members in the derived class. This comes up in minidump_string_writer.h and others.

I'm not sure what the best solution to this is. As-yet untested possibilities:
- remove inheritance in these cases and contain the base object with manual forwarding methods. [[ I didn't investigate how frequently the derived objects are stored into base class pointers ]]
- convert base classes to holding a scoped_ptr of the object with a zero-sized array. [[ This might reduce the benefit of having the object be contained, or otherwise be a bit awkward. ]]

Any suggestions or preferences? Otherwise I'll just poke at individual cases.

Scott Graham

unread,
Feb 4, 2015, 12:55:35 AM2/4/15
to crashp...@chromium.org
A mix of moving arrays to the end (when not used in any inherited objects), and holding the members in scoped_ptrs (when they are) seems like the simplest solution.

Mark Mentovai

unread,
Feb 4, 2015, 11:26:35 AM2/4/15
to Scott Graham, crashp...@chromium.org
I don’t like the zero-length arrays, but dbghelp.h uses them and it was pretty nice to be able to use dbghelp.h on Windows and our own copy with identical declarations on non-Windows. Once that was done, it seemed right to define the non-dbghelp.h structures to be used in minidumps with the same semantics.

I guess the #pragma is fine for (1) and the reordering is certainly fine for (2), and the scoped_ptr doesn’t seem so bad for (3).

--
You received this message because you are subscribed to the Google Groups "Crashpad-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to crashpad-dev...@chromium.org.
To post to this group, send email to crashp...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/crashpad-dev/CANHK6Rby5zZpvaWty1uTs4r_Acny2CYhcFHJtb18imypiWr0nQ%40mail.gmail.com.

Reply all
Reply to author
Forward
0 new messages