PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, September 13, 2022

[FIXED] Why does `mkdtemp()` fail when called in googletest?

 September 13, 2022     c++, cross-platform, googletest, posix     No comments   

Issue

I've created a small RAII class that creates a unique temporary directory and deletes it again upon destruction. On Linux, it uses mkdtemp() to achieve this:

// temporaryDirectoryPath is an std::vector<char>
// containing u8"/tmp/nuclex-pixels-unittest-XXXXXX"

// Let mkdtemp() sort out a unique directory name for us (and create it!)
const char *directoryName = ::mkdtemp(&temporaryDirectoryPath[0]);
if(directoryName == nullptr) {
  perror("mkdtemp() failed."); // DEBUGGING. REMOVE.
  throw std::runtime_error("mkdtemp() failed.");
}

This works just fine when run on its own: runnable code on ideone.com


However, if I use that same code inside a GoogleTest 1.8.1 unit test declared like this:

TEST(MyTestFixture, CanFlumbleTempDirectory) {
  TemporaryDirectoryScope temporaryDirectory;
  // Could call temporaryDirectory.GetPath() here...
}

It fails:

Passing the following to mkdtemp(): /tmp/nuclex-pixels-unittest-XXXXXX
mkdtemp() failed.: Invalid argument

How can GoogleTest be interfering with mkdtemp()?


Solution

The string you pass to mkdtemp is not reliably null-terminated:

      // Then append our directory name template to it
      const char directoryNameTemplate[] = u8"nuclex-pixels-unittest-XXXXXX";
      {
        const char *iterator = directoryNameTemplate;
        while(*iterator != 0) {
          temporaryDirectoryPath.push_back(*iterator);
          ++iterator;
        }
      }

std::vector<char> does not perform implicit null termination, unlike std::string. This works by accident if there happens to be a null byte after the "XXXXXX" suffix. Whether that this is the case depends on the execution environment.



Answered By - Florian Weimer
Answer Checked By - Mildred Charles (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing