Replace tempnam with mkstemp
This change addresses the following build warning:
'''
warning: the use of `tempnam' is dangerous, better use `mkstemp'
'''
Tested: Build and unit tests passed successfully.
Change-Id: I875e2d1d954e30a102b2b58a674c57410f7be684
Signed-off-by: Jayanth Othayoth <ojayanth@gmail.com>
diff --git a/certificate.cpp b/certificate.cpp
index 0387921..67c5aad 100644
--- a/certificate.cpp
+++ b/certificate.cpp
@@ -30,6 +30,7 @@
#include <filesystem>
#include <fstream>
#include <map>
+#include <random>
#include <utility>
#include <vector>
@@ -141,17 +142,22 @@
std::string
Certificate::generateUniqueFilePath(const std::string& directoryPath)
{
- char* filePath = tempnam(directoryPath.c_str(), nullptr);
- if (filePath == nullptr)
+ // Create a template for the temporary file name
+ std::string filePath = directoryPath + "/" + "cert-XXXXXX";
+ int fd = mkstemp(filePath.data());
+ if (fd == -1)
{
lg2::error(
"Error occurred while creating random certificate file path, DIR:{DIR}",
"DIR", directoryPath);
elog<InternalFailure>();
+ throw std::runtime_error("Failed to create unique file path");
}
- std::string filePathStr(filePath);
- free(filePath);
- return filePathStr;
+
+ // Close the file descriptor and file, just need the unique path
+ close(fd);
+ std::remove(filePath.data());
+ return filePath;
}
std::string Certificate::generateAuthCertFileX509Path(