为什么在谷歌测试中调用"mkdtemp()"时失败
Why does `mkdtemp()` fail when called in googletest?
我创建了一个小的RAII类,该类创建一个唯一的临时目录,并在销毁时再次将其删除。在Linux上,它使用mkdtemp()
来实现这一点:
// 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.");
}
这在单独运行时运行得很好:ideone.com上的可运行代码
然而,如果我在GoogleTest 1.8.1单元测试中使用相同的代码,声明如下:
TEST(MyTestFixture, CanFlumbleTempDirectory) {
TemporaryDirectoryScope temporaryDirectory;
// Could call temporaryDirectory.GetPath() here...
}
失败:
Passing the following to mkdtemp(): /tmp/nuclex-pixels-unittest-XXXXXX
mkdtemp() failed.: Invalid argument
GoogleTest如何干扰mkdtemp()
?
传递给mkdtemp
的字符串不可靠地以null结尾:
// 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::string
不同,std::vector<char>
不执行隐式空终止。如果"XXXXXX"
后缀后面恰好有一个空字节,这是偶然发生的。情况是否如此取决于执行环境。
相关文章:
- 如果没有malloc,链表实现将失败
- 模板参数替换失败,并且未完成隐式转换
- 具有默认模板参数的多态类的模板推导失败
- 视图中的参数推导失败:take_while
- 链接到自行创建的dll失败
- 带有特殊路径部分的"std::filesystem::weakly_canonical"失败
- GetShortPathName在网络驱动器上使用中文文件夹时失败
- gcc和c++17的过载解析失败
- 为什么使用 P/Invoke 调用 dll 时,某些计算机中的 LoadLibrary 失败?
- 在WSL:configure_file上对config_file的每次调用都失败:配置文件时出现问题
- 使用 GCC 卸载的 OpenMP 卸载失败,并出现"Ptx assembly aborted due to errors"
- 使用cmake从源代码构建MySQL连接器/C++失败(与以前的声明冲突)
- 链接阶段在Ubuntu上失败,但在MacOS上失败
- 从父数组测试用例构造二叉树失败
- LibGit2 SSH身份验证失败
- 如何让LLDB在成功时退出,在失败时等待
- VS2017,C++包含目录与附加包含目录,子文件夹包含失败-但为什么
- 生成MRPT库时cmake配置失败
- 为什么除非添加括号,否则构造函数上的模板替换会失败?
- variadic模板中的模板参数推导失败