← 返回首页
Function declared in block — CodeQL query help documentation CodeQL docs
CodeQL documentation
CodeQL resources

Function declared in block

ID: cpp/function-in-block Kind: problem Security severity: Severity: recommendation Precision: very-high Tags: - maintainability - readability - external/jsf Query suites: - cpp-security-and-quality.qls

Click to see the query in the CodeQL repository

This rule finds functions that are declared in a block. It is confusing to declare a function at block scope, and the visibility of the function is not what would be expected. extern function declarations inside a block, which are allowed in C, are particularly confusing, as the scope of the declaration is the entire file, not just the block where it was declared.

Recommendation

Declare the function in file scope to prevent any confusion as to its visibility.

Example

int f() { extern int other(); //scope of externs is the entire file, not just the //block where it is declared ... other() } int g() { other(); //this will use the other() function declared inside f() }

References