-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
several improvements to Path::getAbsolutePath()
#6542
Conversation
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This now crashes in Cygwin:
|
Looks like it is caused by the static initialization order. So this requires https://trac.cppcheck.net/ticket/12080 to be fixed first. |
We could also make the cache in simplecpp optional. Nevertheless these caches are completely horrible and need to be implemented in a different way. |
71ef47a
to
49daf24
Compare
The crash is fixed because the test fixtures are no longer global objects which depend on the initialization order. |
Path::getAbsolutePath()
Path::getAbsolutePath()
if (absolute) | ||
absolute_path = absolute; | ||
free(absolute); | ||
if (!filePath.empty() && !spath.empty() && absolute_path.empty() && !exists(spath)) | ||
throw std::runtime_error("path '" + filePath + "' does not exist"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This makes sure we do not get unexpected results. This needs an integration test which triggers this though (I am hoping one of the existing ones will fail so I can derive from that).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The only case where this could happen is in loadVisualStudioProperties()
and that code looks quite shifty and need some proper testing. As Path::isAbsolute()
also needs some work that will probably happen in that context.
b62a0d9
to
fd971fa
Compare
I filed danmar/simplecpp#361 upstream. |
lib/path.cpp
Outdated
std::string absolute_path; | ||
#ifdef _WIN32 | ||
char absolute[_MAX_PATH]; | ||
if (_fullpath(absolute, filePath.c_str(), _MAX_PATH)) | ||
absolute_path = absolute; | ||
if (absolute_path.back() == '\\') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
there is a good chance that absolute_path is empty right? don't call back() in that case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That would indeed be undefined: https://en.cppreference.com/w/cpp/string/basic_string/back - and we even report that. I wonder if that might exists in other parts of the code. We could probably be more aggressive about detecting that - maybe similar to a NULL dereference.
The _fullpath()
call might need better errorhandling.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That would indeed be undefined: https://en.cppreference.com/w/cpp/string/basic_string/back - and we even report that. I wonder if that might exists in other parts of the code. We could probably be more aggressive about detecting that - maybe similar to a NULL dereference.
This is not reported because it is in the _WIN32
code which we do not analyze in the CI. We should probably add analysis steps for files using those to the selfchecks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed. I also added a bunch of missing test cases and TODOs.
I also filed https://trac.cppcheck.net/ticket/13158 about enhancing the selfcheck.
fd971fa
to
290f233
Compare
lib/path.cpp
Outdated
if (absolute) | ||
absolute_path = absolute; | ||
free(absolute); | ||
if (!filePath.empty() && !spath.empty() && absolute_path.empty() && !exists(spath)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we know that filePath is not empty. so condition !filePath.empty() &&
is redundant.
I am not sure shouldn't we use ||
instead of &&
here. each of those conditions look bad?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Even though there is a preprocessor check that should be detected.
I will review the remaining conditions and add a comment about the intention.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I filed https://trac.cppcheck.net/ticket/13178 about detecting this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I adjusted the check and added a comment.
290f233
to
fb1ddd7
Compare
..
do not existgetAbsolutePath()
if the given path does not exist