Bug 196567

Summary: iconv() function crashes with cryptic error message if linked statically
Product: Base System Reporter: devnull <ACCOUNT_DELETED>
Component: kernAssignee: freebsd-bugs (Nobody) <bugs>
Status: New ---    
Severity: Affects Some People CC: satanist+freebsd
Priority: ---    
Version: 10.1-RELEASE   
Hardware: Any   
OS: Any   
Attachments:
Description Flags
add bug information to iconv manpage none

Description devnull freebsd_committer freebsd_triage 2015-01-06 18:13:15 UTC
Hi everyone,

I’ve been trying to link a program statically that calls the iconv(3) function and was confronted with it printing the rather cryptic message "Service unavailable" and immediately exiting the program. After some websearch, I found this thread on the freebsd-current mailinglist:

* https://docs.freebsd.org/cgi/getmsg.cgi?fetch=140017+0+archive/2013/freebsd-current/20130825.freebsd-current
* https://docs.freebsd.org/cgi/getmsg.cgi?fetch=177755+0+archive/2013/freebsd-current/20130825.freebsd-current
* https://docs.freebsd.org/cgi/getmsg.cgi?fetch=195390+0+archive/2013/freebsd-current/20130825.freebsd-current

It appears nothing has happened since that thread, so I add it to the tracker now so it won’t get forgotten and because I just experienced that issue as well.

Here’s the minimal example program from that ML thread to reproduce the crash:

~~~~~~~~~~~~~~~~~~~~~~
int main(void)
{
        iconv_t ic = iconv_open("UTF-8", "ISO-8859-1");
        if (ic == (iconv_t)-1)
                err(1, "iconv_open failed");
        iconv_close(ic);
        return 0;
}
~~~~~~~~~~~~~~~~~~~~~~

Save in `foo.c` and compile like this:

~~~~~~~~~~~~~~~~~~~~~~
$ cc -static foo.c -o foo
~~~~~~~~~~~~~~~~~~~~~~

Then run:

~~~~~~~~~~~~~~~~~~~~~~
$ ./foo
foo: iconv_open failed: Invalid argument
Service unavailable
~~~~~~~~~~~~~~~~~~~~~~

I can confirm this behaviour stil exists in FreeBSD 10.1-RELEASE. The above example program still "works" (i.e. fails and demonstrates the problem).

Expected behaviour is to print a useful and understandable error message if it really isn’t possible to get iconv() to work in a statically linked program.

Greetings,
Marvin
Comment 1 satanist+freebsd 2015-09-26 11:12:34 UTC
Created attachment 161413 [details]
add bug information to iconv manpage
Comment 2 satanist+freebsd 2015-09-26 11:18:37 UTC
Is somebody on this bug? It's annoying to debug.

Some workaround ideas:
1) let linking fail
2) don't write to stdout, some programms use this to communicate.
3) make the error message less cryptic. for example "dynamic linking Service unavailable"
4) add information to the manpage