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

Inconsistent GETCWD in src/colvarbias_meta.cpp can cause underfined behavior #726

Open
al42and opened this issue Oct 11, 2024 · 1 comment · May be fixed by #728
Open

Inconsistent GETCWD in src/colvarbias_meta.cpp can cause underfined behavior #726

al42and opened this issue Oct 11, 2024 · 1 comment · May be fixed by #728

Comments

@al42and
Copy link

al42and commented Oct 11, 2024

In src/colvarbias_meta.cpp, there's GETCWD(BUF, SIZE) macro used to wrap getcwd function to put current working dir into BUF. However, when std::filesystem is available, a different implementation is used, which does not change BUF but instead returns a temporary C-string: https://github.com/Colvars/colvars/blob/0e0ce447630e95f2cd0322b1699e7f60b526ef9f/src/colvarbias_meta.cpp#L25C1-L30C7

This is not ok with how GETCWD is later used:

    char *pwd = new char[3001];
    if (GETCWD(pwd, 3000) == nullptr) {
      if (pwd != nullptr) { //
        delete[] pwd;
      }
      return cvm::error("Error: cannot get the path of the current working directory.\n",
                        COLVARS_BUG_ERROR);
    }

    replica_list_file =
      (std::string(pwd)+std::string(PATHSEP)+
       this->name+"."+replica_id+".files.txt");

The conditional will never be false, but pwd will not be initialized; thus so std::string(pwd) later is UB.

@giacomofiorin
Copy link
Member

Thanks! Will fix it here and include it in the GROMACS 2024 bugfixes as well as the Colvars lib update for 2025.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants