After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 167569 - g_string_append_printf crashes on win32 when used with a NULL argument
g_string_append_printf crashes on win32 when used with a NULL argument
Status: RESOLVED FIXED
Product: glib
Classification: Platform
Component: general
2.4.x
Other All
: Normal normal
: ---
Assigned To: gtkdev
gtkdev
: 458457 (view as bug list)
Depends on:
Blocks:
 
 
Reported: 2005-02-16 08:29 UTC by Cedric Gustin
Modified: 2009-11-04 11:14 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
Check for NULL %s in vasprintf.c (1.31 KB, patch)
2007-01-30 15:25 UTC, Owen Taylor
none Details | Review

Description Cedric Gustin 2005-02-16 08:29:57 UTC
Build and run the following piece of code 

----------
#include <glib.h>
#include <stdio.h>

int main(int argc, char *argv[]) {
   GString *string=g_string_new("");
   g_string_append_printf(string,"%s",NULL);
   printf(string->str);

   return 0;
}
----------

I tested this against glib 2.4.8 and glib 2.6.1 (from
http://gladewin32.sourceforge.net) on win32 (winxp sp2), using both mingw32-gcc
3.4.2 and MSVC.Net 2003 (7.1). 

The same program does not crash on linux (2.6.10, fedora core 3), with glib 2.4.8.

I understand that passing a NULL pointer as third argument is a bad idea, but
maybe some kind of g_return_val_if_fail(arg != NULL, NULL) would be nice in
g_string_append_printf.
Comment 1 Tor Lillqvist 2005-02-16 08:52:16 UTC
Surely this is not a bug. Don't expect printf() and its likes to be able to
print any random garbage for the %s format. NULL is not a valid pointer to an
array of chars.

That it doesn't crash on Linux is purely coincidental. One might even say that
it is a bug that it doesn't crash on Linux. (I don't see any mention of the
"(null)" output for NULL pointers in the printf(3) manpage. I wouldn't rely on
this "feature".) If a buggy program happens to work in one way on one system in
one implementation-defined way one cannot expect it to work identically on
another system.

If we "fix" this, what next? Somebody might as well ask "why doesn't
g_string_append_printf(string, "%s", 42) work on Linux? It works on FooBarIx
3.0."...
Comment 2 Owen Taylor 2007-01-30 15:24:55 UTC
I used to agree 100% with Tor on this one, for a number reasons

 - Supporting NULL in printf is not required by any standard
 - We can't fix uses of printf(), only of the g_log(), g_debug(),
   g_strdup_printf(), etc.
 - I don't think we we should be using the gnulib printf wrapper
   *just* to fix this issue, if a printf (like the Solaris printf)
   is fine for us for every other reason.

But I've had a change of heart on the matter ... if we *are* using the
gnulib printf wrapper (as we do on Win32), it's just silly to sit
there and crash an application for some g_debug() statement when
a few line patch would avoid it.

I'll attach such a patch. (Probably should be sent upstream to 
gnulib if we decide to go ahead and apply it.)
Comment 3 Owen Taylor 2007-01-30 15:25:59 UTC
Created attachment 81516 [details] [review]
Check for NULL %s in vasprintf.c
Comment 4 Tor Lillqvist 2007-01-30 20:14:14 UTC
OK, I trust Owen on this, so I'm all for it. As long as we then also have a configure test to make sure we use the gnulib printf also on platforms where the system printf would be otherwise good enough but doesn't like NULLs.
Comment 5 Matthias Clasen 2007-01-30 20:24:50 UTC
I understood Owens comment to say "fix gnulib, but don't switch to using gnulib just for this"
Comment 6 Tor Lillqvist 2007-01-30 21:27:07 UTC
Hmm. OK... Although I feel sorry for the people who then are going to wonder why their code crashes on Solaris if it works fine on Linux and Win32.
Comment 7 Behdad Esfahbod 2007-01-30 23:01:54 UTC
(In reply to comment #6)
> Hmm. OK... Although I feel sorry for the people who then are going to wonder
> why their code crashes on Solaris if it works fine on Linux and Win32.

Agreed.  Why not force the included printf in debugging mode and g_critical on things that are not portable?
Comment 8 Owen Taylor 2007-01-31 15:33:17 UTC
mclasen> I understood Owens comment to say "fix gnulib, but don't switch to 
mclasen> using gnulib just for this"

Yes, that's what I meant.

tml> Hmm. OK... Although I feel sorry for the people who then are going to 
tml> wonder why their code crashes on Solaris if it works fine on Linux and
tml> Win32.

True. Maybe we should just put in the test Either way, there would be pressure 
on Sun to improve their printf :-) Solaris is probably the only significant
example of a printf that is good enough otherwise but doesn't catch null.

The test should be simple enough to write, , though leaving core files from
a configure run is a just a little bit messy...

behdad> Agreed.  Why not force the included printf in debugging mode and 
behdad> g_critical on things that are not portable?

The trouble is that the most common case of hitting crashes like this is in rare 
debug paths, or in places that are g_critical() already. After all, few programs
are *typically*  printing out "(null)" all the time. So, I don't think a g_critical() is going to catch a lot of the cases out there. And I must admit 
that I like the ability when inserting temporary debugging prints on Linux
to not have to foo ? foo : "(null)"...

Comment 9 Behdad Esfahbod 2007-02-03 16:42:08 UTC
(In reply to comment #8)
> behdad> Agreed.  Why not force the included printf in debugging mode and 
> behdad> g_critical on things that are not portable?
> 
> The trouble is that the most common case of hitting crashes like this is in
> rare 
> debug paths, or in places that are g_critical() already.

Then don't do it for g_warning/error/critical/message/...().  Just for g_print*, g_string_*, ...  The real user APIs, not debugging ones.

> After all, few
> programs
> are *typically*  printing out "(null)" all the time.

Well, nm-applet was doing that before it actually connected to any network and that was crashing on me.

> So, I don't think a
> g_critical() is going to catch a lot of the cases out there. And I must admit 
> that I like the ability when inserting temporary debugging prints on Linux
> to not have to foo ? foo : "(null)"...

You can do that as long as you use printf or g_message/...

Comment 10 Michel Boaventura 2009-02-27 05:13:36 UTC
Hello guys. On glib 2.19 this issue still exists.
This patch will be applied to gnulib?
Thanks.
Comment 11 Tor Lillqvist 2009-02-27 07:48:42 UTC
All right then.

Patch applied to trunk:

2009-02-27  Tor Lillqvist  <tml@novell.com>

	Bug 167569 - g_string_append_printf crashes on win32 when used
	with a NULL argument

	* glib/gnulib/vasnprintf.c (vasnprintf): Add workaround for buggy
	programs. Patch by Owen.
Comment 12 Michel Boaventura 2009-02-27 11:57:35 UTC
Thank you very much Tor.
Comment 13 Tor Lillqvist 2009-11-04 11:14:25 UTC
*** Bug 458457 has been marked as a duplicate of this bug. ***