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

Cleanup static analyzer issues #179

Open
wants to merge 23 commits into
base: master
Choose a base branch
from

Conversation

kcgen
Copy link
Contributor

@kcgen kcgen commented Aug 23, 2021

This PR fixes issues flagged by the Clang static analyzer, Coverity, and PVS Studio.
Suggest reviewing commit-by-commit, for which the following provides more background.


Uninitialized member variables, flagged by GCC and PVS Studio

../submodules/loguru/loguru.cpp:1491:9: warning: 'loguru::LogScopeRAII::_indent_stderr' should be initialized in the member initialization list
../submodules/loguru/loguru.cpp:1491:9: warning: 'loguru::LogScopeRAII::_start_time_ns' should be initialized in the member initialization list
../submodules/loguru/loguru.cpp:1630:16: warning: 'loguru::StringStream::str' should be initialized in the member initialization list
../submodules/loguru/loguru.cpp:1725:9: warning: 'loguru::EcEntryBase::_previous' should be initialized in the member initialization list 

2021-08-22_20-25


Unused position increment (clarify intent), flagged by Clang's static analyzer

2021-08-22_22-59


Simplify empty std::string operations (1/2), flagged by PVS Studio

2021-08-22_22-46

Simplify empty std::string operations (2/2), flagged by PVS Studio

2021-08-22_20-40


Potential nullptr dereference, flagged by PVS Studio

2021-08-22_20-47


Clarify criteria by removing redundancy, flagged by PVS Studio

2021-08-22_20-42

2021-08-22_20-36

If it makes sense to keep keep them, I could see them be valid as asserts or CHECK_F`s -- let me know.


Virtualize EcEntryBase's destructor so it's called by derived classes, flagged by LGTM

2021-08-24_12-24


Explicitly ignore sigaction's return value, flagged by Coverity

2021-08-24_12-23

kcgen added a commit to dosbox-staging/dosbox-staging that referenced this pull request Aug 23, 2021
Fixes PVS Studio issue:

V730 Not all members of a class are initialized inside
the constructor. Consider inspecting: _verbosity, _line,
_name.
Fixes PVS Studio flagged issue:

V769 The 'file_path' pointer in the 'file_path + 1'
expression could be nullptr. In such case, resulting
value will be senseless and it should not be used.
The check to see if info.dli_sname is 0 will always be
false because of the prior if-statement requiring it
to be non-zero:
  "if (dladdr(callstack[i], &info) && info.dli_sname)"
   ...

Fixes PVS Studio flagged issue:

V547 Expression 'info.dli_sname == 0' is always false.
A very minor issue flagged by PVS Studio:

V815 Decreased performance. Consider replacing the
expression 'std::string("")' with 'std::string()'.
pos < out_buff_size is always true, because the
prior if-condition: if (out_buff_size == 0) { return; }
already guarantees that out_buff_size (an unsigned type)
is already non-zero, and long pos = 0, therefore
out_buff_size will always be greater than 0.

Fixes two PVS Studio issues:

V560 A part of conditional expression is always true:
pos < out_buff_size.

↑ V560 A part of conditional expression is always true:
pos < out_buff_size.
Fixes a PVS Studio issue:

 V522 There might be dereferencing of a potential null
 pointer 'with_newline'. Check lines: 1810, 1809.
Fixes two PVS Studio minor issues:

 V815 Decreased performance. Consider replacing the
 expression 's_arguments = ""' with 's_arguments.clear()'.

 V805 Decreased performance. It is inefficient to
 identify an empty string by using 'strlen(str) != 0'
 construct. A more efficient way is to check: str[0] !=
 '\0'.
Fixes a PVS Studio issues:

V823 Decreased performance. Object may be created
in-place in the 's_user_stack_cleanups' container.
Consider replacing methods: 'push_back' ->
'emplace_back'.
Fixes two issues flagged by Clang's static analyzer.
@kcgen kcgen changed the title Squash uninitialized member warnings Cleanup static analyzer issues Aug 23, 2021
kcgen and others added 6 commits August 24, 2021 12:29
Fixes an error flagged by LGTM:

Derived classes from a base class without a
virtual destructor will only call the
destructor of the type of the pointer being
deleted and not the base-class.

This can cause a defect if the pointer type is
a base type while the object instance is a
derived type.
Fixes these warnings:

../../submodules/loguru/loguru.cpp:559:15: warning: comparison is always true due to limited range of data type [-Wtype-limits]
    else if (0 <= c && c < 0x20) { // ASCI control character:
             ~~^~~~
../../submodules/loguru/loguru.cpp: In function ‘loguru::Text loguru::ec_to_text(char)’:
../../submodules/loguru/loguru.cpp:1776:14: warning: comparison is always true due to limited range of data type [-Wtype-limits]
   else if (0 <= c && c < 0x20) {
            ~~^~~~
* Fix warning suppression on Windows clang

When building loguru under Windows clang, _MSC_VER is defined, but it
uses clang-style warning suppression flags. Fix that by checking for
__GNUC__ and __clang__ first, then checking for _MSC_VER.

Also fix the lack of a `#pragma GCC diagnostic pop` for people who
include loguru.cpp.

