Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Comments on noreturn function with non-void return type

Parent

noreturn function with non-void return type

+5
−0

Is it legal ISO C to declare a function as noreturn with a non-void return type (but of course not actually returning)?

As far as I can read from the standard, it seems legal.

Example:

noreturn void *foo(void *x)
{
	pthread_exit(x);
}

ISO C (N2731, C2x) says:

6.7.4 Function specifiers

8 A function declared with a _Noreturn function specifier shall not return to its caller.

Recommended practice

9 The implementation should produce a diagnostic message for a function declared with a _Noreturn function specifier that appears to be capable of returning to its caller.

12 EXAMPLE 2

_Noreturn void f () {
    abort(); // ok
}
_Noreturn void g (int i) { // causes undefined behavior if i <= 0
    if (i > 0) abort();
}

J.2 Undefined behavior

  • A function declared with a _Noreturn function specifier returns to its caller (6.7.4)

There's some doubt in 6.7.4/9, where it says that it's recommended to diagnostic a function that appears to be capable of returning to its caller (noreturn void *foo(void *x), from seeing only it's prototype, "appears to be capable of returning to its caller"), but the other text (and the example) seems to allow it as long as the function body doesn't seem to return.

If it's valid, it can be useful to pass &foo to pthread_create(3), which expects a function that returns void *, while marking it as noreturn, since it won't return.

pthread_create(3):

       #include <pthread.h>

       int pthread_create(pthread_t *restrict thread,
                          const pthread_attr_t *restrict attr,
                          void *(*start_routine)(void *),
                          void *restrict arg);

Current GCC and Clang seem to not warn about it, with -Wall -Wextra (and Clang -Weverything), so it looks good.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.
Why should this post be closed?

0 comment threads

Post
+3
−0

Syntax-wise it is a function specifier and may in theory appear everywhere where inline (or rather the syntax item function-specifier:) can appear, since the standard doesn't say otherwise. Though of course it would be nonsense to declare _Noreturn together with a return type and a compiler failing to warn against such would be a low quality of implementation.

Though I believe the "recommended practice" part is a "code coverage" recommendation, as in having the compiler diagnose if any execution path inside the function could lead to it returning, rather than just checking if it is declared with a return type other than void. This is just a recommendation though, so it isn't normative or required. The only normative text here is 6.7.4/8 and violating it would lead to undefined behavior.

Regarding pthread callbacks I'm not sure what benefit it would yield other than the compiler perhaps skipping some return instruction and saving a few bytes of code size. Arguably, a thread function which never returns but has to be clobbered to death with brute force is incorrectly designed - threads should always be able to exist gracefully on their own upon the reception of a event/semaphore etc. Never returning would potentially also break pthread_join, in case it expects a certain calling convention but the callback deviates from the expected void* f(void*) format (by not stacking the return pointer etc).

As for Clang specifically, I very much doubt it is capable of giving warnings about _Noreturn. Clang is as far as I know still completely broken in this regard and unable to follow the 6.7.4 recommendation since it is unable to correctly generate execution paths to begin with, let alone diagnose them. See this compiler bug: How do I make an infinite empty loop that won't be optimized away?

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

2 comment threads

pthread_join(3) (3 comments)
It's quite sensible to have _Noreturn with a non-void return type (9 comments)
pthread_join(3)
alx‭ wrote over 2 years ago

Calling pthread_exit(3) instead of returning from the callback is the same as calling exit(3) instead of returning from main(). It's not like you're killing it with a signal or anything like that. It handles everything gracefully and acts as if it returned: "The pthread_exit() function terminates the calling thread and returns a value via retval that (if the thread is joinable) is available to another thread in the same process that calls pthread_join(3)."

Lundin‭ wrote over 2 years ago

alx‭ Never the less, it is bad practice not to clean up your own mess no matter what the OS does. Executing such clean-up code is also an excellent way of surfacing dormant bugs elsewhere in the application (free() crashing etc).

alx‭ wrote over 2 years ago

Might be, yes. I usually return. But I was reviewing some existing code that used that scheme. But I tend to agree :)