Skip to content

M0-1-10: Functions called from int32_t main() are not considered as "called" #646

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

Closed
rak3-sh opened this issue Jul 18, 2024 · 8 comments
Closed
Assignees
Labels
Difficulty-Low A false positive or false negative report which is expected to take <1 day effort to address false positive/false negative An issue related to observed false positives or false negatives. Impact-Low user-report Issue reported by an end user of CodeQL Coding Standards

Comments

@rak3-sh
Copy link
Contributor

rak3-sh commented Jul 18, 2024

Affected rules

  • M0-1-10

Description

The class MainFunction defined in EncapsulatingFunctions.qll doesn't consider int32_t main() as a "main" function. Due to this, multiple false positives get raised in case the user has defined main with int32_t return type.

Example

Definitions of func1 and func2 are left out for reasons of brevity.

namespace mains {
    static int32_t var;

    // @brief namespace_func
    static void namespace_func(void) noexcept { // FP: Function is reported as not called, but it is called from "main" below.
        mains::var = -1;
        return;
    }
}  // namespace

// @brief main
// @return exit code
int32_t main(void) {
    int32_t ret {0};
    try {
        ret = func1(); // FP: Reported as not called in the definition location
        mains::var += ret;
        ret = func2(); // FP: Reported as not called in the definition location
        mains::var += ret;
    }
    catch(...) { 
        mains::namespace_func(); // namespace_func is called here.
    }
    return ret; 
}
@rak3-sh rak3-sh added the false positive/false negative An issue related to observed false positives or false negatives. label Jul 18, 2024
@rak3-sh
Copy link
Contributor Author

rak3-sh commented Jul 18, 2024

A possible fix strategy would be to consider Int32_t type as well for MainFunction here. It works locally. Happy to provide a PR that can be reviewed and integrated. Kindly let me know.

class MainFunction extends MainLikeFunction {
  MainFunction() {
    hasGlobalName("main") and
    (
      getType() instanceof IntType or
      getType() instanceof Int32_t
    )
  }
}

@lcartey
Copy link
Collaborator

lcartey commented Jul 19, 2024

Thanks for the report!

For a main function to be valid it needs to be using int, or an alias of int. So the above is only valid on platforms where int32_t is an alias for int. For this reason, I would suggest changing MainFunction to resolve typedefs, then perform the check against IntType, e.g.:

class MainFunction extends MainLikeFunction {
  MainFunction() {
    hasGlobalName("main") and
    (
      getType().resolveTypeDefs() instanceof IntType
    )
  }
}

This would also have the benefit of capturing mains which use other aliases for int.

@lcartey lcartey added user-report Issue reported by an end user of CodeQL Coding Standards Difficulty-Low A false positive or false negative report which is expected to take <1 day effort to address Impact-Low labels Jul 19, 2024
@lcartey lcartey moved this from Reported to Triaged in Coding Standards Public Development Board Jul 19, 2024
@rak3-sh
Copy link
Contributor Author

rak3-sh commented Jul 19, 2024

I agree .. your proposal sounds better! Thank you for your comments!

@rak3-sh
Copy link
Contributor Author

rak3-sh commented Jul 19, 2024

I was wondering how to modify the test for this rule. The test case uses the int main version. If I create another C++ file with int32_t main, will the test infrastructure treat it as a separate test? Should this be added in a separate directory?

@rak3-sh
Copy link
Contributor Author

rak3-sh commented Jul 22, 2024

@lcartey : Would it be OK to have a test for this here considering that the fix is inside the library - EncapsulatingFunctions.qll?

@lcartey
Copy link
Collaborator

lcartey commented Jul 22, 2024

Library tests can be added to this directory:
https://github.com/github/codeql-coding-standards/tree/main/cpp/common/test/library/codingstandards/cpp

And, just to confirm, you will need a separate test directory for each type of main you want to test.

rak3-sh added a commit to rak3-sh/codeql-coding-standards that referenced this issue Jul 23, 2024
@rak3-sh
Copy link
Contributor Author

rak3-sh commented Jul 23, 2024

@lcartey - thanks for your help as always! Kindly find the PR for the fix of this issue here.

@lcartey lcartey moved this from Triaged to In Progress in Coding Standards Public Development Board Jul 24, 2024
@rak3-sh
Copy link
Contributor Author

rak3-sh commented Jul 28, 2024

Issue fixed with PR: #647

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Difficulty-Low A false positive or false negative report which is expected to take <1 day effort to address false positive/false negative An issue related to observed false positives or false negatives. Impact-Low user-report Issue reported by an end user of CodeQL Coding Standards
Projects
Development

No branches or pull requests

2 participants