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?

When did hardware antialiasing start being available?

Turning a hard to access nut?

Animal R'aim of the midrash

Creating points with attributes from coordinates in ArcPy

Recursively updating the MLE as new observations stream in

How to find the largest number(s) in a list of elements, possibly non-unique?

Error in master's thesis, I do not know what to do

Why is the intercept typed in as a 1 in stats packages (R, python)

How to understand 「僕は誰より彼女が好きなんだ。」

Print a physical multiplication table

Jem'Hadar, something strange about their life expectancy

The English Debate

Writing in a Christian voice

Determine voltage drop over 10G resistors with cheap multimeter

Was World War I a war of liberals against authoritarians?

What is the difference between something being completely legal and being completely decriminalized?

PTIJ: Which Dr. Seuss books should one obtain?

Animating wave motion in water

What kind of footwear is suitable for walking in micro gravity environment?

How do you justify more code being written by following clean code practices?

Started in 1987 vs. Starting in 1987

Asserting that Atheism and Theism are both faith based positions

Should a narrator ever describe things based on a characters view instead of fact?

Should I be concerned about student access to a test bank?



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?










8















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
    2 days ago












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

    – Richard Chambers
    2 days 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
    2 days ago















8















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
    2 days ago












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

    – Richard Chambers
    2 days 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
    2 days ago













8












8








8


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 2 days ago









enscensc

4,411815




4,411815












  • 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
    2 days ago












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

    – Richard Chambers
    2 days 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
    2 days 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
    2 days ago












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

    – Richard Chambers
    2 days 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
    2 days 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
2 days 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
2 days ago














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

– Richard Chambers
2 days ago





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

– Richard Chambers
2 days 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
2 days 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
2 days ago












2 Answers
2






active

oldest

votes


















2














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

























  • 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
    2 days ago






  • 1





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

    – wildplasser
    2 days 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
    2 days 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
    2 days 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






















    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

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    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

























    • 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
      2 days ago






    • 1





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

      – wildplasser
      2 days 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
      2 days 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
      2 days ago















    2














    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

























    • 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
      2 days ago






    • 1





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

      – wildplasser
      2 days 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
      2 days 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
      2 days ago













    2












    2








    2







    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 days ago









    machine_1

    2,47921332




    2,47921332










    answered 2 days ago









    P__J__P__J__

    11.3k2726




    11.3k2726












    • 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
      2 days ago






    • 1





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

      – wildplasser
      2 days 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
      2 days 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
      2 days 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
      2 days ago






    • 1





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

      – wildplasser
      2 days 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
      2 days 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
      2 days 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
    2 days 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
    2 days ago




    1




    1





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

    – wildplasser
    2 days ago






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

    – wildplasser
    2 days 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
    2 days 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
    2 days 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
    2 days 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
    2 days 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 2 days ago









        Antti HaapalaAntti Haapala

        84.6k16161202




        84.6k16161202



























            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

            Adding axes to figuresAdding axes labels to LaTeX figuresLaTeX equivalent of ConTeXt buffersRotate a node but not its content: the case of the ellipse decorationHow to define the default vertical distance between nodes?TikZ scaling graphic and adjust node position and keep font sizeNumerical conditional within tikz keys?adding axes to shapesAlign axes across subfiguresAdding figures with a certain orderLine up nested tikz enviroments or how to get rid of themAdding axes labels to LaTeX figures

            Luettelo Yhdysvaltain laivaston lentotukialuksista Lähteet | Navigointivalikko

            Gary (muusikko) Sisällysluettelo Historia | Rockin' High | Lähteet | Aiheesta muualla | NavigointivalikkoInfobox OKTuomas "Gary" Keskinen Ancaran kitaristiksiProjekti Rockin' High