cURL / Mailing Lists / curl-library / Single Mail

curl-library

Re: s/bool/int

From: Kamil Dudka <kdudka_at_redhat.com>
Date: Wed, 26 May 2010 11:32:22 +0200

On Tue May 25 2010 23:39:31 Daniel Stenberg wrote:
> On Tue, 25 May 2010, Kamil Dudka wrote:
> > Is it anyhow useful for you to get a warning when assigning a non-bool
> > value to bool variable?
>
> Yes I think so. The variables we declare as 'bool' MUST NEVER be anything
> else than TRUE or FALSE.
>
> > If so, I can look for a solution to highlight such suspicious places in
> > the code.
>
> If you can figure out a way that isn't making the code ugly, that'd be
> great!

I abused sparse(1) to show us the suspicious places. See the attached patch.
Surprisingly, the situation is really _not_ so bad - only one occurrence,
which is also related to the wildcard patch:

$ make CC=cgcc 2>&1 | grep 'implied cast'
url.c:2466:31: warning: implied cast from long to bool

A patch for this one is attached. Just to show that it catch at least
the problem Tor was facing I tried following:

$ git checkout -b tmp
$ git revert 606b933
$ make CC=cgcc 2>&1 | grep 'implied cast'
curl_fnmatch.c:351:21: warning: implied cast from int to bool
curl_fnmatch.c:353:21: warning: implied cast from int to bool
curl_fnmatch.c:355:21: warning: implied cast from int to bool
curl_fnmatch.c:357:21: warning: implied cast from int to bool
curl_fnmatch.c:359:21: warning: implied cast from int to bool
curl_fnmatch.c:361:21: warning: implied cast from int to bool
curl_fnmatch.c:363:21: warning: implied cast from int to bool
curl_fnmatch.c:365:21: warning: implied cast from int to bool
curl_fnmatch.c:367:21: warning: implied cast from int to bool
curl_fnmatch.c:369:21: warning: implied cast from int to bool

Another problem is that the explicit cast to bool may be also error-prone when
bool is defined is 'unsigned char' or something like that. Luckily, that's
not the default setup.

If you use the _standard_ bool, provided e.g. by gcc, then the value indeed
_is_ guaranteed to be 0 or 1. See the attached example. It gives me the
following:

int = -1 -> bool = 1
int = 0 -> bool = 0
int = 1 -> bool = 1
int = 2 -> bool = 1

Kamil

-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-library
Etiquette: http://curl.haxx.se/mail/etiquette.html

  • text/x-csrc attachment: test.c
Received on 2010-05-26