Fix src/libraries to build on clang 10 (#33734)
authorOmair Majid <omajid@redhat.com>
Fri, 20 Mar 2020 00:23:36 +0000 (20:23 -0400)
committerGitHub <noreply@github.com>
Fri, 20 Mar 2020 00:23:36 +0000 (17:23 -0700)
Clang 10 enable new warnings, some of which is affecting the
src/libraries code.

Clang 10 has added `-Walloca` to warn about uses of `alloca`. This
commit replaces the only non-compliant use of that with a single fixed
stack-allocated buffer.

Clang 10 has also added `-Wimplicit-int-float-conversion`. This commit
uses explicit casts to double to avoid the warnings.

Fixes #33681

Also contains a small fix for slist.h that was somehow missed in #33096.

After this commit, I can build all of runtime with Clang 10.

src/coreclr/src/inc/slist.h
src/libraries/Native/Unix/System.Native/pal_io.c
src/libraries/Native/Unix/System.Native/pal_time.c

index f5d0312..87dec1d 100644 (file)
@@ -274,7 +274,7 @@ public:
         SLink   *ret = SLink::FindAndRemove(m_pHead, GetLink(pObj), &prior);
 
         if (ret == m_pTail)
-            m_pTail = prior;
+            m_pTail = PTR_SLink(prior);
 
         return GetObject(ret);
     }
index 5a8ecba..8706e13 100644 (file)
@@ -913,18 +913,20 @@ int32_t SystemNative_Poll(PollEvent* pollEvents, uint32_t eventCount, int32_t mi
         return Error_EINVAL;
     }
 
-    size_t bufferSize;
-    if (!multiply_s(sizeof(struct pollfd), (size_t)eventCount, &bufferSize))
+    struct pollfd stackBuffer[(uint32_t)(2048/sizeof(struct pollfd))];
+    int useStackBuffer = eventCount <= (sizeof(stackBuffer)/sizeof(stackBuffer[0]));
+    struct pollfd* pollfds = NULL;
+    if (useStackBuffer)
     {
-        return SystemNative_ConvertErrorPlatformToPal(EOVERFLOW);
+        pollfds = (struct pollfd*)&stackBuffer[0];
     }
-
-
-    int useStackBuffer = bufferSize <= 2048;
-    struct pollfd* pollfds = (struct pollfd*)(useStackBuffer ? alloca(bufferSize) : malloc(bufferSize));
-    if (pollfds == NULL)
+    else
     {
-        return Error_ENOMEM;
+        pollfds = (struct pollfd*)calloc(eventCount, sizeof(*pollfds));
+        if (pollfds == NULL)
+        {
+            return Error_ENOMEM;
+        }
     }
 
     for (uint32_t i = 0; i < eventCount; i++)
index ce22f91..924f3a6 100644 (file)
@@ -117,7 +117,7 @@ int32_t SystemNative_GetCpuUtilization(ProcessCpuInformation* previousCpuInfo)
     uint64_t resolution = SystemNative_GetTimestampResolution();
     uint64_t timestamp = SystemNative_GetTimestamp();
 
-    uint64_t currentTime = (uint64_t)(timestamp * ((double)SecondsToNanoSeconds / resolution));
+    uint64_t currentTime = (uint64_t)((double)timestamp * ((double)SecondsToNanoSeconds / (double)resolution));
 
     uint64_t lastRecordedCurrentTime = previousCpuInfo->lastRecordedCurrentTime;
     uint64_t lastRecordedKernelTime = previousCpuInfo->lastRecordedKernelTime;