[cairo] cairo_set_operator() sanity check

Bill Spitzak spitzak at d2.com
Wed Feb 22 10:31:03 PST 2006



Doodle wrote:
> 
> Hi,
> 
> I suddenly experienced a problem with my Cairo testapp, which selects 
> random operators and draws random shapes on the screen.
> 
> Now, I made a mistake in there, which resulted in that 
> cairo_set_operator() was sometimes called with operator number 14, which 
> of course does not exist. The testapp crashed at cairo_stroke() at once.
> 
> I know, it's user error, but I'm interested in your opinions, shouldn't 
> such trivial checks be in Cairo functions (well, up to a limit, of course)?

It may make sense for forward-compatability to have all the unknown 
operator numbers do the screen (a+b-ab) operator. This will allow more 
photoshop-style composites to be added in the future and the programs 
will not have to check the cairo version to avoid crashing, and will 
still produce readable (though wrong) output.

In general I like substituting out-of-range things with some legal 
value, either by masking the bits if it is a power of 2, or replacing 
all illegal values with the same constant. Often it is pretty obvious 
what the replacement value should be.

Please don't set the cairo status or otherwise add error handling, I 
think such code is misleading because people looking at the code often 
think the error path is the important one, and the cairo status should 
be reserved for reporting problems that are not under the programmer's 
control.


More information about the cairo mailing list