Re: [gtk-list] Re: Heisenberg was right?



On Wed, 10 Jun 1998, Rouat manu wrote:

> Allan Black wrote:
> 
> > 
> > Of all the types of bug that I've had to track down, the one that goes
> > away when you insert a print statement is the worst of all!
> 
> I know what you mean!
> 
> 
> > Now: the bad news :-(
> > 
> > There is a good (or bad?) chance that the piece of code which contains the
> > bug is nowhere near the code where the program crashes. What can happen is,
> > a corrupt pointer corrupts another pointer, which corrupts another pointer,
> > ...... eventually, you use one of the corrupted pointers and crash.
> 
> I this case I think it's not too bad - I've replaced and simplified
> the code that I suspect creates the trouble, and the bug apparently
> dissapeared - here is the code:
> 
> (from main:)
> 
>   char buffer[32];
>   sprintf (buffer, "%g", p.w);
>   gtk_entry_set_text (GTK_ENTRY (w_entry),buffer);
>   sprintf (buffer, "%g", p.l);
>   gtk_entry_set_text (GTK_ENTRY (l_entry),buffer);
> 
> this works - but the original code was:
> 
>   gtk_entry_set_text (GTK_ENTRY (w_entry),convert_to_char(p.w));  
>   gtk_entry_set_text (GTK_ENTRY (l_entry),convert_to_char(p.l)); 
> 
> and:
> 
> char *convert_to_char(double data)
> {
>   char *c;
>   static char buffer[10];

you allocate a ten byte buffer for this number, now look at:
"2,22222e+10"
this number has *11* chars, you are writing beyond the char array
bounds.
try 
static char buffer[64];
or something similar.

>   
>   sprintf (buffer, "%g", data);
>   if(data==N_A)
>     sprintf (buffer, "N/A"); 
>     c=buffer;
>     /* printf("buffer:%g\n",data); */
>     
>   return c;  
> }
> 
> It's the convert_to_char function which is screwy, obviously.
> 
> Any ideas to implement this differently?
> 
> (note: #define N_A 6e66 - bad idea I guess....Is there a reserved
> symbol or number in C to describe infinity or 'No result' or
> anything?)

math.h (or implicitely nan.h) defines the following type for N/A values:
#define NAN     (*(__const double *) __nan)
you might also find some architecture specific other defines
like _SNAN, _QNAN, _SNANF and _QNANF.


> 
> Regards,
> 
> 
> manu
> 
> 

---
ciaoTJ



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