From f4f5f2899c627495e26a57c2b64bd4f02413a0f0 Mon Sep 17 00:00:00 2001 From: Jeroen Ketema Date: Thu, 16 Jan 2025 13:01:53 +0100 Subject: [PATCH 1/5] C++: Add more noreturn attribute tests --- .../AV Rule 114/AV Rule 114.expected | 3 +++ .../jsf/4.13 Functions/AV Rule 114/test.c | 26 ++++++++++++++++++- .../jsf/4.13 Functions/AV Rule 114/test.cpp | 7 +++++ 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/cpp/ql/test/query-tests/jsf/4.13 Functions/AV Rule 114/AV Rule 114.expected b/cpp/ql/test/query-tests/jsf/4.13 Functions/AV Rule 114/AV Rule 114.expected index 390da977d3f3..be07d1977ac2 100644 --- a/cpp/ql/test/query-tests/jsf/4.13 Functions/AV Rule 114/AV Rule 114.expected +++ b/cpp/ql/test/query-tests/jsf/4.13 Functions/AV Rule 114/AV Rule 114.expected @@ -3,6 +3,9 @@ | test.c:8:5:8:14 | declaration | Function f2 should return a value of type int but does not return a value here | | test.c:25:9:25:14 | ExprStmt | Function f4 should return a value of type int but does not return a value here | | test.c:39:9:39:14 | ExprStmt | Function f6 should return a value of type int but does not return a value here | +| test.c:117:5:117:10 | ExprStmt | Function f19 should return a value of type int but does not return a value here | +| test.c:123:5:123:10 | ExprStmt | Function f21 should return a value of type int but does not return a value here | +| test.c:135:5:135:10 | ExprStmt | Function f25 should return a value of type int but does not return a value here | | test.cpp:16:1:18:1 | { ... } | Function g2 should return a value of type MyValue but does not return a value here | | test.cpp:52:1:52:1 | return ... | Function g7 should return a value of type MyValue but does not return a value here | | test.cpp:74:1:76:1 | { ... } | Function g10 should return a value of type second but does not return a value here | diff --git a/cpp/ql/test/query-tests/jsf/4.13 Functions/AV Rule 114/test.c b/cpp/ql/test/query-tests/jsf/4.13 Functions/AV Rule 114/test.c index e77dc8c5586a..841166253bf9 100644 --- a/cpp/ql/test/query-tests/jsf/4.13 Functions/AV Rule 114/test.c +++ b/cpp/ql/test/query-tests/jsf/4.13 Functions/AV Rule 114/test.c @@ -1,4 +1,4 @@ -// semmle-extractor-options: -std=c11 +// semmle-extractor-options: -std=c23 int f1(void) { int x = 1; return 2; @@ -110,3 +110,27 @@ int f17() { if (__builtin_expect(1, 0)) __builtin_unreachable(); // GOOD } + +[[_Noreturn]] void f18(); + +int f19() { + f18(); // GOOD +} + +[[___Noreturn__]] void f20(); + +int f21() { + f20(); // GOOD +} + +[[noreturn]] void f22(); + +int f23() { + f22(); // GOOD +} + +[[___noreturn__]] void f24(); + +int f25() { + f24(); // GOOD +} diff --git a/cpp/ql/test/query-tests/jsf/4.13 Functions/AV Rule 114/test.cpp b/cpp/ql/test/query-tests/jsf/4.13 Functions/AV Rule 114/test.cpp index c19c5de13f90..0c7e02ce9ac0 100644 --- a/cpp/ql/test/query-tests/jsf/4.13 Functions/AV Rule 114/test.cpp +++ b/cpp/ql/test/query-tests/jsf/4.13 Functions/AV Rule 114/test.cpp @@ -188,3 +188,10 @@ int g22() { int g23() { Aborting().a(); // GOOD [FALSE POSITIVE] } + +[[__noreturn__]] +int g24(); + +int g25() { + g24(); // GOOD +} From bd40d249e3cac3f357015e0c71d83028d4b716f6 Mon Sep 17 00:00:00 2001 From: Jeroen Ketema Date: Thu, 16 Jan 2025 13:04:44 +0100 Subject: [PATCH 2/5] C++: Support more "noreturn" attributes in DefaultOptions --- cpp/ql/lib/DefaultOptions.qll | 6 +++--- .../jsf/4.13 Functions/AV Rule 114/AV Rule 114.expected | 3 --- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/cpp/ql/lib/DefaultOptions.qll b/cpp/ql/lib/DefaultOptions.qll index dd6fe38e792c..0b5aada91793 100644 --- a/cpp/ql/lib/DefaultOptions.qll +++ b/cpp/ql/lib/DefaultOptions.qll @@ -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__`, `_Noreturn`, or + * `_noreturn` attribute or `noreturn` specifier. */ predicate exits(Function f) { - f.getAnAttribute().hasName(["noreturn", "__noreturn__"]) + f.getAnAttribute().hasName(["noreturn", "__noreturn__", "_Noreturn", "_noreturn"]) or f.getASpecifier().hasName("noreturn") or diff --git a/cpp/ql/test/query-tests/jsf/4.13 Functions/AV Rule 114/AV Rule 114.expected b/cpp/ql/test/query-tests/jsf/4.13 Functions/AV Rule 114/AV Rule 114.expected index be07d1977ac2..390da977d3f3 100644 --- a/cpp/ql/test/query-tests/jsf/4.13 Functions/AV Rule 114/AV Rule 114.expected +++ b/cpp/ql/test/query-tests/jsf/4.13 Functions/AV Rule 114/AV Rule 114.expected @@ -3,9 +3,6 @@ | test.c:8:5:8:14 | declaration | Function f2 should return a value of type int but does not return a value here | | test.c:25:9:25:14 | ExprStmt | Function f4 should return a value of type int but does not return a value here | | test.c:39:9:39:14 | ExprStmt | Function f6 should return a value of type int but does not return a value here | -| test.c:117:5:117:10 | ExprStmt | Function f19 should return a value of type int but does not return a value here | -| test.c:123:5:123:10 | ExprStmt | Function f21 should return a value of type int but does not return a value here | -| test.c:135:5:135:10 | ExprStmt | Function f25 should return a value of type int but does not return a value here | | test.cpp:16:1:18:1 | { ... } | Function g2 should return a value of type MyValue but does not return a value here | | test.cpp:52:1:52:1 | return ... | Function g7 should return a value of type MyValue but does not return a value here | | test.cpp:74:1:76:1 | { ... } | Function g10 should return a value of type second but does not return a value here | From d027e0c06b221d55612f03c6be62614ecd714fe8 Mon Sep 17 00:00:00 2001 From: Jeroen Ketema Date: Thu, 16 Jan 2025 13:12:20 +0100 Subject: [PATCH 3/5] C++: Add change note --- cpp/ql/lib/change-notes/2025-01-16-noreturn.md | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 cpp/ql/lib/change-notes/2025-01-16-noreturn.md diff --git a/cpp/ql/lib/change-notes/2025-01-16-noreturn.md b/cpp/ql/lib/change-notes/2025-01-16-noreturn.md new file mode 100644 index 000000000000..e1147d81dc72 --- /dev/null +++ b/cpp/ql/lib/change-notes/2025-01-16-noreturn.md @@ -0,0 +1,4 @@ +--- +category: minorAnalysis +--- +* `DefaultOptions::exits` now holds for C23 functions with the `_Noreturn` or `__Noreturn__` attribute. From 52eef7c4c276481d09c2db1bec5e36ac995227a6 Mon Sep 17 00:00:00 2001 From: Jeroen Ketema Date: Thu, 16 Jan 2025 13:19:54 +0100 Subject: [PATCH 4/5] C++: Fix typo in test --- cpp/ql/lib/DefaultOptions.qll | 6 +++--- .../test/query-tests/jsf/4.13 Functions/AV Rule 114/test.c | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/cpp/ql/lib/DefaultOptions.qll b/cpp/ql/lib/DefaultOptions.qll index 0b5aada91793..e4aa8d1f2d74 100644 --- a/cpp/ql/lib/DefaultOptions.qll +++ b/cpp/ql/lib/DefaultOptions.qll @@ -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`, `__noreturn__`, `_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__", "_Noreturn", "_noreturn"]) + f.getAnAttribute().hasName(["noreturn", "__noreturn__", "_Noreturn"]) or f.getASpecifier().hasName("noreturn") or diff --git a/cpp/ql/test/query-tests/jsf/4.13 Functions/AV Rule 114/test.c b/cpp/ql/test/query-tests/jsf/4.13 Functions/AV Rule 114/test.c index 841166253bf9..f0b2dff13308 100644 --- a/cpp/ql/test/query-tests/jsf/4.13 Functions/AV Rule 114/test.c +++ b/cpp/ql/test/query-tests/jsf/4.13 Functions/AV Rule 114/test.c @@ -129,7 +129,7 @@ int f23() { f22(); // GOOD } -[[___noreturn__]] void f24(); +[[__noreturn__]] void f24(); int f25() { f24(); // GOOD From ff0d4955cfa81f80a21a6a8540b631bf466d24cb Mon Sep 17 00:00:00 2001 From: Jeroen Ketema Date: Thu, 16 Jan 2025 17:55:45 +0100 Subject: [PATCH 5/5] C++: Fix change note --- cpp/ql/lib/change-notes/2025-01-16-noreturn.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cpp/ql/lib/change-notes/2025-01-16-noreturn.md b/cpp/ql/lib/change-notes/2025-01-16-noreturn.md index e1147d81dc72..a270b650d91d 100644 --- a/cpp/ql/lib/change-notes/2025-01-16-noreturn.md +++ b/cpp/ql/lib/change-notes/2025-01-16-noreturn.md @@ -1,4 +1,4 @@ --- category: minorAnalysis --- -* `DefaultOptions::exits` now holds for C23 functions with the `_Noreturn` or `__Noreturn__` attribute. +* `DefaultOptions::exits` now holds for C23 functions with the `_Noreturn` or `___Noreturn__` attribute.