Summary: cmdhelp_new() modifies const parameter on some
                 Project: Freeciv
            Submitted by: cazfi
            Submitted on: Sun 05 Feb 2012 02:46:37 AM EET
                Category: general
                Severity: 3 - Normal
                Priority: 5 - Normal
                  Status: None
             Assigned to: None
        Originator Email: 
             Open/Closed: Open
         Discussion Lock: Any
        Operating System: None
         Planned Release: 



I got warning that at first seemed only a missing #include when compiling for
Windows. After some thinking, I'm quite convinced that this is more serious
than that.

GNU basename() takes const parameter, POSIX one does not. cmdhelp_new() passes
const parameter to basename(). Everything is fine as long as GNU basename() is
being used, but bad things happen if not.
One gets GNU basename on systems supporting it by *not* including libgen.h
that has POSIX version. But simply omitting libgen.h include (as we do at the
moment) results prototype not to be included at all in systems without GNU
basename(). Then two C features come to play. First is that in lack of
explicit prototype (stating that parameter is *not* const in current platform)
implicit one is used, and from the fact that caller passes const parameter,
implicit prototype is constructed with const parameter. Now we have const
parameter in compile time, and non-const only in library to be linked against
(linking time). Second C (unlike C++) feature is that symbol names do not
contain information about parameter types, or even their count. To linker
"basename(const char *)" and "basename(char *)" are both just "basename()",
and so it happily links our 'const char *' parameter against library's 'char
*' function without any warnings.

Easy solution would be to always use POSIX version, but I'm still trying to
construct fc_basename() that would pass its const parameter as is to GNU
basename if available, and do more complicated things itself if available
baseline() modifies its parameters. Portable (not gcc-specific) configure
checks catering for all the cases seems to be quite hairy to do, though.


Reply to this item at:


  Message sent via/by Gna!

Freeciv-dev mailing list

Reply via email to