googletest icon indicating copy to clipboard operation
googletest copied to clipboard

`MakeFileName()` creates hidden file in case `base_name` is empty.

Open joshiayush opened this issue 3 years ago • 3 comments

Describe the bug

The current version of function MakeFIleName() in FilePath API doesn't check if the base_name is empty or not. In case the number passed to the function is 0 then MakeFileName() function will create path for hidden files in *nix systems which is something that we don't want.

Steps to reproduce the bug

You don't need to reproduce the bug in your side because it's readily visible in the function MakeFileName()

FilePath FilePath::MakeFileName(const FilePath& directory,
                                const FilePath& base_name, int number,
                                const char* extension) {
  std::string file;
  if (number == 0) {
    file = base_name.string() + "." + extension;
  } else {
    file =
        base_name.string() + "_" + StreamableToString(number) + "." + extension;
  }
  return ConcatPaths(directory, FilePath(file));
}

Possible solution

We need to substitute base_name with a random string or just unknown file in case base_name is empty.

Does the bug persist in the most recent commit?

Yes

What operating system and version are you using?

Ubuntu 20.04.4 LTS

What compiler and version are you using?

gcc version 9.4.0 (Ubuntu 9.4.0-1ubuntu1~20.04.1)

What build system are you using?

cmake version 3.22.1

joshiayush avatar Apr 30 '22 08:04 joshiayush

@derekmauro Tell me if you are open to accepting a PR that fixes this issue.

joshiayush avatar Apr 30 '22 08:04 joshiayush

This seems WAI to us, but we might consider having a patch that special cases empty filenames. Could you please explain how you encountered this issue?

dinord avatar May 02 '22 19:05 dinord

Could you please explain how you encountered this issue?

@dinord Personally I didn't encounter this issue but it may become an issue in case base_name is empty. I was just reading the code of FilePath API and found that empty base_name is not handled properly. What do you say? How it should be fixed?

joshiayush avatar May 03 '22 02:05 joshiayush