Skip to content

Implement BannedAPIs package #909

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

Open
wants to merge 42 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
1844339
Add BannedAPIs package details
lcartey Jun 2, 2025
102703a
Rule 18.5.2: AvoidProgramTerminatingFunctions.ql
lcartey Jun 2, 2025
31039e3
RULE-18-5-2: Improve macro defined results
lcartey Jun 2, 2025
e9c0fe4
Remove cstdlib.h
lcartey Jun 2, 2025
58e43ad
Improve C++ stub headers for cstdarg
lcartey Jun 2, 2025
ffed467
Rule 21.10.1: NoVariadicFunctionMacros.ql
lcartey Jun 2, 2025
d3ec7b0
Remove redundant header stub file
lcartey Jun 3, 2025
aae01d0
Add csetjmp header
lcartey Jun 3, 2025
786f747
Add str* functions to cstdlib/stdlib.h headers
lcartey Jun 3, 2025
4ae6e32
Add strerror to cstring/string.h
lcartey Jun 3, 2025
1415a72
Add cwchar/wchar.h as stubs
lcartey Jun 3, 2025
c15b516
Add `stdint.h` as a header, and move cstdint definitions
lcartey Jun 3, 2025
ce58aff
Remove cstdint.h
lcartey Jun 3, 2025
99fa73b
Update cinttypes/inttypes.h
lcartey Jun 3, 2025
84697e6
Populate wint_t from wctype.h, and use it in wchar.h.
lcartey Jun 3, 2025
555fdec
Rule 21.2.2 - UnsafeStringHandlingFunctions.ql
lcartey Jun 3, 2025
c607798
Add a library to support the detection of banned functions
lcartey Jun 3, 2025
7a28f02
Rule 18.5.2 - Use BannedFunctions library
lcartey Jun 3, 2025
7fa6646
Rule 21.2.2 - use BannedFunction library
lcartey Jun 3, 2025
d33b4eb
Add `system` to cstdlib
lcartey Jun 3, 2025
efa017f
Rule 21.2.3 - BannedSystemFunction.ql
lcartey Jun 3, 2025
eccc416
Rule 23.11.1 - UseSmarPtrFactoryFunctions.ql
lcartey Jun 6, 2025
8a8c33d
Update C++ stubs for ctype.h/cctype and wctype.h/cwctype
lcartey Jun 6, 2025
b81423b
Improve C++ stubs for locales
lcartey Jun 6, 2025
cfceb9b
Add C++ string_view stub
lcartey Jun 6, 2025
52b97e6
Rule 24.5.1 - CharacterHandlingFunctionRestrictions.ql
lcartey Jun 6, 2025
ed16770
Rule 24.5.1 - improve structure/consistency of query
lcartey Jun 6, 2025
18e0143
Extend C++ stubs for locale
lcartey Jun 6, 2025
367a18a
Rule 25.5.1 - LocaleGlobalFunctionNotAllowed.ql
lcartey Jun 6, 2025
8485924
Rule 24-5-2 - NoMemoryFunctionsFromCString.ql
lcartey Jun 6, 2025
e26f32a
Rule 21.10.2 - NoCsetjmpHeader.ql
lcartey Jun 6, 2025
c603dba
Rule 21.10.1 - Formatting and reporting improvements
lcartey Jun 6, 2025
55cebdb
Move Rule-6-9-2 to FixedWidthInt.
lcartey Jun 6, 2025
54fe5ea
A3-9-1: Convert to shared query
lcartey Jun 10, 2025
5b37d13
Rule 6.9.2: AvoidStandardIntegerTypeNames.ql
lcartey Jun 10, 2025
f43336a
VariableWidthIntegerTypesUsed - support function return types
lcartey Jun 10, 2025
6791c0c
VariableWidthType: return type test results accepted
lcartey Jul 7, 2025
a2de810
Remove unneeded #pragma
lcartey Jul 7, 2025
516ef74
Rule 23.11.1: Address review comments
lcartey Jul 7, 2025
215a96c
Rule 21.10.1: Support other uses of va_list
lcartey Jul 7, 2025
d0c65bd
Rule 21.10.2: Detect more complex type use of jmp_buf
lcartey Jul 7, 2025
6856e11
BannedAPIs: Add tags
lcartey Jul 7, 2025
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
Prev Previous commit
Next Next commit
Rule 25.5.1 - LocaleGlobalFunctionNotAllowed.ql
Add query for banned locale functions. [a]
  • Loading branch information
