Skip to content
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

Not using braces and the logging library is error prone #183

Open
andrejlevkovitch opened this issue Nov 28, 2023 · 1 comment
Open

Not using braces and the logging library is error prone #183

andrejlevkovitch opened this issue Nov 28, 2023 · 1 comment

Comments

@andrejlevkovitch
Copy link
Contributor

andrejlevkovitch commented Nov 28, 2023

In the repository, for some reason, braces are not using in case when there is just one line after if or else. This is already a potential problem, because that is extremely error prone approach, especially if formatter (like clang-format) is not in use. For demonstrate how bad it is I want to show you how compiler handles one of if-else statements in the code.

Here it is:

if( prefix != NULL )
LogInfo("%s video options:\n", prefix);
else
LogInfo("video options:\n");

So, according to the lines, I expect that if prefix is NULL I should see video options:\n line in the logs. But here is a problem: the if-else doesn't have braces and it uses macros-es inside. Macros is not a function, so a priori you should not make assumptions about how much lines it takes. One line macros is not always equal to just line of code for compiler. And in that particular case it leads to a bug (not fatal, but still). Let's expand that macros-es:

if (prefix != NULL)
  GenericLogMessage(Log::Info, LOG_LEVEL_PREFIX_INFO "%s video options:\n", prefix);
else
  GenericLogMessage(Log::Info, LOG_LEVEL_PREFIX_INFO "video options:\n");

GenericLogMessage is also a macros, so lets expand further:

if (prefix != NULL)
  if (Log::Info <= Log::GetLevel()) fprintf(Log::GetFile(), LOG_LEVEL_PREFIX_INFO "%s video options:\n", prefix);
else
  if (Log::Info <= Log::GetLevel()) fprintf(Log::GetFile(), LOG_LEVEL_PREFIX_INFO "video options:\n");

Hm... So, here is a question: to what if that else relates?

PS I hope that you will abandoned that error prone style and start using braces even if there is "just one line of code". There are some tools that can help with that migration, like clang-format and clang-tidy

@dusty-nv
Copy link
Owner

Hi @andrejlevkovitch, thanks for raising this and appreciate your PR's - just merged them.

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

No branches or pull requests

2 participants