g_snprintf() implementation




Hi,

g_snprintf() has two possible implementations, one which uses vsnprintf()
and one which doesn't. Only the one which doesn't does this:
str[n-1] = '\0';

According to the man page, vsnprintf() does *not* do this, since it
behaves just like regular snprintf(). The other problem is that
vsnprintf() potentially returns -1, but the other implementation does not.

I think the vsnprintf() version should be changed to null-terminate and
never return -1.
Am I missing something?

Havoc

Here's that code...

gint
g_snprintf (gchar       *str,
            gulong       n,
            gchar const *fmt,
            ...)
{
#ifdef  HAVE_VSNPRINTF
  va_list args;
  gint retval;
  
  va_start (args, fmt);
  retval = vsnprintf (str, n, fmt, args);
  va_end (args);
  
  return retval;
#else   /* !HAVE_VSNPRINTF */
  gchar *printed;
  va_list args;
  
  va_start (args, fmt);
  printed = g_strdup_vprintf (fmt, args);
  va_end (args);
  
  strncpy (str, printed, n);
  str[n-1] = '\0';

  g_free (printed);
  
  return strlen (str);
#endif  /* !HAVE_VSNPRINTF */
}




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