lcartey committed Jun 6, 2025
commit 367a18a889055f3f6ef7fd3dce12499ecbad84cd
28 changes: 28 additions & 0 deletions cpp/misra/src/rules/RULE-25-5-1/LocaleGlobalFunctionNotAllowed.ql
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* @id cpp/misra/locale-global-function-not-allowed
* @name RULE-25-5-1: The setlocale and std::locale::global functions shall not be called
* @description Calling setlocale or std::locale::global functions can introduce data races with
* functions that use the locale, leading to undefined behavior.
* @kind problem
* @precision very-high
* @problem.severity error
* @tags external/misra/id/rule-25-5-1
* scope/single-translation-unit
* external/misra/enforcement/decidable
* external/misra/obligation/required
*/

import cpp
import codingstandards.cpp.misra
import codingstandards.cpp.BannedFunctions

class BannedLocaleFunction extends Function {
BannedLocaleFunction() {
this.hasGlobalOrStdName("setlocale") or
this.hasQualifiedName("std", "locale", "global")
}
}

from BannedFunctions<BannedLocaleFunction>::Use use
where not isExcluded(use, BannedAPIsPackage::localeGlobalFunctionNotAllowedQuery())
select use, use.getAction() + " banned function '" + use.getFunctionName() + "' from <locale>."
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
| test.cpp:6:3:6:16 | call to setlocale | Call to banned function 'setlocale' from <locale>. |
| test.cpp:7:3:7:16 | call to setlocale | Call to banned function 'setlocale' from <locale>. |
| test.cpp:8:3:8:16 | call to setlocale | Call to banned function 'setlocale' from <locale>. |
| test.cpp:12:3:12:21 | call to global | Call to banned function 'global' from <locale>. |
| test.cpp:13:3:13:21 | call to global | Call to banned function 'global' from <locale>. |
| test.cpp:36:5:36:18 | call to setlocale | Call to banned function 'setlocale' from <locale>. |
| test.cpp:40:5:40:18 | call to setlocale | Call to banned function 'setlocale' from <locale>. |
| test.cpp:45:3:45:21 | call to global | Call to banned function 'global' from <locale>. |
| test.cpp:46:3:46:21 | call to global | Call to banned function 'global' from <locale>. |
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rules/RULE-25-5-1/LocaleGlobalFunctionNotAllowed.ql
47 changes: 47 additions & 0 deletions cpp/misra/test/rules/RULE-25-5-1/test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include <cctype>
#include <clocale>
#include <locale>

void test_setlocale_call() {
std::setlocale(LC_ALL, "C"); // NON_COMPLIANT
std::setlocale(LC_NUMERIC, "C"); // NON_COMPLIANT
std::setlocale(LC_TIME, "en_US.UTF-8"); // NON_COMPLIANT
}

void test_locale_global_call() {
std::locale::global(std::locale("C")); // NON_COMPLIANT
std::locale::global(std::locale::classic()); // NON_COMPLIANT
}

void test_compliant_locale_usage() {
wchar_t l1 = L'\u2002';
std::locale l2("C");

if (std::isspace(l1, l2)) { // COMPLIANT
}
if (std::isalpha(l1, l2)) { // COMPLIANT
}
if (std::isdigit(l1, l2)) { // COMPLIANT
}
}

void test_compliant_locale_construction() {
std::locale l3("C"); // COMPLIANT
std::locale l4 = std::locale::classic(); // COMPLIANT
std::locale l5; // COMPLIANT
}

void test_nested_setlocale_calls() {
if (true) {
std::setlocale(LC_ALL, "ja_JP.utf8"); // NON_COMPLIANT
}

for (int l6 = 0; l6 < 1; ++l6) {
std::setlocale(LC_CTYPE, "C"); // NON_COMPLIANT
}
}

void test_locale_global_with_different_locales() {
std::locale::global(std::locale("en_US.UTF-8")); // NON_COMPLIANT
std::locale::global(std::locale("ja_JP.utf8")); // NON_COMPLIANT
}