gcc: how to detect bad `bool` usage2019 Community Moderator ElectionHow do I download and install lint?How do you set, clear, and toggle a single bit?What is the difference between g++ and gcc?Why doesn't GCC optimize a*a*a*a*a*a to (a*a*a)*(a*a*a)?Catch incorrect usage of c boolInconsistent gcc diagnostic for string initializationCan we add CV-qualifiers to the arguments of main?gcc vs clang: Meaning of -pedantic-errorsNo warning for uninitialized objects, wrong order in constructionHow to detect all buffer access by using Clang Static CheckerStricit aliasing violation: Why gcc and clang generate different output?

Is it normal that my co-workers at a fitness company criticize my food choices?

Credit cards used everywhere in Singapore or Malaysia?

How well should I expect Adam to work?

This word with a lot of past tenses

What is the significance behind "40 days" that often appears in the Bible?

Most cost effective thermostat setting: consistent temperature vs. lowest temperature possible

Meme-controlled people

Numerical Minimization of Large Expression

What is "focus distance lower/upper" and how is it different from depth of field?

Did Ender ever learn that he killed Stilson and/or Bonzo?

Why won't this compile? Argument of h has an extra {

How do I hide Chekhov's Gun?

As a new Ubuntu desktop 18.04 LTS user, do I need to use ufw for a firewall or is iptables sufficient?

Official degrees of earth’s rotation per day

Life insurance that covers only simultaneous/dual deaths

Python if-else code style for reduced code for rounding floats

Employee lack of ownership

Time travel from stationary position?

What did “the good wine” (τὸν καλὸν οἶνον) mean in John 2:10?

Recruiter wants very extensive technical details about all of my previous work

Brexit - No Deal Rejection

Are ETF trackers fundamentally better than individual stocks?

How difficult is it to simply disable/disengage the MCAS on Boeing 737 Max 8 & 9 Aircraft?

Is honey really a supersaturated solution? Does heating to un-crystalize redissolve it or melt it?



gcc: how to detect bad `bool` usage



2019 Community Moderator ElectionHow do I download and install lint?How do you set, clear, and toggle a single bit?What is the difference between g++ and gcc?Why doesn't GCC optimize a*a*a*a*a*a to (a*a*a)*(a*a*a)?Catch incorrect usage of c boolInconsistent gcc diagnostic for string initializationCan we add CV-qualifiers to the arguments of main?gcc vs clang: Meaning of -pedantic-errorsNo warning for uninitialized objects, wrong order in constructionHow to detect all buffer access by using Clang Static CheckerStricit aliasing violation: Why gcc and clang generate different output?










7















Is there some way to detect the bad usage of bool values in code like



#include <stdbool.h>

void *foo(void)

return false;


int bar(void)

return true;



Both functions are accepted by gcc (8.3.1) and clang (7.0.1) without any warnings



$ gcc -Wall -W -pedantic -c x.c
$ clang -Xclang -analyzer-checker=alpha --analyze -Wall -W -pedantic -c x.c
$ clang -Wall -W -pedantic -c x.c
$


Compiling as C++ code would detect the problem in foo() but is not an option but rest of code is C, not C++.



Are there other (-W) options or switches which would create diagnostics for these cases?










