Re: display integers in a clist



Don Dudley wrote:

A note in case you want to avoid programming bugs and safety
hazards, commonly around by the term "buffer overflows", for your
application:

*NEVER* use sprintf ()! Always use snprintf () instead!
[...]

Even better, use the glib function g_strdup_printf().  It mallocs the
space for your new string for you, and prevents overflow.  Make sure
you free your new string when you're done with it. :

gint num = 1;
gchar *word = "MEMORY";
gchar *newstr;

newstr = g_strdup_printf("%s %d", word, num);

See:
http://developer.gnome.org/doc/API/2.0/glib/glib-String-Utility-Functions.html

Sure. However, one should also consider a small performance penalty due
to additional implicit strlen () and malloc () operations (and
subsequent explicit g_free ()).

And since memory leaks are likely even more common than buffer overflow
vulnerabilities (though less dangerous) a good amount of extra care
needs to be taken as well if g_strdup_printf () is to be used
frequently. Remember, in C and GTK+ there isn't an automatic garbage
collection (as for instance in Java).

Besides, there's also a g_snprintf () function, which provides snprintf
() in a GTK+ compatible way. However, since g_snprintf () internally
makes use of vsnprintf () (of the standard libc) I suppose g_snprintf ()
is available only if snprintf () is available as well. In other words:
apparently you don't lose any amount of compatibility if you use
snprintf () instead of g_snprintf ().



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]