https://wiki.inkscape.org/wiki/api.php?action=feedcontributions&user=UowNlw&feedformat=atomInkscape Wiki - User contributions [en]2024-03-28T19:15:37ZUser contributionsMediaWiki 1.36.1https://wiki.inkscape.org/wiki/index.php?title=Booleans&diff=14871Booleans2007-06-10T01:56:07Z<p>UowNlw: </p>
<hr />
<div>I've come across a subtle bug that could be in many places in the<br />
sources. Basically, any comparrisons of booleans might be unsafe.<br />
<br />
I first ran across it in sp_document_set_undo_sensitive in<br />
document-undo.c. In it, the param 'sensitive' is of type gboolean. At<br />
one point it is compared with doc->priv->sensitive. Ok, things seem fine<br />
so far... however...<br />
<br />
The specific types in question come to contribute to the problem. Since<br />
the code is C and not C , it uses gboolean instead of bool (of course,<br />
since bool doesn't really exist in plain C). gboolean, in turn, lives in<br />
glib's includes (a good place to get things from) and is typedef'd as gint.<br />
<br />
That's where the problems come in. Since it's an int, not a true bool,<br />
any non-zero value is taken as true, and any zero value is taken as<br />
false. So far that seems OK, but let's take a quick look at some<br />
possible values:<br />
<br />
0 == false<br />
1 == true<br />
-1 == true<br />
$ffffffff == true<br />
~0 == true<br />
4 == true<br />
<br />
Now, given all that, this next line of code clearly has some issues:<br />
<br />
if (sensitive == doc->priv->sensitive) return;<br />
<br />
Let's say that 'sensitive' is set to '~0' (all bits set. a nice safe<br />
non-false value and often used for TRUE), and that doc->priv->sensitive<br />
is set to 1. Now both integer values represent 'true', however they are<br />
not actually equal to each other, and that check will fail even when<br />
both variables are not false.<br />
<br />
:-(<br />
<br />
Therefore, anywhere two boolean-ish variables are compared for equality<br />
or inequality they will not always evaluate properly. Additionally,<br />
since these are just int values of one type or another, they could be<br />
all over the codebase yet not noticed. (There's not something simple and<br />
definitive that I can grep all of the code for, otherwise I would have<br />
tried to find all of them myself).<br />
<br />
So the solution would seem to be to have all eyes watching for potential<br />
problem cases and then fixing them as they are encountered.<br />
<br />
However, there is some good news. Doing a check of a single gboolean<br />
variable is safe, since all non-zero values will be treated as true,<br />
code will work as expected. It's only the case where two values are<br />
compared that can have problems.<br />
<br />
if ( sensitive ) // works<br />
if ( !sensitive ) // works<br />
if ( sensitive</div>UowNlw