share|improve this question






















  • Would lint catch these? Especially an updated tool such as PCLint? See for instance invisible-island.net/personal/lint-tools.html and see this about lint pubs.opengroup.org/onlinepubs/7908799/xcu/lint.html

    – Richard Chambers
    4 hours ago












  • Looks like there is an updated version, splint, stackoverflow.com/questions/6881269/…

    – Richard Chambers
    4 hours ago











  • @RichardChambers thinks for the hint; but "splint" does not seem to understand C99+ at all and aborts when seeing variadic macros (e.g. #define log_err(_fmt, ...)).

    – ensc
    4 hours ago















7















Is there some way to detect the bad usage of bool values in code like



#include <stdbool.h>

void *foo(void)

return false;


int bar(void)

return true;



Both functions are accepted by gcc (8.3.1) and clang (7.0.1) without any warnings



$ gcc -Wall -W -pedantic -c x.c
$ clang -Xclang -analyzer-checker=alpha --analyze -Wall -W -pedantic -c x.c
$ clang -Wall -W -pedantic -c x.c
$


Compiling as C++ code would detect the problem in foo() but is not an option but rest of code is C, not C++.



Are there other (-W) options or switches which would create diagnostics for these cases?










share|improve this question






















  • Would lint catch these? Especially an updated tool such as PCLint? See for instance invisible-island.net/personal/lint-tools.html and see this about lint pubs.opengroup.org/onlinepubs/7908799/xcu/lint.html

    – Richard Chambers
    4 hours ago












  • Looks like there is an updated version, splint, stackoverflow.com/questions/6881269/…

    – Richard Chambers
    4 hours ago











  • @RichardChambers thinks for the hint; but "splint" does not seem to understand C99+ at all and aborts when seeing variadic macros (e.g. #define log_err(_fmt, ...)).

    – ensc
    4 hours ago













7












7








7


1






Is there some way to detect the bad usage of bool values in code like



#include <stdbool.h>

void *foo(void)

return false;


int bar(void)

return true;



Both functions are accepted by gcc (8.3.1) and clang (7.0.1) without any warnings



$ gcc -Wall -W -pedantic -c x.c
$ clang -Xclang -analyzer-checker=alpha --analyze -Wall -W -pedantic -c x.c
$ clang -Wall -W -pedantic -c x.c
$


Compiling as C++ code would detect the problem in foo() but is not an option but rest of code is C, not C++.



Are there other (-W) options or switches which would create diagnostics for these cases?










share|improve this question














Is there some way to detect the bad usage of bool values in code like



#include <stdbool.h>

void *foo(void)

return false;


int bar(void)

return true;



Both functions are accepted by gcc (8.3.1) and clang (7.0.1) without any warnings



$ gcc -Wall -W -pedantic -c x.c
$ clang -Xclang -analyzer-checker=alpha --analyze -Wall -W -pedantic -c x.c
$ clang -Wall -W -pedantic -c x.c
$


Compiling as C++ code would detect the problem in foo() but is not an option but rest of code is C, not C++.



Are there other (-W) options or switches which would create diagnostics for these cases?







c gcc clang static-analysis






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 5 hours ago









enscensc

4,336815




4,336815












  • Would lint catch these? Especially an updated tool such as PCLint? See for instance invisible-island.net/personal/lint-tools.html and see this about lint pubs.opengroup.org/onlinepubs/7908799/xcu/lint.html

    – Richard Chambers
    4 hours ago












  • Looks like there is an updated version, splint, stackoverflow.com/questions/6881269/…

    – Richard Chambers
    4 hours ago











  • @RichardChambers thinks for the hint; but "splint" does not seem to understand C99+ at all and aborts when seeing variadic macros (e.g. #define log_err(_fmt, ...)).

    – ensc
    4 hours ago

















  • Would lint catch these? Especially an updated tool such as PCLint? See for instance invisible-island.net/personal/lint-tools.html and see this about lint pubs.opengroup.org/onlinepubs/7908799/xcu/lint.html

    – Richard Chambers
    4 hours ago












  • Looks like there is an updated version, splint, stackoverflow.com/questions/6881269/…

    – Richard Chambers
    4 hours ago











  • @RichardChambers thinks for the hint; but "splint" does not seem to understand C99+ at all and aborts when seeing variadic macros (e.g. #define log_err(_fmt, ...)).

    – ensc
    4 hours ago
















Would lint catch these? Especially an updated tool such as PCLint? See for instance invisible-island.net/personal/lint-tools.html and see this about lint pubs.opengroup.org/onlinepubs/7908799/xcu/lint.html

– Richard Chambers
4 hours ago






Would lint catch these? Especially an updated tool such as PCLint? See for instance invisible-island.net/personal/lint-tools.html and see this about lint pubs.opengroup.org/onlinepubs/7908799/xcu/lint.html

– Richard Chambers
4 hours ago














Looks like there is an updated version, splint, stackoverflow.com/questions/6881269/…

– Richard Chambers
4 hours ago





Looks like there is an updated version, splint, stackoverflow.com/questions/6881269/…

– Richard Chambers
4 hours ago













@RichardChambers thinks for the hint; but "splint" does not seem to understand C99+ at all and aborts when seeing variadic macros (e.g. #define log_err(_fmt, ...)).

– ensc
4 hours ago





@RichardChambers thinks for the hint; but "splint" does not seem to understand C99+ at all and aborts when seeing variadic macros (e.g. #define log_err(_fmt, ...)).

– ensc
4 hours ago












3 Answers
3






active

oldest

votes


















3














Make the example less trivial:



bool x;

void *foo(void)

return x;


int bar(void)

return x;



and it want compile at all.



usually true and false are just definitions and have value 1 and 0



From the stdbool.h header file



#ifndef _STDBOOL_H
#define _STDBOOL_H

#ifndef __cplusplus

#define bool _Bool
#define true 1
#define false 0

#else /* __cplusplus */


in your first example you just return zero and most compilers will not warn as they treat it as NULL. Try to return true and you will get the warning.






share|improve this answer

























  • Any comment DV folks?

    – P__J__
    4 hours ago











  • My code is some simplified variant of refactored code where functions previously returned a bool value and where converted to return richer values. The return true resp. return false was missed in the refactoring and stayed literally; I just want to detect them.

    – ensc
    4 hours ago






  • 1





    Just don't include stdbool.h (you don't need it), and you'll catch all the occurences.

    – wildplasser
    4 hours ago












  • @wildplasser or if stdbool.h is buried deeper, a temporary local redefinition to "break" bool, true and false could also help identify the places to change. (don't leave it like that, but...)

    – Leushenko
    4 hours ago











  • @wildplasser Not a good idea. In my experience, using stdbool.h can significantly increase code readability, simply because booleans are labeled as such. Of course, I would have preferred a definition like ((_Bool)0), but I've never run into the OP's problem (even though I'm making liberal use of stdbool.h).

    – cmaster
    4 hours ago


















1














C defines the <stdbool.h> macros true and false as expanding to integer constant expressions of value 1 and 0 respectively. Since they're ints and bool (_Bool) in itself is an integer type, any such usage is equally valid. Even the value of the boolean expressions in C is an int and not a bool, so there is not much help for you with the bar function.



However, foo is a different beast - if the return value were true then it would be caught right away because 1 is not convertible to a pointer. false, having the integer constant value 0 is a null-pointer constant and will be converted to null pointer. You could perhaps catch the incorrect use by replacing the #include <stdbool.h> with something that does the inclusion but defines false as say 0.0 which is a falsy value but not an integer constant expression.






share|improve this answer






























    0














    The interresting part here is that you call it "bad usage". But the code is perfectly valid.



    _Bool is an integer type.



    From the standard:




    7.18 The macro bool expands to _Bool.




    and




    6.2.4 An object declared as type _Bool is large enough to store the values 0 and 1.




    and




    6.3.2.3 An integer may be converted to any pointer type.




    So there is nothing in your code that violates the standard.



    That said, you may get a warning for a missing cast to void* but besides that there is no reason for any warnings.



    The code may look strange and in theory a compiler could detect that and give a warning. To my knowledge there is no compiler that will do that.






    share|improve this answer

























    • I know that this code is valid C99; but it is wrong and compilers are allowed to emit diagnostics for cases where they suspect broken code. gcc does this already for lot of valid C99 code (even for indentation issues), seems to know something about _Bool itself (see -fsanitize=bool) and I would like to have warnings about my broken code.

      – ensc
      4 hours ago











    • @ensc Maybe I misunderstand the whole question but... why do you expect warnings from code that complies with the standard?

      – 4386427
      4 hours ago











    • because compilers are doing static code analysis noways and I expect that they warn about (obvious) problems.

      – ensc
      4 hours ago











    • @4386427 would you also claim that warnings about assignments in conditions are not useful? They are perfectly valid but nevertheless fishy.

      – Gerhardh
      4 hours ago











    • @ensc Hmm... so your question is really about getting a warning for "strange looking" but compliant code? If so... I obviously misunderstod your question. I don't know a way to get a warning for something like int * p = (int*) 3;

      – 4386427
      4 hours ago










    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55198396%2fgcc-how-to-detect-bad-bool-usage%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    3














    Make the example less trivial:



    bool x;

    void *foo(void)

    return x;


    int bar(void)

    return x;



    and it want compile at all.



    usually true and false are just definitions and have value 1 and 0



    From the stdbool.h header file



    #ifndef _STDBOOL_H
    #define _STDBOOL_H

    #ifndef __cplusplus

    #define bool _Bool
    #define true 1
    #define false 0

    #else /* __cplusplus */


    in your first example you just return zero and most compilers will not warn as they treat it as NULL. Try to return true and you will get the warning.






    share|improve this answer

























    • Any comment DV folks?

      – P__J__
      4 hours ago











    • My code is some simplified variant of refactored code where functions previously returned a bool value and where converted to return richer values. The return true resp. return false was missed in the refactoring and stayed literally; I just want to detect them.

      – ensc
      4 hours ago






    • 1





      Just don't include stdbool.h (you don't need it), and you'll catch all the occurences.

      – wildplasser
      4 hours ago












    • @wildplasser or if stdbool.h is buried deeper, a temporary local redefinition to "break" bool, true and false could also help identify the places to change. (don't leave it like that, but...)

      – Leushenko
      4 hours ago











    • @wildplasser Not a good idea. In my experience, using stdbool.h can significantly increase code readability, simply because booleans are labeled as such. Of course, I would have preferred a definition like ((_Bool)0), but I've never run into the OP's problem (even though I'm making liberal use of stdbool.h).

      – cmaster
      4 hours ago















    3














    Make the example less trivial:



    bool x;

    void *foo(void)

    return x;


    int bar(void)

    return x;



    and it want compile at all.



    usually true and false are just definitions and have value 1 and 0



    From the stdbool.h header file



    #ifndef _STDBOOL_H
    #define _STDBOOL_H

    #ifndef __cplusplus

    #define bool _Bool
    #define true 1
    #define false 0

    #else /* __cplusplus */


    in your first example you just return zero and most compilers will not warn as they treat it as NULL. Try to return true and you will get the warning.






    share|improve this answer

























    • Any comment DV folks?

      – P__J__
      4 hours ago











    • My code is some simplified variant of refactored code where functions previously returned a bool value and where converted to return richer values. The return true resp. return false was missed in the refactoring and stayed literally; I just want to detect them.

      – ensc
      4 hours ago






    • 1





      Just don't include stdbool.h (you don't need it), and you'll catch all the occurences.

      – wildplasser
      4 hours ago












    • @wildplasser or if stdbool.h is buried deeper, a temporary local redefinition to "break" bool, true and false could also help identify the places to change. (don't leave it like that, but...)

      – Leushenko
      4 hours ago











    • @wildplasser Not a good idea. In my experience, using stdbool.h can significantly increase code readability, simply because booleans are labeled as such. Of course, I would have preferred a definition like ((_Bool)0), but I've never run into the OP's problem (even though I'm making liberal use of stdbool.h).

      – cmaster
      4 hours ago













    3












    3








    3







    Make the example less trivial:



    bool x;

    void *foo(void)

    return x;


    int bar(void)

    return x;



    and it want compile at all.



    usually true and false are just definitions and have value 1 and 0



    From the stdbool.h header file



    #ifndef _STDBOOL_H
    #define _STDBOOL_H

    #ifndef __cplusplus

    #define bool _Bool
    #define true 1
    #define false 0

    #else /* __cplusplus */


    in your first example you just return zero and most compilers will not warn as they treat it as NULL. Try to return true and you will get the warning.






    share|improve this answer















    Make the example less trivial:



    bool x;

    void *foo(void)

    return x;


    int bar(void)

    return x;



    and it want compile at all.



    usually true and false are just definitions and have value 1 and 0



    From the stdbool.h header file



    #ifndef _STDBOOL_H
    #define _STDBOOL_H

    #ifndef __cplusplus

    #define bool _Bool
    #define true 1
    #define false 0

    #else /* __cplusplus */


    in your first example you just return zero and most compilers will not warn as they treat it as NULL. Try to return true and you will get the warning.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited 2 hours ago









    machine_1

    2,47921332




    2,47921332










    answered 4 hours ago









    P__J__P__J__

    11.3k2726




    11.3k2726












    • Any comment DV folks?

      – P__J__
      4 hours ago











    • My code is some simplified variant of refactored code where functions previously returned a bool value and where converted to return richer values. The return true resp. return false was missed in the refactoring and stayed literally; I just want to detect them.

      – ensc
      4 hours ago






    • 1





      Just don't include stdbool.h (you don't need it), and you'll catch all the occurences.

      – wildplasser
      4 hours ago












    • @wildplasser or if stdbool.h is buried deeper, a temporary local redefinition to "break" bool, true and false could also help identify the places to change. (don't leave it like that, but...)

      – Leushenko
      4 hours ago











    • @wildplasser Not a good idea. In my experience, using stdbool.h can significantly increase code readability, simply because booleans are labeled as such. Of course, I would have preferred a definition like ((_Bool)0), but I've never run into the OP's problem (even though I'm making liberal use of stdbool.h).

      – cmaster
      4 hours ago

















    • Any comment DV folks?

      – P__J__
      4 hours ago











    • My code is some simplified variant of refactored code where functions previously returned a bool value and where converted to return richer values. The return true resp. return false was missed in the refactoring and stayed literally; I just want to detect them.

      – ensc
      4 hours ago






    • 1





      Just don't include stdbool.h (you don't need it), and you'll catch all the occurences.

      – wildplasser
      4 hours ago












    • @wildplasser or if stdbool.h is buried deeper, a temporary local redefinition to "break" bool, true and false could also help identify the places to change. (don't leave it like that, but...)

      – Leushenko
      4 hours ago











    • @wildplasser Not a good idea. In my experience, using stdbool.h can significantly increase code readability, simply because booleans are labeled as such. Of course, I would have preferred a definition like ((_Bool)0), but I've never run into the OP's problem (even though I'm making liberal use of stdbool.h).

      – cmaster
      4 hours ago
















    Any comment DV folks?

    – P__J__
    4 hours ago





    Any comment DV folks?

    – P__J__
    4 hours ago













    My code is some simplified variant of refactored code where functions previously returned a bool value and where converted to return richer values. The return true resp. return false was missed in the refactoring and stayed literally; I just want to detect them.

    – ensc
    4 hours ago





    My code is some simplified variant of refactored code where functions previously returned a bool value and where converted to return richer values. The return true resp. return false was missed in the refactoring and stayed literally; I just want to detect them.

    – ensc
    4 hours ago




    1




    1





    Just don't include stdbool.h (you don't need it), and you'll catch all the occurences.

    – wildplasser
    4 hours ago






    Just don't include stdbool.h (you don't need it), and you'll catch all the occurences.

    – wildplasser
    4 hours ago














    @wildplasser or if stdbool.h is buried deeper, a temporary local redefinition to "break" bool, true and false could also help identify the places to change. (don't leave it like that, but...)

    – Leushenko
    4 hours ago





    @wildplasser or if stdbool.h is buried deeper, a temporary local redefinition to "break" bool, true and false could also help identify the places to change. (don't leave it like that, but...)

    – Leushenko
    4 hours ago













    @wildplasser Not a good idea. In my experience, using stdbool.h can significantly increase code readability, simply because booleans are labeled as such. Of course, I would have preferred a definition like ((_Bool)0), but I've never run into the OP's problem (even though I'm making liberal use of stdbool.h).

    – cmaster
    4 hours ago





    @wildplasser Not a good idea. In my experience, using stdbool.h can significantly increase code readability, simply because booleans are labeled as such. Of course, I would have preferred a definition like ((_Bool)0), but I've never run into the OP's problem (even though I'm making liberal use of stdbool.h).

    – cmaster
    4 hours ago













    1














    C defines the <stdbool.h> macros true and false as expanding to integer constant expressions of value 1 and 0 respectively. Since they're ints and bool (_Bool) in itself is an integer type, any such usage is equally valid. Even the value of the boolean expressions in C is an int and not a bool, so there is not much help for you with the bar function.



    However, foo is a different beast - if the return value were true then it would be caught right away because 1 is not convertible to a pointer. false, having the integer constant value 0 is a null-pointer constant and will be converted to null pointer. You could perhaps catch the incorrect use by replacing the #include <stdbool.h> with something that does the inclusion but defines false as say 0.0 which is a falsy value but not an integer constant expression.






    share|improve this answer



























      1














      C defines the <stdbool.h> macros true and false as expanding to integer constant expressions of value 1 and 0 respectively. Since they're ints and bool (_Bool) in itself is an integer type, any such usage is equally valid. Even the value of the boolean expressions in C is an int and not a bool, so there is not much help for you with the bar function.



      However, foo is a different beast - if the return value were true then it would be caught right away because 1 is not convertible to a pointer. false, having the integer constant value 0 is a null-pointer constant and will be converted to null pointer. You could perhaps catch the incorrect use by replacing the #include <stdbool.h> with something that does the inclusion but defines false as say 0.0 which is a falsy value but not an integer constant expression.






      share|improve this answer

























        1












        1








        1







        C defines the <stdbool.h> macros true and false as expanding to integer constant expressions of value 1 and 0 respectively. Since they're ints and bool (_Bool) in itself is an integer type, any such usage is equally valid. Even the value of the boolean expressions in C is an int and not a bool, so there is not much help for you with the bar function.



        However, foo is a different beast - if the return value were true then it would be caught right away because 1 is not convertible to a pointer. false, having the integer constant value 0 is a null-pointer constant and will be converted to null pointer. You could perhaps catch the incorrect use by replacing the #include <stdbool.h> with something that does the inclusion but defines false as say 0.0 which is a falsy value but not an integer constant expression.






        share|improve this answer













        C defines the <stdbool.h> macros true and false as expanding to integer constant expressions of value 1 and 0 respectively. Since they're ints and bool (_Bool) in itself is an integer type, any such usage is equally valid. Even the value of the boolean expressions in C is an int and not a bool, so there is not much help for you with the bar function.



        However, foo is a different beast - if the return value were true then it would be caught right away because 1 is not convertible to a pointer. false, having the integer constant value 0 is a null-pointer constant and will be converted to null pointer. You could perhaps catch the incorrect use by replacing the #include <stdbool.h> with something that does the inclusion but defines false as say 0.0 which is a falsy value but not an integer constant expression.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 4 hours ago









        Antti HaapalaAntti Haapala

        84.5k16161202




        84.5k16161202





















            0














            The interresting part here is that you call it "bad usage". But the code is perfectly valid.



            _Bool is an integer type.



            From the standard:




            7.18 The macro bool expands to _Bool.




            and




            6.2.4 An object declared as type _Bool is large enough to store the values 0 and 1.




            and




            6.3.2.3 An integer may be converted to any pointer type.




            So there is nothing in your code that violates the standard.



            That said, you may get a warning for a missing cast to void* but besides that there is no reason for any warnings.



            The code may look strange and in theory a compiler could detect that and give a warning. To my knowledge there is no compiler that will do that.






            share|improve this answer

























            • I know that this code is valid C99; but it is wrong and compilers are allowed to emit diagnostics for cases where they suspect broken code. gcc does this already for lot of valid C99 code (even for indentation issues), seems to know something about _Bool itself (see -fsanitize=bool) and I would like to have warnings about my broken code.

              – ensc
              4 hours ago











            • @ensc Maybe I misunderstand the whole question but... why do you expect warnings from code that complies with the standard?

              – 4386427
              4 hours ago











            • because compilers are doing static code analysis noways and I expect that they warn about (obvious) problems.

              – ensc
              4 hours ago











            • @4386427 would you also claim that warnings about assignments in conditions are not useful? They are perfectly valid but nevertheless fishy.

              – Gerhardh
              4 hours ago











            • @ensc Hmm... so your question is really about getting a warning for "strange looking" but compliant code? If so... I obviously misunderstod your question. I don't know a way to get a warning for something like int * p = (int*) 3;

              – 4386427
              4 hours ago















            0














            The interresting part here is that you call it "bad usage". But the code is perfectly valid.



            _Bool is an integer type.



            From the standard:




            7.18 The macro bool expands to _Bool.




            and




            6.2.4 An object declared as type _Bool is large enough to store the values 0 and 1.




            and




            6.3.2.3 An integer may be converted to any pointer type.




            So there is nothing in your code that violates the standard.



            That said, you may get a warning for a missing cast to void* but besides that there is no reason for any warnings.



            The code may look strange and in theory a compiler could detect that and give a warning. To my knowledge there is no compiler that will do that.






            share|improve this answer

























            • I know that this code is valid C99; but it is wrong and compilers are allowed to emit diagnostics for cases where they suspect broken code. gcc does this already for lot of valid C99 code (even for indentation issues), seems to know something about _Bool itself (see -fsanitize=bool) and I would like to have warnings about my broken code.

              – ensc
              4 hours ago











            • @ensc Maybe I misunderstand the whole question but... why do you expect warnings from code that complies with the standard?

              – 4386427
              4 hours ago











            • because compilers are doing static code analysis noways and I expect that they warn about (obvious) problems.

              – ensc
              4 hours ago











            • @4386427 would you also claim that warnings about assignments in conditions are not useful? They are perfectly valid but nevertheless fishy.

              – Gerhardh
              4 hours ago











            • @ensc Hmm... so your question is really about getting a warning for "strange looking" but compliant code? If so... I obviously misunderstod your question. I don't know a way to get a warning for something like int * p = (int*) 3;

              – 4386427
              4 hours ago













            0












            0








            0







            The interresting part here is that you call it "bad usage". But the code is perfectly valid.



            _Bool is an integer type.



            From the standard:




            7.18 The macro bool expands to _Bool.




            and




            6.2.4 An object declared as type _Bool is large enough to store the values 0 and 1.




            and




            6.3.2.3 An integer may be converted to any pointer type.




            So there is nothing in your code that violates the standard.



            That said, you may get a warning for a missing cast to void* but besides that there is no reason for any warnings.



            The code may look strange and in theory a compiler could detect that and give a warning. To my knowledge there is no compiler that will do that.






            share|improve this answer















            The interresting part here is that you call it "bad usage". But the code is perfectly valid.



            _Bool is an integer type.



            From the standard:




            7.18 The macro bool expands to _Bool.




            and




            6.2.4 An object declared as type _Bool is large enough to store the values 0 and 1.




            and




            6.3.2.3 An integer may be converted to any pointer type.




            So there is nothing in your code that violates the standard.



            That said, you may get a warning for a missing cast to void* but besides that there is no reason for any warnings.



            The code may look strange and in theory a compiler could detect that and give a warning. To my knowledge there is no compiler that will do that.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited 3 hours ago

























            answered 4 hours ago









            43864274386427

            21.8k31846




            21.8k31846












            • I know that this code is valid C99; but it is wrong and compilers are allowed to emit diagnostics for cases where they suspect broken code. gcc does this already for lot of valid C99 code (even for indentation issues), seems to know something about _Bool itself (see -fsanitize=bool) and I would like to have warnings about my broken code.

              – ensc
              4 hours ago











            • @ensc Maybe I misunderstand the whole question but... why do you expect warnings from code that complies with the standard?

              – 4386427
              4 hours ago











            • because compilers are doing static code analysis noways and I expect that they warn about (obvious) problems.

              – ensc
              4 hours ago











            • @4386427 would you also claim that warnings about assignments in conditions are not useful? They are perfectly valid but nevertheless fishy.

              – Gerhardh
              4 hours ago











            • @ensc Hmm... so your question is really about getting a warning for "strange looking" but compliant code? If so... I obviously misunderstod your question. I don't know a way to get a warning for something like int * p = (int*) 3;

              – 4386427
              4 hours ago

















            • I know that this code is valid C99; but it is wrong and compilers are allowed to emit diagnostics for cases where they suspect broken code. gcc does this already for lot of valid C99 code (even for indentation issues), seems to know something about _Bool itself (see -fsanitize=bool) and I would like to have warnings about my broken code.

              – ensc
              4 hours ago











            • @ensc Maybe I misunderstand the whole question but... why do you expect warnings from code that complies with the standard?

              – 4386427
              4 hours ago











            • because compilers are doing static code analysis noways and I expect that they warn about (obvious) problems.

              – ensc
              4 hours ago











            • @4386427 would you also claim that warnings about assignments in conditions are not useful? They are perfectly valid but nevertheless fishy.

              – Gerhardh
              4 hours ago











            • @ensc Hmm... so your question is really about getting a warning for "strange looking" but compliant code? If so... I obviously misunderstod your question. I don't know a way to get a warning for something like int * p = (int*) 3;

              – 4386427
              4 hours ago
















            I know that this code is valid C99; but it is wrong and compilers are allowed to emit diagnostics for cases where they suspect broken code. gcc does this already for lot of valid C99 code (even for indentation issues), seems to know something about _Bool itself (see -fsanitize=bool) and I would like to have warnings about my broken code.

            – ensc
            4 hours ago





            I know that this code is valid C99; but it is wrong and compilers are allowed to emit diagnostics for cases where they suspect broken code. gcc does this already for lot of valid C99 code (even for indentation issues), seems to know something about _Bool itself (see -fsanitize=bool) and I would like to have warnings about my broken code.

            – ensc
            4 hours ago













            @ensc Maybe I misunderstand the whole question but... why do you expect warnings from code that complies with the standard?

            – 4386427
            4 hours ago





            @ensc Maybe I misunderstand the whole question but... why do you expect warnings from code that complies with the standard?

            – 4386427
            4 hours ago













            because compilers are doing static code analysis noways and I expect that they warn about (obvious) problems.

            – ensc
            4 hours ago





            because compilers are doing static code analysis noways and I expect that they warn about (obvious) problems.

            – ensc
            4 hours ago













            @4386427 would you also claim that warnings about assignments in conditions are not useful? They are perfectly valid but nevertheless fishy.

            – Gerhardh
            4 hours ago





            @4386427 would you also claim that warnings about assignments in conditions are not useful? They are perfectly valid but nevertheless fishy.

            – Gerhardh
            4 hours ago













            @ensc Hmm... so your question is really about getting a warning for "strange looking" but compliant code? If so... I obviously misunderstod your question. I don't know a way to get a warning for something like int * p = (int*) 3;

            – 4386427
            4 hours ago





            @ensc Hmm... so your question is really about getting a warning for "strange looking" but compliant code? If so... I obviously misunderstod your question. I don't know a way to get a warning for something like int * p = (int*) 3;

            – 4386427
            4 hours ago

















            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid …


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55198396%2fgcc-how-to-detect-bad-bool-usage%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Which organization defines CJK Unified Ideographs? The Next CEO of Stack OverflowCharacters which have several different shapesHow useful are the kanji in reading Chinese?Can Chinese readers scan large amounts of text faster/more accurately than their alphabet-using counterparts?丼: why is “well” also “bowl of food”?What Does Unicode 8.0 Mean For Chinese?How are blanks indicated for placeholders in Chinese (like ???)Is there a dictionary of standard character variants?How to display CJK Extension F?How is it decided as to which character is used on the tech terminology?How does 子 come to mean 'midnight'?

            Shenzhen Football Club Índice Elenco atual | Títulos | Referências Ligações externas | Menu de navegaçãoGooglenotíciaslivrosacadêmicoeditar«"Elenco"»Site oficialexpandindo-oeee

            When We Were Young (canção de Adele) Índice Antecedentes e lançamento | Faixas e formatos | Performances e covers | Desempenho nas tabelas musicais | Créditos | Histórico de lançamento | Referências Menu de navegação«Best albums of 2015»«Adele - When We Were Young (Radio Date: 22-01-2016)»«When We Were Young - Single by Adele»«Adele: Inside Her Private Life and Triumphant Return»«adele interview: world exclusive first interview in three years»«Tobias Jesso Jr: since Adele tweeted his song, he's even bigger than his dad»«Adele interviews Tobias Jesso Jr: 'I think that a couple of the ideas we had could be rap songs'»«Adele on Her Return: 'I Was So Frightened That No One Cared'»«Tobias Jesso Jr. on working with Adele: 'I was as nervous as shit'»«How Ariel Rechtshaid Pushed Adele To Her Limit»«Adele Previews 'When We Were Young' on '60 Minutes' Teaser, Tops Trending 140»«Adele Performs New '25' Ballad, "When We Were Young" Live: Watch»«Which '25' Song Should Be Adele's Next Single?»«Adele's 'When We Were Young' Confirmed As Second Single From '25'»«Adele's new single artwork for 'When We Were Young' is perfectly adorable»«Adele at the BBC review: honest, funny and spectacular – Celebrity News News»«'Saturday Night Live': Adele Sings 'Hello' and 'When We Were Young'»«'Adele: Live in New York City' NBC Special – Set List Revealed!»«Adele Closes Out the 2016 Brit Awards With 'When We Were Young'»«What Is The Adele Live Tour Set List? There's No Way She'd Leave Out These 8 Songs»«See Demi Lovato's Soaring Cover of Adele's 'When We Were Young'»«'The Voice': 5 Best Moments From Week 1 Blind Auditions»«Adele – When We Were Young (Media Control Charts)»«Adele – When We Were Young (Entertainment Monitoring Africa)»«Adele – When We Were Young (ARIA Charts)»«Adele – When We Were Young (Ö3 Austria Top 40)»«Adele – When We Were Young (Ultratop 50)»«Adele – When We Were Young (Ultratop 40)»«Adele – When We Were Young (Canadian Hot 100)»«Adele – When We Were Young (Tracklisten)»«Adele – When We Were Young (The Official Charts Company)»«Adele – Hello (IFPI Slovenská Republika)»«Adele – When We Were Young (Productores de Música de España)»«Adele – When We Were Young (Billboard Hot 100)»«Adele – When We Were Young (Pop Songs)»«Adele – When We Were Young (Adult Pop Songs)»«Adele – When We Were Young (Hot Adult Contemporary Charts)»«Adele – When We Were Young (Hot Dance Club Songs)»«Adele – When We Were Young (Rock Airplay)»«Adele – When We Were Young (IFPI Finlândia)»«Adele – When We Were Young (Syndicat National de l'Éditon Phonographique)»«Adele – When We Were Young (Magyar Hanglemezkiadók Szövetsége)»«Adele – When We Were Young (Irish Recorded Music Association)»«Adele – When We Were Young (Mexico Airplay)»«Adele – When We Were Young (VG-lista)»«Adele – When We Were Young (NZ Top 40 Singles)»«Adele – When We Were Young (MegaCharts)»«Adele – When We Were Young (Związek Producentów Audio Video)»«Adele – When We Were Young (Portugal Digital Songs)»«Adele – When We Were Young (UK Indie Singles Chart)»«Adele – When We Were Young (UK Singles Chart)»«Adele – When We Were Young (Sverigetopplistan)»«Adele – When We Were Young (Schweizer Hitparade)»«Adele – When We Were Young (Portugal Digital Songs)»«Music Canada – Gold/Platinum – When We Were Young»«NZ Top 40 Singles Chart»«Certified Awards»e