[cairo] On allowing NULL font options

Alp Toker alp at atoker.com
Thu Jan 17 19:29:11 PST 2008

Behdad Esfahbod wrote:
> On Thu, 2008-01-17 at 17:39 -0500, Chris Wilson wrote:
>>     [cairo-font-options] Treat NULL as a default cairo_font_options_t
>>     Interpret a NULL cairo_font_options_t as the default values - i.e
>>     as if it were a fresh pointer returned by
>> cairo_font_options_create().
> I thought about this before.  I think we shouldn't doing it.  Currently
> the cairo practice is: no NULL.

I agree with Behdad here, but perhaps we should be more careful never to 
return a NULL font options in external libraries like gdkcairo.

Just today I received a crasher bug report in an application. The 
problem code?

     if (GdkScreen* screen = gdk_screen_get_default())
         options = gdk_screen_get_font_options(screen);
         options = defaultOptions;

     m_scaledFont = cairo_scaled_font_create(face, &mat, &ctm, options);

This looks safe, but in fact will lead to crashes because 
gdk_screen_get_font_options() is documented as being able to return NULL:

  * gdk_screen_get_font_options:
  * @screen: a #GdkScreen
  * Gets any options previously set with gdk_screen_set_font_options().
  * Return value: the current font options, or %NULL if no default
  *  font options have been set.
  * Since: 2.10

This is hard to catch because, on desktop systems, 
gdk_screen_set_font_options() almost always returns a valid 

It only seems to return NULL on obscure configurations (the user was 
testing on an embedded ARM board), so leads to crashes further down the 
line even after reasonable testing.

More information about the cairo mailing list