From 487d7f010b90a82178d55eea21e72dc6dedf9d5d Mon Sep 17 00:00:00 2001 From: Raphtaliyah Date: Thu, 16 Nov 2023 15:39:13 +0100 Subject: [PATCH] Fix build issues with clang18 (#94782) * Fixed build issues with clang18 * Convert use of VLAs to alloca * Enable warnings for VLAs * Convert VLA to alloca in Apple specific code --- eng/native/configurecompiler.cmake | 5 +++++ src/coreclr/gc/unix/gcenv.unix.cpp | 10 +++++----- src/coreclr/pal/src/file/path.cpp | 6 +++--- src/coreclr/pal/src/include/pal/palinternal.h | 1 + src/native/minipal/getexepath.h | 2 +- 5 files changed, 15 insertions(+), 9 deletions(-) diff --git a/eng/native/configurecompiler.cmake b/eng/native/configurecompiler.cmake index 6fc480fb20aab..2b2a6c0e0a349 100644 --- a/eng/native/configurecompiler.cmake +++ b/eng/native/configurecompiler.cmake @@ -564,6 +564,11 @@ if (CLR_CMAKE_HOST_UNIX) add_compile_options(-Wimplicit-fallthrough) endif() + # VLAs are non standard in C++, aren't available on Windows and + # are a warning by default since clang 18. + # For consistency, enable warnings for all compiler versions. + add_compile_options($<$:-Wvla>) + #These seem to indicate real issues add_compile_options($<$:-Wno-invalid-offsetof>) diff --git a/src/coreclr/gc/unix/gcenv.unix.cpp b/src/coreclr/gc/unix/gcenv.unix.cpp index 62680cf1428e2..c211a13651940 100644 --- a/src/coreclr/gc/unix/gcenv.unix.cpp +++ b/src/coreclr/gc/unix/gcenv.unix.cpp @@ -631,9 +631,9 @@ bool GCToOSInterface::VirtualCommit(void* address, size_t size, uint16_t node) if ((int)node <= g_highestNumaNode) { int usedNodeMaskBits = g_highestNumaNode + 1; - int nodeMaskLength = (usedNodeMaskBits + sizeof(unsigned long) - 1) / sizeof(unsigned long); - unsigned long nodeMask[nodeMaskLength]; - memset(nodeMask, 0, sizeof(nodeMask)); + int nodeMaskLength = usedNodeMaskBits + sizeof(unsigned long) - 1; + unsigned long* nodeMask = (unsigned long*)alloca(nodeMaskLength); + memset(nodeMask, 0, nodeMaskLength); int index = node / sizeof(unsigned long); nodeMask[index] = ((unsigned long)1) << (node & (sizeof(unsigned long) - 1)); @@ -1189,10 +1189,10 @@ uint64_t GetAvailablePhysicalMemory() #elif defined(__FreeBSD__) size_t inactive_count = 0, laundry_count = 0, free_count = 0; size_t sz = sizeof(inactive_count); - sysctlbyname("vm.stats.vm.v_inactive_count", &inactive_count, &sz, NULL, 0); + sysctlbyname("vm.stats.vm.v_inactive_count", &inactive_count, &sz, NULL, 0); sz = sizeof(laundry_count); - sysctlbyname("vm.stats.vm.v_laundry_count", &laundry_count, &sz, NULL, 0); + sysctlbyname("vm.stats.vm.v_laundry_count", &laundry_count, &sz, NULL, 0); sz = sizeof(free_count); sysctlbyname("vm.stats.vm.v_free_count", &free_count, &sz, NULL, 0); diff --git a/src/coreclr/pal/src/file/path.cpp b/src/coreclr/pal/src/file/path.cpp index 92383dd204ace..62418b240d3fe 100644 --- a/src/coreclr/pal/src/file/path.cpp +++ b/src/coreclr/pal/src/file/path.cpp @@ -399,8 +399,8 @@ GetTempPathW( return 0; } - char TempBuffer[nBufferLength > 0 ? nBufferLength : 1]; - DWORD dwRetVal = GetTempPathA( nBufferLength, TempBuffer ); + char* tempBuffer = (char*)alloca(nBufferLength > 0 ? nBufferLength : 1); + DWORD dwRetVal = GetTempPathA( nBufferLength, tempBuffer ); if ( dwRetVal >= nBufferLength ) { @@ -411,7 +411,7 @@ GetTempPathW( else if ( dwRetVal != 0 ) { /* Convert to wide. */ - if ( 0 == MultiByteToWideChar( CP_ACP, 0, TempBuffer, -1, + if ( 0 == MultiByteToWideChar( CP_ACP, 0, tempBuffer, -1, lpBuffer, dwRetVal + 1 ) ) { ASSERT( "An error occurred while converting the string to wide.\n" ); diff --git a/src/coreclr/pal/src/include/pal/palinternal.h b/src/coreclr/pal/src/include/pal/palinternal.h index 644918728edf1..63e6d305d9f6e 100644 --- a/src/coreclr/pal/src/include/pal/palinternal.h +++ b/src/coreclr/pal/src/include/pal/palinternal.h @@ -426,6 +426,7 @@ function_name() to call the system's implementation #undef va_start #undef va_end #undef va_copy +#undef va_arg #undef stdin #undef stdout #undef stderr diff --git a/src/native/minipal/getexepath.h b/src/native/minipal/getexepath.h index dfee16910ad4d..601447a1af215 100644 --- a/src/native/minipal/getexepath.h +++ b/src/native/minipal/getexepath.h @@ -37,7 +37,7 @@ static inline char* minipal_getexepath(void) return NULL; } - char path_buf[path_length]; + char* path_buf = (char*)alloca(path_length); if (_NSGetExecutablePath(path_buf, &path_length) != 0) { errno = EINVAL;