Skip to content

Commit

Permalink
Replaced the use of "new" in find_resource
Browse files Browse the repository at this point in the history
- Addresses SonarCloud issue cpp:S5025 (Memory should not be managed manually)
- This function was not changed for the Windows port, but a similar
change to the one suggested was done in expandEnv in the same file.
- The first stream is not destructed at the exact same point it was in
the previous code (but rather when the second stream replaces it on
assignment to the same variable). An arbitrary scope could have been
introduced to destruct the object at the same place, but it doesn't
seem to be necessary and would make the code a bit strange.
  • Loading branch information
eduar-hte committed May 4, 2024
1 parent d35c8c7 commit 3862695
Showing 1 changed file with 4 additions and 11 deletions.
15 changes: 4 additions & 11 deletions src/utils/system.cc
Original file line number Diff line number Diff line change
Expand Up @@ -98,19 +98,15 @@ double cpu_seconds(void) {

std::string find_resource(const std::string& resource,
const std::string& config, std::string *err) {
std::ifstream *iss;

err->assign("Looking at: ");
// Trying absolute or relative to the current dir.
iss = new std::ifstream(resource, std::ios::in);
if (iss->is_open()) {
iss->close();
delete iss;
auto iss = std::ifstream(resource, std::ios::in);
if (iss.is_open()) {
return resource;
} else {
err->append("'" + resource + "', ");
}
delete iss;

// What about `*' ?
if (utils::expandEnv(resource, 0).size() > 0) {
Expand All @@ -121,15 +117,12 @@ std::string find_resource(const std::string& resource,

// Trying the same path of the configuration file.
std::string f = get_path(config) + "/" + resource;
iss = new std::ifstream(f, std::ios::in);
if (iss->is_open()) {
iss->close();
delete iss;
iss = std::ifstream(f, std::ios::in);
if (iss.is_open()) {
return f;
} else {
err->append("'" + f + "', ");
}
delete iss;

// What about `*' ?
if (utils::expandEnv(f, 0).size() > 0) {
Expand Down

0 comments on commit 3862695

Please sign in to comment.