RE: glib's g_logv 1024 byte maximum character array



Am Don, 2003-07-24 um 10.22 schrieb martyn 2 russell bt com:
> > it's not a bug. if you actually take a look at gmessage.c, it says:
> > 
> >   /* we use a stack buffer of fixed size, because we might get called
> >    * recursively.
> >    */
> >   _g_vsnprintf (buffer, 1024, format, args1);
> > 
> 
> Yes I did take a look at this, and yes, I know that is why there is a limit
> (to protect against recursion), BUT!! The bug is that when a string has
> NOTHING but multibyte characters, at some point, you are bound to have
> invalid UTF8 string if you ALWAYS cut of at the 1024th byte.  Checking for
> multibyte characters first is needed.
> 
> Yes, there has to be a limit to the buffer which is used, but, for something
> as well used as the g_log() functions, it would be better if there was no
> limit similar to the way an unlimited string arguments can be used with
> g_strdup_printf() as you quite rightly pointed out.
> 
> I do not know the mechanics of the g_log() functions or implications if
> g_strdup_printf() could be utilised (this is a good suggestion though), :)
> perhaps that would solve the problem? 
> 

Here is a patch to remove the string length limitation in the
non-recursive case. Ok to commit, Tim ?


Index: glib/gmessages.c
===================================================================
RCS file: /cvs/gnome/glib/glib/gmessages.c,v
retrieving revision 1.55
diff -u -p -r1.55 gmessages.c
--- glib/gmessages.c	30 Mar 2003 22:02:19 -0000	1.55
+++ glib/gmessages.c	25 Jul 2003 21:00:31 -0000
@@ -422,7 +422,6 @@ g_logv (const gchar   *log_domain,
 	const gchar   *format,
 	va_list	       args1)
 {
-  gchar buffer[1025];
   gboolean was_fatal = (log_level & G_LOG_FLAG_FATAL) != 0;
   gboolean was_recursion = (log_level & G_LOG_FLAG_RECURSION) != 0;
   gint i;
@@ -431,11 +430,6 @@ g_logv (const gchar   *log_domain,
   if (!log_level)
     return;
   
-  /* we use a stack buffer of fixed size, because we might get called
-   * recursively.
-   */
-  _g_vsnprintf (buffer, 1024, format, args1);
-  
   for (i = g_bit_nth_msf (log_level, -1); i >= 0; i = g_bit_nth_msf (log_level, i))
     {
       register GLogLevelFlags test_level;
@@ -491,7 +485,25 @@ g_logv (const gchar   *log_domain,
 		}
 	    }
 
-	  log_func (log_domain, test_level, buffer, data);
+	  if (test_level & G_LOG_FLAG_RECURSION)
+	    {
+	      /* we use a stack buffer of fixed size, since we're likely
+	       * in an out-of-memory situation
+	       */
+	      gchar buffer[1025];
+	      gint size;
+	      size = _g_vsnprintf (buffer, 1024, format, args1);
+
+	      log_func (log_domain, test_level, buffer, data);
+	    }
+	  else
+	    {
+	      gchar *msg = g_strdup_vprintf (format, args1);
+
+	      log_func (log_domain, test_level, msg, data);
+
+	      g_free (msg);
+	    }
 
 	  if (test_level & G_LOG_FLAG_FATAL)
 	    {


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