Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

#306 assert() should have __noreturn__ attribute

closed-invalid
nobody
header (101)
5
2012-07-06
2012-07-04
Khairul Kasmiran
No

The assert() function and its kind should have the __noreturn__ attribute (through __MINGW_ATTRIB_NORETURN). The reason is illustrated below.

The following program:

--------------------------------------

#include <assert.h>

int main(void) {
assert(0);
}

--------------------------------------

when compiled under -Wreturn-type or -Wall generates the following spurious warning:

--------------------------------------

noreturn.c: In function 'main':
noreturn.c:5:1: warning: control reaches end of non-void function

--------------------------------------

even though control will never reach the end of main().

Discussion

1 2 > >> (Page 1 of 2)
  • Jonathan Yong
    Jonathan Yong
    2012-07-04

    These are runtime asserts, GCC can't know if the assert expression is true or not.
    Besides, noreturn is false for assert, as it might actually return if the expression is true.

     
  • _assert()/_wassert() noreturn patch

     
    Attachments
  • I agree with you, except that the truthness of some assert expressions can be determined at compile time like in the trivial case above.

    Maybe I should have been more specific. What I actually meant is the _assert() function called by the assert macro. The attached patch made against assert.h (revision 5161) at /trunk/mingw-w64-headers/crt/ implements what I wanted.

     
  • Jonathan Yong
    Jonathan Yong
    2012-07-05

    From the GCC manual:
    "Some programs define their own functions that never return. You can declare them noreturn to tell the compiler this fact.."

    How is this even correct for _assert if it can return without aborting the program?

    I have to reiterate again these are runtime assertions, they are not in anyway evaluated at compile time, even if it was obvious.

    I think you are looking for C++ static_assert instead.

     
  • Testing done with the following program:

    --------------------------------------

    #include <assert.h>
    #include <stdio.h>

    int main(void) {
    _assert("1", __FILE__, __LINE__);
    puts("After _assert()");

    return 0;
    }

    --------------------------------------

    strongly suggests that the MinGW-w64 implementation of _assert() does not return no matter what I do.

     
  • Kai Tietz
    Kai Tietz
    2012-07-06

    Yes, it would be a failure to add to assert noreturn as it can return dependent to its boolean-expression-value.

    So this bug is invalid in my opinion.

     
  • Kai Tietz
    Kai Tietz
    2012-07-06

    • status: open --> pending-invalid
     
  • Kai Tietz
    Kai Tietz
    2012-07-06

    Err, sorry about the "expression is true". I mixed it up with assert ....

    There is a chance that _assert/_wassert can return. In GUI mode user has here the option to choose ignore in dialog-box, which means _assert/_wassert returns. Therefore the attribute of noreturn would be wrong.

    Cheers,
    Kai

     
  • Hmm, didn't know that. Thanks Kai for the explanation.

     
    • status: pending-invalid --> open-invalid
     
1 2 > >> (Page 1 of 2)