Browse Source

Fix default vprintf on UWP (#2730)

`platform_common.h` already has a declaration for BH_VPRINTF so we can
get rid of the one in `platform_internal.h`. Also add some explicit
casts to avoid MSVC compiler warnings.
zoraaver 2 years ago
parent
commit
fa2839a805

+ 0 - 3
core/shared/platform/windows/platform_internal.h

@@ -183,9 +183,6 @@ typedef uint32_t os_raw_file_handle;
 // implementation of vprintf on debug builds so output from WASI libc is sent to
 // the debugger and not lost completely.
 #if !defined(BH_VPRINTF) && !defined(NDEBUG) && WINAPI_PARTITION_DESKTOP == 0
-int
-uwp_print_to_debugger(const char *format, va_list ap);
-
 #define BH_VPRINTF uwp_print_to_debugger
 #define UWP_DEFAULT_VPRINTF
 #endif

+ 8 - 5
core/shared/platform/windows/win_util.c

@@ -3,6 +3,7 @@
  * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  */
 
+#include "platform_common.h"
 #include "win_util.h"
 
 __wasi_timestamp_t
@@ -69,17 +70,19 @@ uwp_print_to_debugger(const char *format, va_list ap)
     char *buf = stack_buf;
     int ret = vsnprintf(stack_buf, sizeof(stack_buf), format, ap);
 
-    if (ret >= sizeof(stack_buf)) {
+    if ((size_t)ret >= sizeof(stack_buf)) {
         // Allocate an extra byte for the null terminator.
-        char *heap_buf = BH_MALLOC(ret + 1);
+        char *heap_buf = BH_MALLOC((unsigned int)(ret) + 1);
         buf = heap_buf;
 
         if (heap_buf == NULL) {
-            errno = ENOMEM;
-            return -1;
+            // Output as much as we can to the debugger if allocating a buffer
+            // fails.
+            OutputDebugStringA(stack_buf);
+            return ret;
         }
 
-        ret = vsnprintf(heap_buf, ret + 1, format, ap);
+        ret = vsnprintf(heap_buf, (size_t)ret + 1, format, ap);
     }
 
     if (ret >= 0)