C99 structure initialisers and Samba3

tridge at samba.org tridge at samba.org
Wed Aug 23 09:00:49 GMT 2006


 > I can see it with lots of the same functions or strings, but
 > that particular example?

I picked on that particular one as it happens to be the first one that
the ldb build hit on my Solaris box. For that particular structure in
that instance we could just re-order to match the declaration and
remove the name tags, but it does leave us with much less compile time
error checking. The structure has 8 function pointers with 1
prototype, and 4 functions with a 2nd prototype. With C99 you just
need to init the 5 members that are actually used, whereas with old
style initialisation you need to init them all (or possibly skip
trailing NULLs). 

So if someone doesn't get the number of NULL entries right in the old
style C declaration it will break things badly. It might not even fail
the test suite if (for example) you broke the start_transaction() or
end_transaction() member, as the result might be that we just don't
fsync() at the right time, so we'd only know of the bug after a power
failure :-)

In operational.c the ones that are initialised happen to be the first
5 entries, so you could use a pile of trailing NULLs, but that is just
luck really.

We've been bitten by real bugs with incorrect structure initialisation
in other places (see svn 17717 for example) so I'd prefer to move in
the direction of more checking instead of less.

Another really good candiate would be this one in loadparm.c:

 service sDefault = {
	True,			/* valid */
	NULL,			/* szService */
	NULL,			/* szPath */
	NULL,			/* szCopy */
	NULL,			/* szInclude */
        etc etc etc

It's around 130 lines of carefully ordered initialisation in
Samba3. That also hasn't been converted to C99 style in Samba4 yet,
but I think it should be. It would be so easy to get that one wrong,
and I wouldn't be at all confident our test suite would catch it (as
the test suite tends to test protocols functions, not configure

Cheers, Tridge

More information about the samba-technical mailing list