Skip to content

Commit 3771240

Browse files
committed
DIR-4-7: Create shared query for unchecked return values
Create a new shared query from the implementation of M0-3-2, which detects cases where error checking has not occurred after a call to a standard C library function.
1 parent dcb32c1 commit 3771240

File tree

11 files changed

+122
-51
lines changed

11 files changed

+122
-51
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
| test.c:16:3:16:8 | call to remove | Return value is not tested for errors. |
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// GENERATED FILE - DO NOT MODIFY
2+
import codingstandards.cpp.rules.functionerroneousreturnvaluenottested.FunctionErroneousReturnValueNotTested
3+
4+
class TestFileQuery extends FunctionErroneousReturnValueNotTestedSharedQuery, TestQuery { }
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* @id c/misra/function-error-information-untested
3+
* @name DIR-4-7: If a function generates error information, then that error information shall be tested
4+
* @description A function (whether it is part of the standard library, a third party library or a
5+
* user defined function) may provide some means of indicating the occurrence of an
6+
* error. This may be via a global error flag, a parametric error flag, a special
7+
* return value or some other means. Whenever such a mechanism is provided by a
8+
* function the calling program shall check for the indication of an error as soon as
9+
* the function returns.
10+
* @kind problem
11+
* @precision very-high
12+
* @problem.severity recommendation
13+
* @tags external/misra/id/dir-4-7
14+
* maintainability
15+
* external/misra/obligation/required
16+
*/
17+
18+
import cpp
19+
import codingstandards.c.misra
20+
import codingstandards.cpp.rules.functionerroneousreturnvaluenottested.FunctionErroneousReturnValueNotTested
21+
22+
class FunctionErrorInformationUntestedQuery extends FunctionErroneousReturnValueNotTestedSharedQuery {
23+
FunctionErrorInformationUntestedQuery() {
24+
this = ContractsPackage::functionErrorInformationUntestedQuery()
25+
}
26+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
c/common/test/rules/functionerroneousreturnvaluenottested/FunctionErroneousReturnValueNotTested.ql

cpp/autosar/src/rules/M0-3-2/FunctionErroneousReturnValueNotTested.ql

Lines changed: 7 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -19,54 +19,11 @@
1919

2020
import cpp
2121
import codingstandards.cpp.autosar
22-
import codingstandards.cpp.dataflow.DataFlow
23-
import semmle.code.cpp.controlflow.Guards
22+
import codingstandards.cpp.rules.functionerroneousreturnvaluenottested.FunctionErroneousReturnValueNotTested
2423

25-
from FunctionCall fc
26-
where
27-
not isExcluded(fc, ExpressionsPackage::functionErroneousReturnValueNotTestedQuery()) and
28-
fc.getTarget()
29-
.hasGlobalOrStdName([
30-
// fcntl.h
31-
"open", "openat", "fcntl", "creat",
32-
// locale.h
33-
"setlocale",
34-
// stdlib.h
35-
"system", "getenv", "getenv_s",
36-
// signal.h
37-
"signal", "raise",
38-
// setjmp.h
39-
"setjmp",
40-
// stdio.h
41-
"fopen", "fopen_s", "freopen", "freopen_s", "fclose", "fcloseall", "fflush", "setvbuf",
42-
"fgetc", "getc", "fgets", "fputc", "getchar", "gets", "gets_s", "putchar", "puts",
43-
"ungetc", "scanf", "fscanf", "sscanf", "scanf_s", "fscanf_s", "sscanf_s", "vscanf",
44-
"vfscanf", "vsscanf", "vscanf_s", "vfscanf_s", "vsscanf_s", "printf", "fprintf",
45-
"sprintf", "snprintf", "printf_s", "fprintf_s", "sprintf_s", "snprintf_s", "vprintf",
46-
"vfprintf", "vsprintf", "vsnprintf", "vprintf_s", "vfprintf_s", "vsprintf_s",
47-
"vsnprintf_s", "ftell", "fgetpos", "fseek", "fsetpos", "remove", "rename", "tmpfile",
48-
"tmpfile_s", "tmpnam", "tmpnam_s",
49-
// string.h
50-
"strcpy_s", "strncpy_s", "strcat_s", "strncat_s", "memset_s", "memcpy_s", "memmove_s",
51-
"strerror_s",
52-
// threads.h
53-
"thrd_create", "thrd_sleep", "thrd_detach", "thrd_join", "mtx_init", "mtx_lock",
54-
"mtx_timedlock", "mtx_trylock", "mtx_unlock", "cnd_init", "cnd_signal", "cnd_broadcast",
55-
"cnd_wait", "cnd_timedwait", "tss_create", "tss_get", "tss_set",
56-
// time.h
57-
"time", "clock", "timespec_get", "asctime_s", "ctime_s", "gmtime", "gmtime_s",
58-
"localtime", "localtime_s",
59-
// unistd.h
60-
"write", "read", "close", "unlink",
61-
// wchar.h
62-
"fgetwc", "getwc", "fgetws", "fputwc", "putwc", "fputws", "getwchar", "putwchar",
63-
"ungetwc", "wscanf", "fwscanf", "swscanf", "wscanf_s", "fwscanf_s", "swscanf_s",
64-
"vwscanf", "vfwscanf", "vswscanf", "vwscanf_s", "vfwscanf_s", "vswscanf_s", "wprintf",
65-
"fwprintf", "swprintf", "wprintf_s", "fwprintf_s", "swprintf_s", "snwprintf_s",
66-
"vwprintf", "vfwprintf", "vswprintf", "vwprintf_s", "vfwprintf_s", "vswprintf_s",
67-
"vsnwprintf_s"
68-
]) and
69-
forall(GuardCondition gc |
70-
not DataFlow::localFlow(DataFlow::exprNode(fc), DataFlow::exprNode(gc.getAChild*()))
71-
)
72-
select fc, "Return value is not tested for errors."
24+
class FunctionErrorInformationUntestedQuery extends FunctionErroneousReturnValueNotTestedSharedQuery
25+
{
26+
FunctionErrorInformationUntestedQuery() {
27+
this = ExpressionsPackage::functionErroneousReturnValueNotTestedQuery()
28+
}
29+
}

cpp/autosar/test/rules/M0-3-2/FunctionErroneousReturnValueNotTested.qlref

Lines changed: 0 additions & 1 deletion
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/**
2+
* Provides a library which includes a `problems` predicate for reporting unchecked error values.
3+
*/
4+
5+
import cpp
6+
import codingstandards.cpp.Customizations
7+
import codingstandards.cpp.dataflow.DataFlow
8+
import semmle.code.cpp.controlflow.Guards
9+
import codingstandards.cpp.Exclusions
10+
11+
abstract class FunctionErroneousReturnValueNotTestedSharedQuery extends Query { }
12+
13+
Query getQuery() { result instanceof FunctionErroneousReturnValueNotTestedSharedQuery }
14+
15+
query predicate problems(FunctionCall fc, string message) {
16+
not isExcluded(fc, getQuery()) and
17+
fc.getTarget()
18+
.hasGlobalOrStdName([
19+
// fcntl.h
20+
"open", "openat", "fcntl", "creat",
21+
// locale.h
22+
"setlocale",
23+
// stdlib.h
24+
"system", "getenv", "getenv_s",
25+
// signal.h
26+
"signal", "raise",
27+
// setjmp.h
28+
"setjmp",
29+
// stdio.h
30+
"fopen", "fopen_s", "freopen", "freopen_s", "fclose", "fcloseall", "fflush", "setvbuf",
31+
"fgetc", "getc", "fgets", "fputc", "getchar", "gets", "gets_s", "putchar", "puts",
32+
"ungetc", "scanf", "fscanf", "sscanf", "scanf_s", "fscanf_s", "sscanf_s", "vscanf",
33+
"vfscanf", "vsscanf", "vscanf_s", "vfscanf_s", "vsscanf_s", "printf", "fprintf",
34+
"sprintf", "snprintf", "printf_s", "fprintf_s", "sprintf_s", "snprintf_s", "vprintf",
35+
"vfprintf", "vsprintf", "vsnprintf", "vprintf_s", "vfprintf_s", "vsprintf_s",
36+
"vsnprintf_s", "ftell", "fgetpos", "fseek", "fsetpos", "remove", "rename", "tmpfile",
37+
"tmpfile_s", "tmpnam", "tmpnam_s",
38+
// string.h
39+
"strcpy_s", "strncpy_s", "strcat_s", "strncat_s", "memset_s", "memcpy_s", "memmove_s",
40+
"strerror_s",
41+
// threads.h
42+
"thrd_create", "thrd_sleep", "thrd_detach", "thrd_join", "mtx_init", "mtx_lock",
43+
"mtx_timedlock", "mtx_trylock", "mtx_unlock", "cnd_init", "cnd_signal", "cnd_broadcast",
44+
"cnd_wait", "cnd_timedwait", "tss_create", "tss_get", "tss_set",
45+
// time.h
46+
"time", "clock", "timespec_get", "asctime_s", "ctime_s", "gmtime", "gmtime_s",
47+
"localtime", "localtime_s",
48+
// unistd.h
49+
"write", "read", "close", "unlink",
50+
// wchar.h
51+
"fgetwc", "getwc", "fgetws", "fputwc", "putwc", "fputws", "getwchar", "putwchar",
52+
"ungetwc", "wscanf", "fwscanf", "swscanf", "wscanf_s", "fwscanf_s", "swscanf_s",
53+
"vwscanf", "vfwscanf", "vswscanf", "vwscanf_s", "vfwscanf_s", "vswscanf_s", "wprintf",
54+
"fwprintf", "swprintf", "wprintf_s", "fwprintf_s", "swprintf_s", "snwprintf_s",
55+
"vwprintf", "vfwprintf", "vswprintf", "vwprintf_s", "vfwprintf_s", "vswprintf_s",
56+
"vsnwprintf_s"
57+
]) and
58+
forall(GuardCondition gc |
59+
not DataFlow::localFlow(DataFlow::exprNode(fc), DataFlow::exprNode(gc.getAChild*()))
60+
) and
61+
message = "Return value is not tested for errors."
62+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// GENERATED FILE - DO NOT MODIFY
2+
import codingstandards.cpp.rules.functionerroneousreturnvaluenottested.FunctionErroneousReturnValueNotTested
3+
4+
class TestFileQuery extends FunctionErroneousReturnValueNotTestedSharedQuery, TestQuery { }

0 commit comments

Comments
 (0)