Skip to content

Fix #14195 Show warning when main() throws an exception#7887

Merged
chrchr-github merged 10 commits intodanmar:mainfrom
chrchr-github:chr_14195
Oct 27, 2025
Merged

Fix #14195 Show warning when main() throws an exception#7887
chrchr-github merged 10 commits intodanmar:mainfrom
chrchr-github:chr_14195

Conversation

@chrchr-github
Copy link
Copy Markdown
Collaborator

No description provided.

@chrchr-github
Copy link
Copy Markdown
Collaborator Author

Do we need to do something with the new id?

@firewave
Copy link
Copy Markdown
Collaborator

Do we need to do something with the new id?

Beside triggering it from getErrorMessages() (which you did) I am not aware of anything.

Comment thread lib/checkexceptionsafety.cpp Outdated

void CheckExceptionSafety::entryPointThrowError(const Token * const tok)
{
reportError(tok, Severity::error, "throwInEntryPoint", "Exception thrown in function that is an entry point.", CWE398, Certainty::normal);
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest something like:

Unhandled exception is thrown in function that is an entry point.

We only warn if it's unhandled right?

This is undefined behavior right? To throw an exception that is not catched..

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Throwing in main() results in program termination: https://en.cppreference.com/w/cpp/language/throw.html
I'll add a negative test.

Copy link
Copy Markdown
Owner

@danmar danmar Oct 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok so it's defined behavior. It's not 100% clear to me that it should have "error" severity or if "warning" / "portability" would be more proper.
If program termination is wanted this is not a bad option to achieve that. However there is a danger that some RAII classes will not be destroyed properly. And using some std::exit function or something like that could be more clear about the intent.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like the outcome is the same as for throwing from a noexcept function, so the severity should also be the same.

I suggest something like:
Unhandled exception is thrown in function that is an entry point.

Should we change the message for throwInNoexceptFunction as well?

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like the outcome is the same as for throwing from a noexcept function, so the severity should also be the same.

I agree .. it's not 100% clear to me so well we can keep it as is.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we change the message for throwInNoexceptFunction as well?

please feel free to do it because it's only when it's not handled it's a problem. A user might think we sloppily warn whenever exceptions are thrown.

@danmar
Copy link
Copy Markdown
Owner

danmar commented Oct 27, 2025

I would like that the PR title and ticket are a bit more descriptive. If we will look in the git log later we might wonder if this prevents Cppcheck from throwing some exceptions in main().
So I suggest improve check: ...

@chrchr-github chrchr-github changed the title Fix #14195 main throws an exception Fix #14195 Show warning when main() throws an exception Oct 27, 2025
@sonarqubecloud
Copy link
Copy Markdown

@chrchr-github chrchr-github merged commit c6f3d72 into danmar:main Oct 27, 2025
55 checks passed
@chrchr-github chrchr-github deleted the chr_14195 branch October 27, 2025 19:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants