Skip to content

Commit

Permalink
Address feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelRFairhurst committed Sep 27, 2024
1 parent 778db73 commit c8e5091
Show file tree
Hide file tree
Showing 13 changed files with 86 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
| test.c:9:16:9:31 | test_noreturn_f2 | The function test_noreturn_f2 declared with attribute _Noreturn returns a value. |
| test.c:34:16:34:31 | test_noreturn_f5 | The function test_noreturn_f5 declared with attribute _Noreturn returns a value. |
| test.c:49:32:49:47 | test_noreturn_f7 | The function test_noreturn_f7 declared with attribute _Noreturn returns a value. |
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// GENERATED FILE - DO NOT MODIFY
import codingstandards.cpp.rules.functionnoreturnattributecondition.FunctionNoReturnAttributeCondition

class TestFileQuery extends FunctionNoReturnAttributeConditionSharedQuery, TestQuery { }
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#include "stdlib.h"
#include "threads.h"
#include "setjmp.h"

_Noreturn void test_noreturn_f1(int i) { // COMPLIANT
abort();
Expand Down Expand Up @@ -52,4 +54,30 @@ __attribute__((noreturn)) void test_noreturn_f7(int i) { // NON_COMPLIANT

__attribute__((noreturn)) void test_noreturn_f8(int i) { // COMPLIANT
abort();
}

_Noreturn void test_noreturn_f9(int i) { // COMPLIANT
test_noreturn_f1(i);
}

_Noreturn void test_noreturn_f10(int i) { // COMPLIANT
switch(i) {
case 0:
abort(); break;
case 1:
exit(0); break;
case 2:
_Exit(0); break;
case 3:
quick_exit(0); break;
case 4:
thrd_exit(0); break;
default:
jmp_buf jb;
longjmp(jb, 0);
}
}

_Noreturn void test_noreturn_f11(int i) { // COMPLIANT
return test_noreturn_f11(i);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,20 @@
* @precision very-high
* @problem.severity recommendation
* @tags external/misra/id/rule-17-10
* correctness
* external/misra/obligation/required
*/

import cpp
import codingstandards.c.misra
import codingstandards.c.Noreturn
import codingstandards.cpp.Noreturn

from NoreturnFunction f, Type returnType
where
not isExcluded(f, NoReturnPackage::nonVoidReturnTypeOfNoreturnFunctionQuery()) and
returnType = f.getType() and
not returnType instanceof VoidType
not returnType instanceof VoidType and
not f.isCompilerGenerated()
select f,
"The function " + f.getName() + " is declared _noreturn but has a return type of " +
returnType.toString() + "."
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,21 @@
* @precision high
* @problem.severity recommendation
* @tags external/misra/id/rule-17-11
* correctness
* external/misra/obligation/advisory
*/

import cpp
import codingstandards.c.misra
import codingstandards.c.Noreturn
import codingstandards.cpp.Noreturn

from Function f
where
not isExcluded(f, NoReturnPackage::returnStatementInNoreturnFunctionQuery()) and
not f instanceof NoreturnFunction and
not mayReturn(f) and
f.hasDefinition() and
f.getName() != "main" // Allowed exception; _Noreturn main() is undefined behavior.
not f.getName() = "main" and // Allowed exception; _Noreturn main() is undefined behavior.
not f.isCompilerGenerated()
select f,
"The function " + f.getName() + " cannot return and should be declared attribute _Noreturn."
13 changes: 7 additions & 6 deletions c/misra/src/rules/RULE-17-9/ReturnStatementInNoreturnFunction.ql
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@
* @precision very-high
* @problem.severity error
* @tags external/misra/id/rule-17-9
* correctness
* external/misra/obligation/mandatory
*/

import cpp
import codingstandards.c.misra
import codingstandards.c.Noreturn
import codingstandards.cpp.rules.functionnoreturnattributecondition.FunctionNoReturnAttributeCondition

from NoreturnFunction f
where
not isExcluded(f, NoReturnPackage::returnStatementInNoreturnFunctionQuery()) and
mayReturn(f)
select f, "The function " + f.getName() + " declared with attribute _Noreturn returns a value."
class ReturnStatementInNoreturnFunctionQuery extends FunctionNoReturnAttributeConditionSharedQuery {
ReturnStatementInNoreturnFunctionQuery() {
this = NoReturnPackage::returnStatementInNoreturnFunctionQuery()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
| test.c:18:6:18:21 | test_noreturn_f4 | The function test_noreturn_f4 cannot return and should be declared attribute _Noreturn. |
| test.c:47:6:47:21 | test_noreturn_f8 | The function test_noreturn_f8 cannot return and should be declared attribute _Noreturn. |
| test.c:63:6:63:22 | test_noreturn_f10 | The function test_noreturn_f10 cannot return and should be declared attribute _Noreturn. |
| test.c:97:6:97:22 | test_noreturn_f15 | The function test_noreturn_f15 cannot return and should be declared attribute _Noreturn. |
| test.c:101:6:101:22 | test_noreturn_f16 | The function test_noreturn_f16 cannot return and should be declared attribute _Noreturn. |
13 changes: 13 additions & 0 deletions c/misra/test/rules/RULE-17-11/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,17 @@ __attribute__((noreturn)) void test_noreturn_f13(int i) { // COMPLIANT
// noreturn.
int main(char **argv, int argc) { // COMPLIANT
abort();
}

_Noreturn void test_noreturn_f14(int i) { // COMPLIANT
test_noreturn_f1(i);
}

void test_noreturn_f15(int i) { // NON_COMPLIANT
test_noreturn_f1(i);
}

void test_noreturn_f16(int i) { // NON_COMPLIANT
// Infinite tail recursion
test_noreturn_f16(i);
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
c/common/test/rules/functionnoreturnattributecondition/FunctionNoReturnAttributeCondition.ql
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,29 @@
import cpp
import codingstandards.cpp.Customizations
import codingstandards.cpp.Exclusions
import codingstandards.cpp.Noreturn

abstract class FunctionNoReturnAttributeConditionSharedQuery extends Query { }

Query getQuery() { result instanceof FunctionNoReturnAttributeConditionSharedQuery }

/**
* `noreturn` functions are declared differently in c/c++. Attempt to match
* the description to the file; low risk if it chooses incorrectly.
*/
string describeNoreturn(Function f) {
if f.getFile().getExtension() = ["c", "C", "h", "H"]
then result = "_Noreturn"
else result = "[[noreturn]]"
}

/**
* This checks that the return statement is reachable from the function entry point
*/
query predicate problems(Function f, string message) {
query predicate problems(NoreturnFunction f, string message) {
not isExcluded(f, getQuery()) and
f.getAnAttribute().getName() = "noreturn" and
exists(ReturnStmt s |
f = s.getEnclosingFunction() and
s.getBasicBlock().isReachable()
) and
message = "The function " + f.getName() + " declared with attribute [[noreturn]] returns a value."
mayReturn(f) and
not f.isCompilerGenerated() and
message =
"The function " + f.getName() + " declared with attribute " + describeNoreturn(f) + " returns a value."
}
7 changes: 4 additions & 3 deletions rule_packages/c/NoReturn.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"precision": "very-high",
"severity": "recommendation",
"short_name": "NonVoidReturnTypeOfNoreturnFunction",
"tags": []
"tags": ["correctness"]
}
],
"title": "A function declared with _noreturn shall have a return type of void"
Expand All @@ -29,7 +29,7 @@
"precision": "high",
"severity": "recommendation",
"short_name": "FunctionWithNoReturningBranchShouldBeNoreturn",
"tags": []
"tags": ["correctness"]
}
],
"title": "A function without a branch that returns shall be declared with _Noreturn"
Expand All @@ -46,7 +46,8 @@
"precision": "very-high",
"severity": "error",
"short_name": "ReturnStatementInNoreturnFunction",
"tags": []
"tags": ["correctness"],
"shared_implementation_short_name": "FunctionNoReturnAttributeCondition"
}
],
"title": "Verify that a function declared with _Noreturn does not return"
Expand Down

0 comments on commit c8e5091

Please sign in to comment.