fix warning tmpnam

This commit is contained in:
2025-08-13 11:17:31 +03:00
parent be2d3c197c
commit 64e142b8c6
4 changed files with 24 additions and 5 deletions

View File

@@ -501,6 +501,8 @@ if (NOT CROSSTOOLS)
target_include_directories(pip_lua PUBLIC "${_lua_src_dir}") target_include_directories(pip_lua PUBLIC "${_lua_src_dir}")
if (WIN32) if (WIN32)
target_compile_definitions(pip_lua PRIVATE LUA_BUILD_AS_DLL LUA_CORE) target_compile_definitions(pip_lua PRIVATE LUA_BUILD_AS_DLL LUA_CORE)
else()
target_compile_definitions(pip_lua PRIVATE LUA_USE_POSIX)
endif() endif()
list(APPEND HDR_DIRS "${PIP_3PL_DIR}/LuaBridge") list(APPEND HDR_DIRS "${PIP_3PL_DIR}/LuaBridge")
list(APPEND HDRS ${_lua_src_hdr}) list(APPEND HDRS ${_lua_src_hdr})

View File

@@ -176,14 +176,16 @@ bool PIFile::openTemporary(PIIODevice::DeviceMode mode) {
PIString tp; PIString tp;
#ifdef WINDOWS #ifdef WINDOWS
tp = PIDir::temporary().path() + PIDir::separator + "file" + PIString::fromNumber(randomi()); tp = PIDir::temporary().path() + PIDir::separator + "file" + PIString::fromNumber(randomi());
#else
char * rc = tmpnam(0);
if (!rc) return false;
tp = rc;
#endif
while (isExists(tp)) { while (isExists(tp)) {
tp += PIString::fromNumber(randomi() % 10); tp += PIString::fromNumber(randomi() % 10);
} }
#else
char template_rc[] = "/tmp/pifile_tmp_XXXXXX";
int fd = mkstemp(template_rc);
if (fd == -1) return false;
::close(fd);
tp = template_rc;
#endif
return open(tp, mode); return open(tp, mode);
} }

View File

@@ -35,3 +35,4 @@ pip_test(math)
pip_test(core) pip_test(core)
pip_test(piobject) pip_test(piobject)
pip_test(client_server pip_client_server) pip_test(client_server pip_client_server)
pip_test(io)

14
tests/io/testpifile.cpp Normal file
View File

@@ -0,0 +1,14 @@
#include "pifile.h"
#include "gtest/gtest.h"
TEST(PIFile_Test, openTemporary) {
const auto ba = PIByteArray::fromHex("AABBCC");
PIFile f;
ASSERT_TRUE(f.openTemporary());
ASSERT_TRUE(f.path().isNotEmpty());
ASSERT_EQ(ba.size(), f.write(ba));
ASSERT_EQ(ba, f.readAll());
ASSERT_TRUE(f.close());
}