* Fix unsigned/signed comparison

`snprintf` can return a negative value to signify that the buffer is not
large enough to contain the bytes you attempted to write, so check for a
positive value before advancing `pos` -- otherwise a later `snprintf`
might overwrite bytes we've already written to the buffer.
Fixes two issues flagged by Clang's static analyzer.
@kcgen
Copy link
Contributor Author

kcgen commented Sep 16, 2021

Re-sync'd with latest changes in master.

@kcgen
Copy link
Contributor Author

kcgen commented Sep 16, 2021

Recently merged PR#180 introduce two new PVS Studio warnings:

2021-09-16_08-33
2021-09-16_08-31

This branch now fixes these as well, in commit: dosbox-staging@0155fe9

Fixes CI failure:

/usr/include/x86_64-linux-gnu/bits/stdio2.h:64:35: note: ‘__builtin___snprintf_chk’ output between 5 and 11 bytes into a destination of size 5
   64 |   return __builtin___snprintf_chk (__s, __n, __USE_FORTIFY_LEVEL - 1,
      |          ~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   65 |        __bos (__s), __fmt, __va_arg_pack ());
      |        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
cc1plus: all warnings being treated as errors
CMakeFiles/loguru_test.dir/build.make:75: recipe for target 'CMakeFiles/loguru_test.dir/loguru_test.cpp.o' failed
make[2]: *** [CMakeFiles/loguru_test.dir/loguru_test.cpp.o] Error 1
CMakeFiles/Makefile2:82: recipe for target 'CMakeFiles/loguru_test.dir/all' failed
make[1]: *** [CMakeFiles/loguru_test.dir/all] Error 2
Makefile:100: recipe for target 'all' failed
make: *** [all] Error 2
CMake Error at test/appveyor.cmake:22 (_message):
MSYS2's own headers define NOMINMAX in:

  /msys64/mingw64/include/c++/11.2.0/x86_64-w64-mingw32/bits/os_defines.h

This causes a redefinition warning when building Loguru:

  ../src/libs/loguru/loguru.cpp:131: warning: "NOMINMAX" redefined
  131 |         #define NOMINMAX
      |
  In file included from D:/a/_temp/msys64/mingw64/include/c++/11.2.0/x86_64-w64-mingw32/bits/c++config.h:586,
                 from D:/a/_temp/msys64/mingw64/include/c++/11.2.0/utility:68,
                 from D:/a/_temp/msys64/mingw64/include/c++/11.2.0/algorithm:60,
                 from ../src/libs/loguru/loguru.cpp:33:
  45 | #define NOMINMAX 1
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.

2 participants