1
0
mirror of https://github.com/Kitware/CMake.git synced 2025-10-14 02:08:27 +08:00

KWSys 2021-10-27 (e19a5668)

Code extracted from:

    https://gitlab.kitware.com/utils/kwsys.git

at commit e19a5668f01bb9d96440ce96e777749d6e92562d (e19a5668f01bb9d96440ce96e777749d6e92562d).

Upstream Shortlog
-----------------

Mathieu Westphal (1):
      e28d7282 DynamicLoader: Add RTLD_GLOBAL as a supported flag on linux

Sean McBride (8):
      704a63d4 Replace sprintf with snprintf
      f9f6d67b Replace non-standard _snprintf with standard snprintf
      f771c009 Fix -Wunused-macros warning by defining under same conditions as usage
      f3d4b12b Fix Wmissing-prototypes warnings by making functions static
      6f4a1826 Fix Wmissing-variable-declarations by declaring variable in a header
      31d25023 Fix all Wold-style-cast warnings
      0f44b620 Fix Wreserved-id-macro warning by removing double underscore
      44718539 Fix for extraneous semi-colon warning
This commit is contained in:
KWSys Upstream
2021-10-27 09:30:56 -04:00
committed by Brad King
parent 58f046ba26
commit 6015a898d4
15 changed files with 128 additions and 89 deletions

View File

@@ -16,11 +16,11 @@
@KWSYS_SYSTEMTOOLS_USE_TRANSLATION_MAP@
#if defined(__SUNPRO_CC) && __SUNPRO_CC > 0x5130 && defined(__has_attribute)
# define @KWSYS_NAMESPACE@__has_cpp_attribute(x) __has_attribute(x)
# define @KWSYS_NAMESPACE@_has_cpp_attribute(x) __has_attribute(x)
#elif defined(__has_cpp_attribute)
# define @KWSYS_NAMESPACE@__has_cpp_attribute(x) __has_cpp_attribute(x)
# define @KWSYS_NAMESPACE@_has_cpp_attribute(x) __has_cpp_attribute(x)
#else
# define @KWSYS_NAMESPACE@__has_cpp_attribute(x) 0
# define @KWSYS_NAMESPACE@_has_cpp_attribute(x) 0
#endif
#if __cplusplus >= 201103L
@@ -31,13 +31,13 @@
#ifndef @KWSYS_NAMESPACE@_FALLTHROUGH
# if __cplusplus >= 201703L && \
@KWSYS_NAMESPACE@__has_cpp_attribute(fallthrough)
@KWSYS_NAMESPACE@_has_cpp_attribute(fallthrough)
# define @KWSYS_NAMESPACE@_FALLTHROUGH [[fallthrough]]
# elif __cplusplus >= 201103L && \
@KWSYS_NAMESPACE@__has_cpp_attribute(gnu::fallthrough)
@KWSYS_NAMESPACE@_has_cpp_attribute(gnu::fallthrough)
# define @KWSYS_NAMESPACE@_FALLTHROUGH [[gnu::fallthrough]]
# elif __cplusplus >= 201103L && \
@KWSYS_NAMESPACE@__has_cpp_attribute(clang::fallthrough)
@KWSYS_NAMESPACE@_has_cpp_attribute(clang::fallthrough)
# define @KWSYS_NAMESPACE@_FALLTHROUGH [[clang::fallthrough]]
# endif
#endif
@@ -45,7 +45,7 @@
# define @KWSYS_NAMESPACE@_FALLTHROUGH static_cast<void>(0)
#endif
#undef @KWSYS_NAMESPACE@__has_cpp_attribute
#undef @KWSYS_NAMESPACE@_has_cpp_attribute
/* If building a C++ file in kwsys itself, give the source file
access to the macros without a configured namespace. */

View File

