GNOME Bugzilla – Bug 74336
iconv() with null arguments segfaults on sunos
Last modified: 2004-12-22 21:47:04 UTC
I was just trying out glib 2.0.0. It compiled fine on SunOS, but three of the tests failed, namely testgdate, spawn-test and unicode-encoding, all of which segfaulted. It turned out that in all cases the reason was the same:
+ Trace 19015
I tried out the system iconv just by itself, and indeed, calling it with four null arguments always segfaults. I'm not sure whether this is properly a bug in Sun's iconv implementation, since at a quick glance I don't see Unix98 specifying the behavior with all null arguments, so it might be just an extension in GNU libiconv. In any case, glib probably ought to have a workaround.
From Unix98: For state-dependent encodings, the conversion descriptor cd is placed into its initial shift state by a call for which inbuf is a null pointer, or for which inbuf points to a null pointer. When iconv() is called in this way, and if outbuf is not a null pointer or a pointer to a null pointer, and outbytesleft points to a positive value, iconv() will place, into the output buffer, the byte sequence to change the output buffer to its initial shift state. If the output buffer is not large enough to hold the entire reset sequence, iconv() will fail and set errno to [E2BIG]. Subsequent calls with inbuf as other than a null pointer or a pointer to a null pointer cause the conversion to take place from the current state of the conversion descriptor. So, inbuf and outbuf being NULL is 100% specified behavior. Does Solaris iconv behave correctly if you pass in non-NULL for inbytesleft / outbytesleft?
It seems to segfault if anything besides inbuf is NULL. Outbuf, on the other hand, may _point_ to NULL, but may not _be_ NULL. Yes, I read the same paragraph from Unix98, and I noted that it specifically only states what happens "if outbuf is not a null pointer or a pointer to a null pointer". But I do believe that the intention is that a null pointer is also allowed. Then again, it's hardly a wonder if this Sun iconv is not Unix98 compliant, since: /* * Copyright (c) 1993 by Sun Microsystems, Inc. */ #ifndef _ICONV_H #define _ICONV_H #pragma ident "@(#)iconv.h 1.2 94/01/21 SMI" Even the function declaration is wrong: extern size_t iconv(iconv_t, const char **, size_t *, char **, size_t *); (Since const char** is not compatible with char**) Anyway. This is probably just a problem with a legacy platform, but it would be nice if glib worked out of the box here, too. On the other hand, it took me less effort to install gnu libiconv than to file this report, so this is not _that_ bad a showstopper. :)
I should add that on a Solaris 8 system that I tried, iconv() worked fine with NULL arguments. So the issue seems to be Solaris 7 -specific.
Wed Mar 20 18:20:21 2002 Owen Taylor <otaylor@redhat.com> * glib/gconvert.c (open_converter): Try to work around segfaults on Solaris if NULL is passed for outbuf... supposedly gchar *outbuf = NULL... &outbuf works. (#74336, Lauri Alanko)