Re: g_value_peek_pointer weirdness



Tim Janik <timj gtk org> wrote:
> On Mon, 15 Dec 2003, Sheldon Simms wrote:
> >
> > 178   if (!value_table->value_peek_pointer)
> > 179     g_return_val_if_fail (g_value_fits_pointer
(value) == TRUE, NULL);
> > 180
> > 181   return
value_table->value_peek_pointer(value)
>
> g_value_peek_pointer() is supposed to only be called
> for values for which g_value_fits_pointer() returns
> TRUE.

Ok, I missed the point that g_return_val_if_fail()
prints a warning
message if checks are enabled.

> > But in that case, why not just do this:
> >
> > 178   if (!value_table->value_peek_pointer)
> > 179     return NULL;
> > 180
> > 181   return
value_table->value_peek_pointer(value);
> 
> because that wouldn't point out that
g_value_peek_pointer()
> was erroneously used.

Ok, but isn't the code weird in any case? Clearly if
line 179 somehow
fails to return, even though
value_table->value_peek_pointer == NULL,
then line 181 would cause a segfault. In fact,
glib/gmessages.h contains

 #ifdef G_DISABLE_CHECKS

 #define g_return_if_fail(expr)		G_STMT_START{
(void)0; }G_STMT_END
 #define g_return_val_if_fail(expr,val)	G_STMT_START{
(void)0; }G_STMT_END
 #define g_return_if_reached()		G_STMT_START{ return;
}G_STMT_END
 #define g_return_val_if_reached(val)	G_STMT_START{
return (val); }G_STMT_END

 #else /* !G_DISABLE_CHECKS */

So, I guess no one disables checks and then calls
g_value_peek_pointer() with
a bad value.

It seems to me that it would be correct to use
g_assert() here:

  if (!value_table->value_peek_pointer)
    {
      g_assert(g_value_fits_pointer(value) == TRUE);
      return NULL;
    }

  return value_table->value_peek_pointer(value);

-Sheldon


__________________________________
Do you Yahoo!?
Protect your identity with Yahoo! Mail AddressGuard
http://antispam.yahoo.com/whatsnewfree



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