bug 764: Initialize the right member of union option_value
commite5f6592ee20780a61f70feeb1f9e17631b9c5835
authorKalle Olavi Niemitalo <kon@iki.fi>
Sat, 15 Aug 2009 19:39:07 +0000 (15 22:39 +0300)
committerKalle Olavi Niemitalo <Kalle@Astalo.kon.iki.fi>
Tue, 18 Aug 2009 02:02:16 +0000 (18 05:02 +0300)
treea0f4fa7caad28cea629df473887ab6ad66bfaa47
parent70ef4c8fddec397c29d267133b61db8a8604d305
bug 764: Initialize the right member of union option_value

INIT_OPTION used to initialize union option_value at compile time by
casting the default value to LIST_OF(struct option) *, which is the
type of the first member.  On sparc64 and other big-endian systems
where sizeof(int) < sizeof(struct list_head *), this tended to leave
option->value.number as zero, thus messing up OPT_INT and OPT_BOOL
at least.  OPT_LONG however tended to work right.

This would be easy to fix with C99 designated initializers,
but doc/hacking.txt says ELinks must be kept C89 compatible.
Another solution would be to make register_options() read the
value from option->value.tree (the first member), cast it back
to the right type, and write it to the appropriate member;
but that would still require somewhat dubious conversions
between integers, data pointers, and function pointers.

So here's a rather more invasive solution.  Add struct option_init,
which is somewhat similar to struct option but has non-overlapping
members for different types of values, to ensure nothing is lost
in compile-time conversions.  Move unsigned char *path from struct
option_info to struct option_init, and replace struct option_info
with a union that contains struct option_init and struct option.
Now, this union can be initialized with no portability problems,
and register_options() then moves the values from struct option_init
to their final places in struct option.

In my x86 ELinks build with plenty of options configured in, this
change bloated the text section by 340 bytes but compressed the data
section by 2784 bytes, presumably because union option_info is a
pointer smaller than struct option_info was.
29 files changed:
NEWS
src/bfu/leds.c
src/bookmarks/bookmarks.c
src/config/cmdline.c
src/config/options.c
src/config/options.h
src/config/options.inc
src/cookies/cookies.c
src/document/css/css.c
src/ecmascript/ecmascript.c
src/formhist/formhist.c
src/globhist/globhist.c
src/main/module.h
src/mime/backend/default.c
src/mime/backend/mailcap.c
src/mime/backend/mimetypes.c
src/mime/mime.c
src/network/ssl/ssl.c
src/protocol/bittorrent/bittorrent.c
src/protocol/file/cgi.c
src/protocol/file/file.c
src/protocol/fsp/fsp.c
src/protocol/ftp/ftp.c
src/protocol/http/http.c
src/protocol/nntp/nntp.c
src/protocol/protocol.c
src/protocol/rewrite/rewrite.c
src/protocol/smb/smb2.c
src/protocol/user.c