@@ -99,18 +99,21 @@ Status Directory::Load(std::string const& name, std::string* errorMessage)
this->Clear();
intptr_t srchHandle;
char* buf;
size_t bufLength;
size_t n = name.size();
if (name.back() == '/' || name.back() == '\\') {
buf = new char[n + 1 + 1];
sprintf(buf, "%s*", name.c_str());
bufLength = n + 1 + 1;
buf = new char[bufLength];
snprintf(buf, bufLength, "%s*", name.c_str());
} else {
// Make sure the slashes in the wildcard suffix are consistent with the
// rest of the path
buf = new char[n + 2 + 1];
bufLength = n + 2 + 1;
buf = new char[bufLength];
if (name.find('\\') != std::string::npos) {
sprintf(buf, "%s\\*", name.c_str());
snprintf(buf, bufLength, "%s\\*", name.c_str());
} else {
sprintf(buf, "%s/*", name.c_str());
snprintf(buf, bufLength, "%s/*", name.c_str());
}
}
struct _wfinddata_t data; // data of current file
@@ -148,13 +151,16 @@ unsigned long Directory::GetNumberOfFilesInDirectory(const std::string& name,
{
intptr_t srchHandle;
char* buf;
size_t bufLength;
size_t n = name.size();
if (name.back() == '/') {
bufLength = n + 1 + 1;
buf = new char[n + 1 + 1];
sprintf(buf, "%s*", name.c_str());
snprintf(buf, bufLength, "%s*", name.c_str());
} else {
bufLength = n + 2 + 1;
buf = new char[n + 2 + 1];
sprintf(buf, "%s/*", name.c_str());
snprintf(buf, bufLength, "%s/*", name.c_str());
}
struct _wfinddata_t data; // data of current file

View File

@@ -275,20 +275,20 @@ const char* DynamicLoader::LastError()
if (length < 1) {
/* FormatMessage failed. Use a default message. */
_snprintf(str, DYNLOAD_ERROR_BUFFER_SIZE,
"DynamicLoader encountered error 0x%X. "
"FormatMessage failed with error 0x%X",
error, GetLastError());
snprintf(str, DYNLOAD_ERROR_BUFFER_SIZE,
"DynamicLoader encountered error 0x%lX. "
"FormatMessage failed with error 0x%lX",
error, GetLastError());
return str;
}
if (!WideCharToMultiByte(CP_UTF8, 0, lpMsgBuf, -1, str,
DYNLOAD_ERROR_BUFFER_SIZE, nullptr, nullptr)) {
/* WideCharToMultiByte failed. Use a default message. */
_snprintf(str, DYNLOAD_ERROR_BUFFER_SIZE,
"DynamicLoader encountered error 0x%X. "
"WideCharToMultiByte failed with error 0x%X",
error, GetLastError());
snprintf(str, DYNLOAD_ERROR_BUFFER_SIZE,
"DynamicLoader encountered error 0x%lX. "
"WideCharToMultiByte failed with error 0x%lX",
error, GetLastError());
}
return str;
@@ -436,9 +436,14 @@ namespace KWSYS_NAMESPACE {
DynamicLoader::LibraryHandle DynamicLoader::OpenLibrary(
const std::string& libname, int flags)
{
CHECK_OPEN_FLAGS(flags, 0, nullptr);
CHECK_OPEN_FLAGS(flags, RTLDGlobal, nullptr);
return dlopen(libname.c_str(), RTLD_LAZY);
int llFlags = RTLD_LAZY;
if (flags & RTLDGlobal) {
llFlags |= RTLD_GLOBAL;
}
return dlopen(libname.c_str(), llFlags);
}
int DynamicLoader::CloseLibrary(DynamicLoader::LibraryHandle lib)

View File

@@ -73,7 +73,12 @@ public:
// This is currently only supported on Windows.
SearchBesideLibrary = 0x00000001,
AllOpenFlags = SearchBesideLibrary
// Make loaded symbols visible globally
//
// This is currently only supported on *nix systems.
RTLDGlobal = 0x00000002,
AllOpenFlags = SearchBesideLibrary | RTLDGlobal
};
/** Load a dynamic library into the current process.

View File

@@ -2287,7 +2287,8 @@ static void kwsysProcessSetExitExceptionByIndex(kwsysProcess* cp, int sig,
#endif
default:
cp->ProcessResults[idx].ExitException = kwsysProcess_Exception_Other;
sprintf(cp->ProcessResults[idx].ExitExceptionString, "Signal %d", sig);
snprintf(cp->ProcessResults[idx].ExitExceptionString,
KWSYSPE_PIPE_BUFFER_SIZE + 1, "Signal %d", sig);
break;
}
}
@@ -2540,7 +2541,7 @@ static void kwsysProcessKill(pid_t process_id)
int pid;
if (sscanf(d->d_name, "%d", &pid) == 1 && pid != 0) {
struct stat finfo;
sprintf(fname, "/proc/%d/stat", pid);
snprintf(fname, sizeof(fname), "/proc/%d/stat", pid);
if (stat(fname, &finfo) == 0) {
FILE* f = fopen(fname, "r");
if (f) {

View File

@@ -29,7 +29,7 @@ a UNIX-style select system call.
# define KWSYS_WINDOWS_DEPRECATED_GetVersionEx
#endif
#include <io.h> /* _unlink */
#include <stdio.h> /* sprintf */
#include <stdio.h> /* snprintf */
#include <string.h> /* strlen, strdup */
#ifndef _MAX_FNAME
@@ -1867,18 +1867,18 @@ void kwsysProcessCleanup(kwsysProcess* cp, DWORD error)
KWSYSPE_PIPE_BUFFER_SIZE, 0);
if (length < 1) {
/* FormatMessage failed. Use a default message. */
_snprintf(cp->ErrorMessage, KWSYSPE_PIPE_BUFFER_SIZE,
"Process execution failed with error 0x%X. "
"FormatMessage failed with error 0x%X",
error, GetLastError());
snprintf(cp->ErrorMessage, KWSYSPE_PIPE_BUFFER_SIZE,
"Process execution failed with error 0x%lX. "
"FormatMessage failed with error 0x%lX",
error, GetLastError());
}
if (!WideCharToMultiByte(CP_UTF8, 0, err_msg, -1, cp->ErrorMessage,
KWSYSPE_PIPE_BUFFER_SIZE, NULL, NULL)) {
/* WideCharToMultiByte failed. Use a default message. */
_snprintf(cp->ErrorMessage, KWSYSPE_PIPE_BUFFER_SIZE,
"Process execution failed with error 0x%X. "
"WideCharToMultiByte failed with error 0x%X",
error, GetLastError());
snprintf(cp->ErrorMessage, KWSYSPE_PIPE_BUFFER_SIZE,
"Process execution failed with error 0x%lX. "
"WideCharToMultiByte failed with error 0x%lX",
error, GetLastError());
}
}
@@ -2144,8 +2144,8 @@ static void kwsysProcessSetExitExceptionByIndex(kwsysProcess* cp, int code,
case STATUS_NO_MEMORY:
default:
cp->ProcessResults[idx].ExitException = kwsysProcess_Exception_Other;
_snprintf(cp->ProcessResults[idx].ExitExceptionString,
KWSYSPE_PIPE_BUFFER_SIZE, "Exit code 0x%x\n", code);
snprintf(cp->ProcessResults[idx].ExitExceptionString,
KWSYSPE_PIPE_BUFFER_SIZE, "Exit code 0x%x\n", code);
break;
}
}

