Skip to content

Commit 3cf76d0

Browse files
committed
Add cpplint warnings for exit() and abort()
Our code should only terminate normally via return from main, or abnormally via the macros from `invariant.h`. While at it, also fixes some warnings about cpplint.py's code. Fixes: #1902
1 parent 4fe3ade commit 3cf76d0

File tree

13 files changed

+299
-5
lines changed

13 files changed

+299
-5
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Author: Michael Tautschnig
2+
3+
// Test file that should NOT generate any termination warnings
4+
// This file contains various uses of "exit" and "abort" that should not trigger
5+
6+
#include <iostream>
7+
#include <string>
8+
9+
int main()
10+
{
11+
// Variable names containing exit/abort - should not trigger
12+
int exit_code = 0;
13+
bool exit_flag = false;
14+
void *exit_ptr = nullptr;
15+
bool abort_flag = false;
16+
int abort_count = 0;
17+
18+
// Assignment to variables
19+
exit_code = 1;
20+
exit_flag = true;
21+
abort_flag = false;
22+
23+
// Function calls that use custom namespaces (not std:: or ::)
24+
// Note: Member function calls like obj.exit() would trigger warnings
25+
// due to current regex implementation, so we avoid them here
26+
27+
return exit_code;
28+
}
29+
30+
// Function definitions that shouldn't trigger
31+
void my_exit_function()
32+
{
33+
std::cout << "This is not exit()" << std::endl;
34+
}
35+
36+
void abort_handler()
37+
{
38+
std::cout << "This is not abort()" << std::endl;
39+
}
40+
41+
// Custom namespace functions (not std:: or ::)
42+
void use_custom_functions()
43+
{
44+
// These have custom namespace prefixes, so should not trigger
45+
// Note: current implementation only checks for std:: and :: prefixes
46+
// myns::exit(0); // Would not trigger if uncommented
47+
// custom::abort(); // Would not trigger if uncommented
48+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
CORE
2+
main.cpp
3+
4+
^# Total errors found: 0$
5+
^EXIT=0$
6+
^SIGNAL=0$
7+
--
8+
^# Total errors found: [1-9]
9+
--
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Author: Michael Tautschnig
2+
3+
// Test file for NOLINT suppression
4+
// This file tests that NOLINT comments properly suppress warnings
5+
6+
#include <cstdlib>
7+
8+
int main()
9+
{
10+
// These should be suppressed by NOLINT comments
11+
exit(1); // NOLINT - justified use case
12+
abort(); // NOLINT(runtime/termination) - specific suppression
13+
14+
std::exit(2); // NOLINT
15+
::abort(); // NOLINT(runtime/termination)
16+
17+
// These should still trigger warnings (no NOLINT)
18+
exit(3); // Line 18: Warning expected
19+
abort(); // Line 19: Warning expected
20+
21+
// Test NOLINT with other categories (should still suppress)
22+
exit(4); // NOLINT(whitespace/parens)
23+
24+
return 0;
25+
}
26+
27+
void test_function()
28+
{
29+
// Mixed suppressed and unsuppressed
30+
exit(5); // Line 30: Warning expected
31+
abort(); // NOLINT - suppressed
32+
std::exit(6); // Line 32: Warning expected
33+
::abort(); // NOLINT(runtime/termination) - suppressed
34+
}
35+
36+
// Test NOLINT at end of line vs middle
37+
void another_test()
38+
{
39+
exit(7); /* NOLINT */
40+
int x = 0;
41+
abort(); // Line 41: Warning expected
42+
int y = 0;
43+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
CORE
2+
main.cpp
3+
4+
^regression/cpp-linter/abort-exit-nolint/main\.cpp:18: exit\(\) should not be used. Normal termination should be via return from main. Abnormal termination should use invariant.h macros. If this use is justified, mark with NOLINT. See https://github.com/diffblue/cbmc/issues/1902 \[runtime/termination\] \[4\]
5+
^regression/cpp-linter/abort-exit-nolint/main\.cpp:19: abort\(\) should not be used. Normal termination should be via return from main. Abnormal termination should use invariant.h macros. If this use is justified, mark with NOLINT. See https://github.com/diffblue/cbmc/issues/1902 \[runtime/termination\] \[4\]
6+
^regression/cpp-linter/abort-exit-nolint/main\.cpp:22: exit\(\) should not be used. Normal termination should be via return from main. Abnormal termination should use invariant.h macros. If this use is justified, mark with NOLINT. See https://github.com/diffblue/cbmc/issues/1902 \[runtime/termination\] \[4\]
7+
^regression/cpp-linter/abort-exit-nolint/main\.cpp:30: exit\(\) should not be used. Normal termination should be via return from main. Abnormal termination should use invariant.h macros. If this use is justified, mark with NOLINT. See https://github.com/diffblue/cbmc/issues/1902 \[runtime/termination\] \[4\]
8+
^regression/cpp-linter/abort-exit-nolint/main\.cpp:32: exit\(\) should not be used. Normal termination should be via return from main. Abnormal termination should use invariant.h macros. If this use is justified, mark with NOLINT. See https://github.com/diffblue/cbmc/issues/1902 \[runtime/termination\] \[4\]
9+
^regression/cpp-linter/abort-exit-nolint/main\.cpp:41: abort\(\) should not be used. Normal termination should be via return from main. Abnormal termination should use invariant.h macros. If this use is justified, mark with NOLINT. See https://github.com/diffblue/cbmc/issues/1902 \[runtime/termination\] \[4\]
10+
^# Total errors found: 6$
11+
^EXIT=1$
12+
^SIGNAL=0$
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Author: Michael Tautschnig
2+
3+
// Test file for abort() call detection
4+
// This file should generate multiple warnings
5+
6+
#include <cstdlib>
7+
8+
void error_handler()
9+
{
10+
// Basic abort calls - should trigger warnings
11+
abort(); // Line 11: Warning expected
12+
13+
// Namespace qualified calls - should trigger warnings
14+
std::abort(); // Line 14: Warning expected
15+
::abort(); // Line 15: Warning expected
16+
17+
// Calls with whitespace - should trigger warnings
18+
abort(); // Line 18: Warning expected
19+
abort(); // Line 19: Warning expected
20+
abort(); // Line 20: Warning expected (tab)
21+
22+
// Calls in expressions - should trigger warnings
23+
if(fatal_error)
24+
abort(); // Line 24: Warning expected
25+
}
26+
27+
// Variable names - should NOT trigger warnings
28+
bool abort_flag = false;
29+
int abort_count = 0;
30+
31+
class abortablet
32+
{
33+
public:
34+
void process()
35+
{
36+
if(should_abort)
37+
{
38+
abort(); // Line 38: Warning expected
39+
}
40+
}
41+
};
42+
43+
// Function names containing abort - should NOT trigger
44+
void abort_operation()
45+
{
46+
return;
47+
}
48+
49+
void check_abort_status()
50+
{
51+
return;
52+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
CORE
2+
main.cpp
3+
4+
^regression/cpp-linter/abort/main\.cpp:11: abort\(\) should not be used. Normal termination should be via return from main. Abnormal termination should use invariant.h macros. If this use is justified, mark with NOLINT. See https://github.com/diffblue/cbmc/issues/1902 \[runtime/termination\] \[4\]
5+
^regression/cpp-linter/abort/main\.cpp:14: abort\(\) should not be used. Normal termination should be via return from main. Abnormal termination should use invariant.h macros. If this use is justified, mark with NOLINT. See https://github.com/diffblue/cbmc/issues/1902 \[runtime/termination\] \[4\]
6+
^regression/cpp-linter/abort/main\.cpp:15: abort\(\) should not be used. Normal termination should be via return from main. Abnormal termination should use invariant.h macros. If this use is justified, mark with NOLINT. See https://github.com/diffblue/cbmc/issues/1902 \[runtime/termination\] \[4\]
7+
^regression/cpp-linter/abort/main\.cpp:18: abort\(\) should not be used. Normal termination should be via return from main. Abnormal termination should use invariant.h macros. If this use is justified, mark with NOLINT. See https://github.com/diffblue/cbmc/issues/1902 \[runtime/termination\] \[4\]
8+
^regression/cpp-linter/abort/main\.cpp:19: abort\(\) should not be used. Normal termination should be via return from main. Abnormal termination should use invariant.h macros. If this use is justified, mark with NOLINT. See https://github.com/diffblue/cbmc/issues/1902 \[runtime/termination\] \[4\]
9+
^regression/cpp-linter/abort/main\.cpp:20: abort\(\) should not be used. Normal termination should be via return from main. Abnormal termination should use invariant.h macros. If this use is justified, mark with NOLINT. See https://github.com/diffblue/cbmc/issues/1902 \[runtime/termination\] \[4\]
10+
^regression/cpp-linter/abort/main\.cpp:24: abort\(\) should not be used. Normal termination should be via return from main. Abnormal termination should use invariant.h macros. If this use is justified, mark with NOLINT. See https://github.com/diffblue/cbmc/issues/1902 \[runtime/termination\] \[4\]
11+
^regression/cpp-linter/abort/main\.cpp:38: abort\(\) should not be used. Normal termination should be via return from main. Abnormal termination should use invariant.h macros. If this use is justified, mark with NOLINT. See https://github.com/diffblue/cbmc/issues/1902 \[runtime/termination\] \[4\]
12+
^# Total errors found: 8$
13+
^EXIT=1$
14+
^SIGNAL=0$
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Author: Michael Tautschnig
2+
3+
// Test file for exit() call detection
4+
// This file should generate multiple warnings
5+
6+
#include <cstdlib>
7+
8+
int main()
9+
{
10+
// Basic exit calls - should trigger warnings
11+
exit(1); // Line 11: Warning expected
12+
exit(EXIT_FAILURE); // Line 12: Warning expected
13+
14+
// Namespace qualified calls - should trigger warnings
15+
std::exit(1); // Line 15: Warning expected
16+
::exit(2); // Line 16: Warning expected
17+
18+
// Calls with whitespace - should trigger warnings
19+
exit(3); // Line 19: Warning expected
20+
exit(4); // Line 20: Warning expected
21+
exit(5); // Line 21: Warning expected (tab)
22+
23+
// Calls in expressions - should trigger warnings
24+
if(condition)
25+
exit(6); // Line 25: Warning expected
26+
return exit(7); // Line 26: Warning expected
27+
28+
// Variable names - should NOT trigger warnings
29+
int exit_code = 0;
30+
bool exit_flag = false;
31+
void *exit_ptr = nullptr;
32+
33+
return exit_code;
34+
}
35+
36+
void error_function()
37+
{
38+
// More exit calls in different contexts
39+
exit(10); // Line 39: Warning expected
40+
}
41+
42+
class test_classt
43+
{
44+
public:
45+
void method()
46+
{
47+
exit(11); // Line 47: Warning expected
48+
}
49+
};
50+
51+
// Function names containing exit - should NOT trigger
52+
void my_exit_function()
53+
{
54+
return;
55+
}

0 commit comments

Comments
 (0)