Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C++: Support more "noreturn" attributes in DefaultOptions #18510

Merged
merged 5 commits into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions cpp/ql/lib/DefaultOptions.qll
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ class Options extends string {
*
* By default, this holds for `exit`, `_exit`, `_Exit`, `abort`,
* `__assert_fail`, `longjmp`, `__builtin_unreachable` and any
* function with a `noreturn` or `__noreturn__` attribute or
* `noreturn` specifier.
* function with a `noreturn`, `__noreturn__`, or `_Noreturn`
* attribute or `noreturn` specifier.
*/
predicate exits(Function f) {
f.getAnAttribute().hasName(["noreturn", "__noreturn__"])
f.getAnAttribute().hasName(["noreturn", "__noreturn__", "_Noreturn"])
or
f.getASpecifier().hasName("noreturn")
or
Expand Down
4 changes: 4 additions & 0 deletions cpp/ql/lib/change-notes/2025-01-16-noreturn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
category: minorAnalysis
---
* `DefaultOptions::exits` now holds for C23 functions with the `_Noreturn` or `___Noreturn__` attribute.
26 changes: 25 additions & 1 deletion cpp/ql/test/query-tests/jsf/4.13 Functions/AV Rule 114/test.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// semmle-extractor-options: -std=c11
// semmle-extractor-options: -std=c23
int f1(void) {
int x = 1;
return 2;
Expand Down Expand Up @@ -110,3 +110,27 @@ int f17() {
if (__builtin_expect(1, 0))
__builtin_unreachable(); // GOOD
}

[[_Noreturn]] void f18();

int f19() {
f18(); // GOOD
}

[[___Noreturn__]] void f20();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's going on with __Noreturn__ (in the change note), ___Noreturn__ (here, with the extra _), and the predicate itself (which list neither)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be with just two _, well spotted. Let me temporarily put this back in draft and fix this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wrong. It should be three _ as a prefix and two as a suffix, so it's just the change note that's incorrect. See: https://en.cppreference.com/w/c/language/attributes/noreturn. Note that the lower case version just has two _ both as prefix and as suffix.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The predicate itself does not list ___Noreturn__, as the frontend strips off two of the _ in the prefix and also strips off the __ suffix. So, those do not end up in the database.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow, they've made this unnecessarily fiddly!


int f21() {
f20(); // GOOD
}

[[noreturn]] void f22();

int f23() {
f22(); // GOOD
}

[[__noreturn__]] void f24();

int f25() {
f24(); // GOOD
}
Original file line number Diff line number Diff line change
Expand Up @@ -188,3 +188,10 @@ int g22() {
int g23() {
Aborting().a(); // GOOD [FALSE POSITIVE]
}

[[__noreturn__]]
int g24();

int g25() {
g24(); // GOOD
}