View File

@@ -457,9 +457,9 @@ static void kwsys_shared_forward_strerror(char* message)
message, KWSYS_SHARED_FORWARD_MAXPATH, 0);
if (length < 1 || length > KWSYS_SHARED_FORWARD_MAXPATH) {
/* FormatMessage failed. Use a default message. */
_snprintf(message, KWSYS_SHARED_FORWARD_MAXPATH,
"Error 0x%X (FormatMessage failed with error 0x%X)", original,
GetLastError());
snprintf(message, KWSYS_SHARED_FORWARD_MAXPATH,
"Error 0x%lX (FormatMessage failed with error 0x%lX)", original,
GetLastError());
}
# else
/* Implementation for UNIX. */

View File

@@ -53,7 +53,7 @@ std::string Status::GetString() const
LocalFree(message);
} break;
#endif
};
}
return err;
}

View File

@@ -1267,7 +1267,9 @@ public:
private:
void* GetRealAddress() const
{
return (void*)((char*)this->Address - (char*)this->BinaryBaseAddress);
return reinterpret_cast<void*>(
static_cast<char*>(this->Address) -
static_cast<char*>(this->BinaryBaseAddress));
}
std::string GetFileName(const std::string& path) const;
@@ -2789,19 +2791,20 @@ bool SystemInformationImplementation::RetrieveProcessorSerialNumber()
// ; ecx: middle 32 bits are the processor signature bits
// ; edx: bottom 32 bits are the processor signature bits
char sn[128];
sprintf(sn, "%.2x%.2x-%.2x%.2x-%.2x%.2x-%.2x%.2x-%.2x%.2x-%.2x%.2x",
((SerialNumber[1] & 0xff000000) >> 24),
((SerialNumber[1] & 0x00ff0000) >> 16),
((SerialNumber[1] & 0x0000ff00) >> 8),
((SerialNumber[1] & 0x000000ff) >> 0),
((SerialNumber[2] & 0xff000000) >> 24),
((SerialNumber[2] & 0x00ff0000) >> 16),
((SerialNumber[2] & 0x0000ff00) >> 8),
((SerialNumber[2] & 0x000000ff) >> 0),
((SerialNumber[3] & 0xff000000) >> 24),
((SerialNumber[3] & 0x00ff0000) >> 16),
((SerialNumber[3] & 0x0000ff00) >> 8),
((SerialNumber[3] & 0x000000ff) >> 0));
snprintf(sn, sizeof(sn),
"%.2x%.2x-%.2x%.2x-%.2x%.2x-%.2x%.2x-%.2x%.2x-%.2x%.2x",
((SerialNumber[1] & 0xff000000) >> 24),
((SerialNumber[1] & 0x00ff0000) >> 16),
((SerialNumber[1] & 0x0000ff00) >> 8),
((SerialNumber[1] & 0x000000ff) >> 0),
((SerialNumber[2] & 0xff000000) >> 24),
((SerialNumber[2] & 0x00ff0000) >> 16),
((SerialNumber[2] & 0x0000ff00) >> 8),
((SerialNumber[2] & 0x000000ff) >> 0),
((SerialNumber[3] & 0xff000000) >> 24),
((SerialNumber[3] & 0x00ff0000) >> 16),
((SerialNumber[3] & 0x0000ff00) >> 8),
((SerialNumber[3] & 0x000000ff) >> 0));
this->ChipID.SerialNumber = sn;
return true;
@@ -3749,24 +3752,24 @@ long long SystemInformationImplementation::GetProcMemoryAvailable(
ResourceLimitType rlim;
ierr = GetResourceLimit(RLIMIT_DATA, &rlim);
if ((ierr == 0) && (rlim.rlim_cur != RLIM_INFINITY)) {
memAvail = min((long long)rlim.rlim_cur / 1024, memAvail);
memAvail = min(static_cast<long long>(rlim.rlim_cur) / 1024, memAvail);
}
ierr = GetResourceLimit(RLIMIT_AS, &rlim);
if ((ierr == 0) && (rlim.rlim_cur != RLIM_INFINITY)) {
memAvail = min((long long)rlim.rlim_cur / 1024, memAvail);
memAvail = min(static_cast<long long>(rlim.rlim_cur) / 1024, memAvail);
}
#elif defined(__APPLE__)
struct rlimit rlim;
int ierr;
ierr = getrlimit(RLIMIT_DATA, &rlim);
if ((ierr == 0) && (rlim.rlim_cur != RLIM_INFINITY)) {
memAvail = min((long long)rlim.rlim_cur / 1024, memAvail);
memAvail = min(static_cast<long long>(rlim.rlim_cur) / 1024, memAvail);
}
ierr = getrlimit(RLIMIT_RSS, &rlim);
if ((ierr == 0) && (rlim.rlim_cur != RLIM_INFINITY)) {
memAvail = min((long long)rlim.rlim_cur / 1024, memAvail);
memAvail = min(static_cast<long long>(rlim.rlim_cur) / 1024, memAvail);
}
#endif
@@ -4068,7 +4071,7 @@ void SystemInformationImplementation::SetStackTraceOnError(int enable)
// install ours
struct sigaction sa;
sa.sa_sigaction = (SigAction)StacktraceSignalHandler;
sa.sa_sigaction = static_cast<SigAction>(StacktraceSignalHandler);
sa.sa_flags = SA_SIGINFO | SA_RESETHAND;
# ifdef SA_RESTART
sa.sa_flags |= SA_RESTART;
@@ -4564,7 +4567,8 @@ bool SystemInformationImplementation::ParseSysCtl()
this->AvailablePhysicalMemory = 0;
vm_statistics_data_t vmstat;
mach_msg_type_number_t count = HOST_VM_INFO_COUNT;
if (host_statistics(mach_host_self(), HOST_VM_INFO, (host_info_t)&vmstat,
if (host_statistics(mach_host_self(), HOST_VM_INFO,
reinterpret_cast<host_info_t>(&vmstat),
&count) == KERN_SUCCESS) {
err = kw_sysctlbyname_int64("hw.pagesize", &tempInt64);
if (err == 0) {
@@ -5395,8 +5399,8 @@ bool SystemInformationImplementation::QueryOSInformation()
}
}
sprintf(operatingSystem, "%ls (Build %ld)", osvi.szCSDVersion,
osvi.dwBuildNumber & 0xFFFF);
snprintf(operatingSystem, sizeof(operatingSystem), "%ls (Build %ld)",
osvi.szCSDVersion, osvi.dwBuildNumber & 0xFFFF);
this->OSVersion = operatingSystem;
} else
# endif // VER_NT_WORKSTATION
@@ -5439,9 +5443,10 @@ bool SystemInformationImplementation::QueryOSInformation()
// Display version, service pack (if any), and build number.
if (osvi.dwMajorVersion <= 4) {
// NB: NT 4.0 and earlier.
sprintf(operatingSystem, "version %ld.%ld %ls (Build %ld)",
osvi.dwMajorVersion, osvi.dwMinorVersion, osvi.szCSDVersion,
osvi.dwBuildNumber & 0xFFFF);
snprintf(operatingSystem, sizeof(operatingSystem),
"version %ld.%ld %ls (Build %ld)", osvi.dwMajorVersion,
osvi.dwMinorVersion, osvi.szCSDVersion,
osvi.dwBuildNumber & 0xFFFF);
this->OSVersion = operatingSystem;
} else if (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 1) {
// Windows XP and .NET server.
@@ -5467,8 +5472,8 @@ bool SystemInformationImplementation::QueryOSInformation()
}
} else {
// Windows 2000 and everything else.
sprintf(operatingSystem, "%ls (Build %ld)", osvi.szCSDVersion,
osvi.dwBuildNumber & 0xFFFF);
snprintf(operatingSystem, sizeof(operatingSystem), "%ls (Build %ld)",
osvi.szCSDVersion, osvi.dwBuildNumber & 0xFFFF);
this->OSVersion = operatingSystem;
}
break;

View File

@@ -4552,10 +4552,10 @@ std::string SystemTools::GetOperatingSystemNameAndVersion()
}
res += " ";
sprintf(buffer, "%ld", osvi.dwMajorVersion);
snprintf(buffer, sizeof(buffer), "%ld", osvi.dwMajorVersion);
res += buffer;
res += ".";
sprintf(buffer, "%ld", osvi.dwMinorVersion);
snprintf(buffer, sizeof(buffer), "%ld", osvi.dwMinorVersion);
res += buffer;
}
@@ -4575,7 +4575,7 @@ std::string SystemTools::GetOperatingSystemNameAndVersion()
if (lRet == ERROR_SUCCESS) {
res += " Service Pack 6a (Build ";
sprintf(buffer, "%ld", osvi.dwBuildNumber & 0xFFFF);
snprintf(buffer, sizeof(buffer), "%ld", osvi.dwBuildNumber & 0xFFFF);
res += buffer;
res += ")";
} else // Windows NT 4.0 prior to SP6a
@@ -4583,7 +4583,7 @@ std::string SystemTools::GetOperatingSystemNameAndVersion()
res += " ";
res += osvi.szCSDVersion;
res += " (Build ";
sprintf(buffer, "%ld", osvi.dwBuildNumber & 0xFFFF);
snprintf(buffer, sizeof(buffer), "%ld", osvi.dwBuildNumber & 0xFFFF);
res += buffer;
res += ")";
}
@@ -4594,7 +4594,7 @@ std::string SystemTools::GetOperatingSystemNameAndVersion()
res += " ";
res += osvi.szCSDVersion;
res += " (Build ";
sprintf(buffer, "%ld", osvi.dwBuildNumber & 0xFFFF);
snprintf(buffer, sizeof(buffer), "%ld", osvi.dwBuildNumber & 0xFFFF);
res += buffer;
res += ")";
}

