Remove unreachable code from GE5ImageIO and use unique_ptr#3619
Conversation
In C++, `new char[N]` does not return a `nullptr`, it might just throw an `std::bad_alloc` exception, in case of allocation failure. So there is no need to do something like "if (buffer == nullptr) clean up and throw an exception".
Fixes potential (however rare) memory leaks, in case of an exception during the execution of `GE5ImageIO::ReadHeader`.
Fixes a potential memory leak, in case of an exception during the execution of `GE5ImageIO::ModifyImageInformation()`.
|
@N-Dekker Thanks for all these efforts to avoid the use raw pointers. In this case, the rationale and/or commit message looks to be inconsistent with the coverage report: The "unreachable code" is actually being reached. Any ways to explain why that is happening if the code should be unreachable? |
Very interesting @jhlegarreta ! Those lines that say |
|
From a very old (but pretty famous) C++FAQ, by Marshall Cline:
https://www.cs.technion.ac.il/users/yechiel/c++-faq/new-never-returns-null.html |
|
Just as a joke, not applicable.
Hmm, we don't know here what is class Fred, may be e.g.: #include <iostream>
class Fred
{
public:
void * operator new(size_t) /* throw() */
{
return nullptr;
}
void operator delete(void*)
{
}
};
int main(int, char**)
{
Fred * f = new Fred;
if (f == nullptr)
{
std::cout << "Fred is nullptr" << std::endl;
}
delete f;
return 0;
}Works for me with and without Edit: |
@issakomi Very clever 👍, Mihail! Thanks! Now I just hope we can keep your
Indeed, not relevant for this specific PR, but So I think it works as expected. Right? |
IDK. I have observed at times coverage reports that seemed misleading (e.g. not reporting lines as covered when the test contained calls to them, and the file was contained in the In this case, a way to see if those lines are actually hit is to inspect the output of the corresponding test in the dashboard. Also, I remember reading in other PRs compilers throwing warnings about unreachable code (maybe GCC 12.1.0 as @issakomi mentions?). |
|
Тhe class Fred was a joke of course, sorry for the noise ... But it is good that Eigen's scenario with overloaded new was checked, better check twice, IMHO.
IMHO, i don't think compiler will always warn about dead code, i have discovered several such bugs in my code with Synopys Coverity analyzer, compiler said nothing. #include <iostream>
int main(int, char**)
{
const bool b = true;
int a = 1;
if (!b)
{
++a;
}
std::cout << a << std::endl;
return 0;
}No warning. |
Interesting... for what it's worth, the following does at least produce a warning from MSVC, saying:
|
|
For the record, the original Anyway, thanks for your comments @issakomi, @jhlegarreta and thanks for your approval @dzenanz I think this pull request is ready 😃 |
int* p = new(std::nothrow) int[100000000ul]; // non-throwing overload
if (p == nullptr)another alternative, but just for completeness, not applicable for the PR |
|
Thanks for merging @hjmjohnson Of course, it would have been nice if any static analysis tool would have produced a warning on unreachable code! Moreover, I find it really misleading that ubuntu2004 Coverage 8 claimed that those unreachable lines of code were actually covered. At https://open.cdash.org/viewCoverageFile.php?buildid=8157810&fileid=52192360 as remarked by @jhlegarreta. Hope the Coverage tool will still be fixed! |
Similar to pull request InsightSoftwareConsortium#3619 commit e2b7445 "STYLE: Declare local `buffer` in `GE5ImageIO::ReadHeader` as unique_ptr"
Similar to pull request InsightSoftwareConsortium#3619 commit ef0da50 "STYLE: Declare local `buffer` in `GE5ImageIO::ReadHeader` as unique_ptr"
GE5ImageIO::ReadHeaderappeared to have unreachable code inif-clauses like this:This pull request proposes to remove those unreachable lines of code, and replace locally declared raw pointers by
unique_ptr, in GE5ImageIO.