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 36 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 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
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
2 changes: 2 additions & 0 deletions change_notes/2025-06-10-a3-9-1-functions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- `A3-9-1` - `VariableWidthIntegerTypesUsed.ql`:
- This query now reports the use of non-fixed width integer types in function return types, with the exception of `char` types and for `main` functions.
2 changes: 1 addition & 1 deletion cpp/autosar/src/codingstandards/cpp/CommonTypes.qll
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import cpp as default

/*
* Implementations of the C/C++ Fixed Width Types from cstdint.h.
* Implementations of the C/C++ Fixed Width Types from cstdint.
*
* TODO: Deprecate once this is available in the CodeQL standard library.
*/
Expand Down
28 changes: 6 additions & 22 deletions cpp/autosar/src/rules/A3-9-1/VariableWidthIntegerTypesUsed.ql
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,10 @@

import cpp
import codingstandards.cpp.autosar
import codingstandards.cpp.EncapsulatingFunctions
import codingstandards.cpp.BuiltInNumericTypes
import codingstandards.cpp.Type
import codingstandards.cpp.Operator
import codingstandards.cpp.rules.variablewidthintegertypesused.VariableWidthIntegerTypesUsed

from Variable v, Type typeStrippedOfSpecifiers
where
not isExcluded(v, DeclarationsPackage::variableWidthIntegerTypesUsedQuery()) and
typeStrippedOfSpecifiers = stripSpecifiers(v.getType()) and
(
typeStrippedOfSpecifiers instanceof BuiltInIntegerType or
typeStrippedOfSpecifiers instanceof UnsignedCharType or
typeStrippedOfSpecifiers instanceof SignedCharType
) and
not v instanceof ExcludedVariable and
// Dont consider template instantiations because instantiations with
// Fixed Width Types are recorded after stripping their typedef'd type,
// thereby, causing false positives (#540).
not v.isFromTemplateInstantiation(_) and
//post-increment/post-decrement operators are required by the standard to have a dummy int parameter
not v.(Parameter).getFunction() instanceof PostIncrementOperator and
not v.(Parameter).getFunction() instanceof PostDecrementOperator
select v, "Variable '" + v.getName() + "' has variable-width type."
class VariableWidthIntegerTypesUsedQuery extends VariableWidthIntegerTypesUsedSharedQuery {
VariableWidthIntegerTypesUsedQuery() {
this = DeclarationsPackage::variableWidthIntegerTypesUsedQuery()
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
cpp/common/test/rules/variablewidthintegertypesused/VariableWidthIntegerTypesUsed.ql
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
| test.cpp:4:8:4:8 | c | Variable 'c' has variable-width char type. |
| test.cpp:38:14:38:15 | c1 | Variable 'c1' has variable-width char type. |
| test.cpp:56:17:56:18 | c2 | Variable 'c2' has variable-width char type. |
| test.cpp:10:14:10:15 | c1 | Variable 'c1' has variable-width char type. |
| test.cpp:14:17:14:18 | c2 | Variable 'c2' has variable-width char type. |
86 changes: 7 additions & 79 deletions cpp/autosar/test/rules/A3-9-1/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,88 +2,16 @@

void test_variable_width_type_variables() {
char c; // NON_COMPLIANT
unsigned char uc; // NON_COMPLIANT
signed char sc; // NON_COMPLIANT

int i; // NON_COMPLIANT
unsigned int ui; // NON_COMPLIANT
unsigned u; // NON_COMPLIANT
signed int si; // NON_COMPLIANT
signed s; // NON_COMPLIANT

short sh; // NON_COMPLIANT
unsigned short ush; // NON_COMPLIANT
signed short ssh; // NON_COMPLIANT

long l; // NON_COMPLIANT
unsigned long ul; // NON_COMPLIANT
signed long sl; // NON_COMPLIANT

std::int8_t i8; // COMPLIANT
std::int16_t i16; // COMPLIANT
std::int32_t i32; // COMPLIANT
std::int64_t i64; // COMPLIANT

std::uint8_t u8; // COMPLIANT
std::uint16_t u16; // COMPLIANT
std::uint32_t u32; // COMPLIANT
std::uint64_t u64; // COMPLIANT
}

int main(int argc, char *argv[]) { // COMPLIANT
// main as an exception
unsigned char uc; // COMPLIANT - covered by VariableWidthIntegerTypesUsed
signed char sc; // COMPLIANT - covered by VariableWidthIntegerTypesUsed
}

void test_variable_width_type_qualified_variables() {
const char c1 = 0; // NON_COMPLIANT
const unsigned char uc1 = 0; // NON_COMPLIANT
const signed char sc1 = 0; // NON_COMPLIANt

const int i1 = 0; // NON_COMPLIANT
const unsigned int ui1 = 0; // NON_COMPLIANT
const unsigned u1 = 0; // NON_COMPLIANT
const signed int si1 = 0; // NON_COMPLIANT
const signed s1 = 0; // NON_COMPLIANT

const short sh1 = 0; // NON_COMPLIANT
const unsigned short ush1 = 0; // NON_COMPLIANT
const signed short ssh1 = 0; // NON_COMPLIANT

const long l1 = 0; // NON_COMPLIANT
const unsigned long ul1 = 0; // NON_COMPLIANT
const signed long sl1 = 0; // NON_COMPLIANT
const unsigned char uc1 = 0; // COMPLIANT - (VariableWidthIntegerTypesUsed)
const signed char sc1 = 0; // COMPLIANT - (VariableWidthIntegerTypesUsed)

volatile char c2; // NON_COMPLIANT
volatile unsigned char uc2; // NON_COMPLIANT
volatile signed char sc2; // NON_COMPLIANt

volatile int i2; // NON_COMPLIANT
volatile unsigned int ui2; // NON_COMPLIANT
volatile unsigned u2; // NON_COMPLIANT
volatile signed int si2; // NON_COMPLIANT
volatile signed s2; // NON_COMPLIANT

volatile short sh2; // NON_COMPLIANT
volatile unsigned short ush2; // NON_COMPLIANT
volatile signed short ssh2; // NON_COMPLIANT

volatile long l2; // NON_COMPLIANT
volatile unsigned long ul2; // NON_COMPLIANT
volatile signed long sl2; // NON_COMPLIANT
}

struct test_fix_fp_614 {
test_fix_fp_614 operator++(int); // COMPLIANT
test_fix_fp_614 operator--(int); // COMPLIANT
};

// COMPLIANT - instantiated with Fixed Width Types.
template <typename MyType> constexpr void test_fix_fp_540(MyType value) {
value++;
}

int call_test_fix_fp_540() {
test_fix_fp_540<std::uint8_t>(19);
test_fix_fp_540<std::int16_t>(20);
return 0;
}
volatile unsigned char uc2; // COMPLIANT - (VariableWidthIntegerTypesUsed)
volatile signed char sc2; // COMPLIANT - (VariableWidthIntegerTypesUsed)
}
69 changes: 69 additions & 0 deletions cpp/common/src/codingstandards/cpp/BannedFunctions.qll
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/**
* A library for supporting the consistent detection of banned functions in C++ code.
*/

import cpp
import AlertReporting

/**
* A signature for a banned function.
*/
signature class BannedFunction extends Function;

/**
* A module for detecting uses of banned functions in C++ code.
*/
module BannedFunctions<BannedFunction F> {
final private class FinalExpr = Expr;

/**
* An expression that uses a banned function.
*
* It can be either a function call or a function access (taking the address of the function).
*/
class UseExpr extends FinalExpr {
string action;
F bannedFunction;

UseExpr() {
this.(FunctionCall).getTarget() = bannedFunction and
action = "Call to"
or
this.(FunctionAccess).getTarget() = bannedFunction and
action = "Address taken for"
}

string getFunctionName() { result = bannedFunction.getName() }

string getAction() { result = action }

Element getPrimaryElement() {
// If this is defined in a macro in the users source location, then report the macro
// expansion, otherwise report the element itself. This ensures that we always report
// the use of the terminating function, but combine usages when the macro is defined
// by the user.
exists(Element e | e = MacroUnwrapper<UseExpr>::unwrapElement(this) |
if exists(e.getFile().getRelativePath()) then result = e else result = this
)
}
}

final private class FinalElement = Element;

/**
* A `Use` of a banned function.
*
* This is an `Element` in a program which represents the use of a banned function.
* For uses within macro expansions, this may report the location of the macro, if
* it is defined within the user's source code.
*/
class Use extends FinalElement {
UseExpr use;

Use() { this = use.getPrimaryElement() }

string getFunctionName() { result = use.getFunctionName() }

string getAction() { result = use.getAction() }
}
}
7 changes: 7 additions & 0 deletions cpp/common/src/codingstandards/cpp/BuiltInNumericTypes.qll
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,10 @@ class BuiltInIntegerType extends BuiltInType {
class ExcludedVariable extends Parameter {
ExcludedVariable() { getFunction() instanceof MainFunction }
}

/**
* Any main function.
*/
class ExcludedFunction extends Function {
ExcludedFunction() { this instanceof MainFunction }
}
Loading
Loading