Re: Bug in g_message() with long lines ?




Guy Slater <guy@ebi.ac.uk> writes:

> Hi,
> 
> I'm just starting to use glib (nice library - I wish I'd known
> about it a long time ago).
> 
> I think there may be a bug in glib when g_message() is used
> to report long strings.  (Strings here are often DNA,
> so can be very long).
> 
> This one had me puzzled for a while as I was using g_message()
> to print debugging info.  The problem also seems to be a bit platform
> specific.

Well, what you are basically discovering is that the g_log()
functions are limited to a fixed size buffer. The reason for
this is that they may be called in out-of-memory situations,
so can't rely on allocating memory.

I think this probably could be addressed in a couple of ways:

 1) Allow memory to be allocated for non-fatal messages, and
    only use fixed size buffers for fatal messages.

 2) Try allocating the necessary memory with malloc(), and
    only if that fails fall back to the static buffer.

(Printing out long DNA sequences in user messages seems a little
odd to me, so I'll mention that g_print(), g_message(), etc,
really aren't meant as replacements for printf() - they are
just meant for redirectable logging of errors and warnings.)
 
> This bit of code gives an example:
> 
> /* --- START EXAMPLE --- */
> #include <glib.h>
> 
> int main(){
>     gchar str[2001];
>     guint i;
>     for(i = 0; i < 2000; i++)
>         str[i] = 'X';
>     str[2000] = '\0';
>     g_message("str is [%s]", str);
>     return 0;
>     }
> /* --- END EXAMPLE --- */
> 
> On Linux, using glib 1.2.6, the output
> is truncated after reporting 1015 chars:
> 
> linux % ./example
> Message: str is [XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX!
!
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
> linux %
> 
> 
> On an alpha, using glib 1.2.7, it just reports this:
> alpha % ./example
> Message: str is [%s]
> alpha %
> 
> I think this may be something to do with the bit in g_logv() in gmessages.c
> with the comment, /* we are out of luck here */, but I am not really sure
> what is going on in there, so haven't attempted a patch.

"alpha" isn't very descriptive. Linux? True64? 

But, yes, basically, the thing is that if we don't have vnsprintf()
then we can't print only the first 1024 characters of the string,
because the fallback g_snprintf() needs to allocate memory, so
we just print out the format.

Regards,
                                        Owen



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