View File

@@ -19,7 +19,7 @@ file Copyright.txt or https://cmake.org/licensing#kwsys for details. */
#include <testSystemTools.h>
int _doLongPathTest()
static int _doLongPathTest()
{
using namespace kwsys;
static const int LONG_PATH_THRESHOLD = 512;
@@ -77,7 +77,7 @@ int _doLongPathTest()
return res;
}
int _nonExistentDirectoryTest()
static int _nonExistentDirectoryTest()
{
using namespace kwsys;
int res = 0;
@@ -105,7 +105,7 @@ int _nonExistentDirectoryTest()
return res;
}
int _copyDirectoryTest()
static int _copyDirectoryTest()
{
using namespace kwsys;
const std::string source(TEST_SYSTEMTOOLS_BINARY_DIR

View File

@@ -11,20 +11,20 @@
// Needed for __GLIBC__ test macro.
#ifdef __linux__
# include <features.h>
#endif
// Will define LIBDL_SO macro on systems with glibc.
#ifdef __GLIBC__
# include <gnu/lib-names.h>
# ifdef __GLIBC__
# include <gnu/lib-names.h>
// Define to LIBC_SO, if not defined by above header.
# ifndef LIBDL_SO
# define LIBDL_SO LIBC_SO
# ifndef LIBDL_SO
# define LIBDL_SO LIBC_SO
# endif
# endif
#endif
// Define the LIBDL_SO macro, if not defined above.
#ifndef LIBDL_SO
# define LIBDL_SO "libdl.so"
# ifndef LIBDL_SO
# define LIBDL_SO "libdl.so"
# endif
#endif
// Work-around CMake dependency scanning limitation. This must
@@ -40,6 +40,10 @@
// left on disk.
#include <testSystemTools.h>
// For TestDynamicLoaderData, which, though not referenced literally,
// is referenced semantically.
#include "testDynload.h"
static std::string GetLibName(const char* lname, const char* subdir = nullptr)
{
// Construct proper name of lib

View File

@@ -6,6 +6,8 @@
# define DL_EXPORT
#endif
#include "testDynload.h"
DL_EXPORT int TestDynamicLoaderData = 0;
DL_EXPORT void TestDynamicLoaderSymbolPointer(void)

9
testDynload.h Normal file
View File

@@ -0,0 +1,9 @@
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file Copyright.txt or https://cmake.org/licensing#kwsys for details. */
#ifdef _WIN32
# define DL_EXPORT __declspec(dllexport)
#else
# define DL_EXPORT
#endif
extern DL_EXPORT int TestDynamicLoaderData;

View File

@@ -80,7 +80,7 @@ static int testRobustEncoding()
std::ios::fmtflags const& flags = std::cout.flags();
int ret = 0;
char cstr[] = { (char)-1, 0 };
char cstr[] = { static_cast<char>(-1), 0 };
// this conversion could fail
std::wstring wstr = kwsys::Encoding::ToWide(cstr);
@@ -89,7 +89,7 @@ static int testRobustEncoding()
const wchar_t* wcstr = wstr.c_str();
std::cout << "ToWide(NULL) returned";
for (size_t i = 0; i < wstr.size(); i++) {
std::cout << " " << std::hex << (int)wcstr[i];
std::cout << " " << std::hex << static_cast<int>(wcstr[i]);
}
std::cout << std::endl;
ret++;
@@ -99,7 +99,7 @@ static int testRobustEncoding()
const wchar_t* wcstr = wstr.c_str();
std::cout << "ToWide(\"\") returned";
for (size_t i = 0; i < wstr.size(); i++) {
std::cout << " " << std::hex << (int)wcstr[i];
std::cout << " " << std::hex << static_cast<int>(wcstr[i]);
}
std::cout << std::endl;
ret++;
@@ -160,7 +160,9 @@ static int testCommandLineArguments()
{
int status = 0;
char const* argv[2] = { "./app.exe", (char const*)helloWorldStrings[1] };
char const* argv[2] = {
"./app.exe", reinterpret_cast<char const*>(helloWorldStrings[1])
};
kwsys::Encoding::CommandLineArguments args(2, argv);
kwsys::Encoding::CommandLineArguments arg2 =