[glib: 1/2] win32: don't assume the format specifier for the stdlib printf/scanf like functions



commit 6095b9bd3c9a1870e218d8773d2cd77670769af9
Author: Christoph Reiter <reiter christoph gmail com>
Date:   Sat Aug 10 19:55:53 2019 +0200

    win32: don't assume the format specifier for the stdlib printf/scanf like functions
    
    When using the mingw printf shims for C99 compat the msvc format specifiers don't work
    and the build fails.
    
    Ideally we would use glib functions which abstract this away, but in the error handler context
    we shouldn't call back into glib. And for scanf we don't have a glib wrapper.
    
    Instead call the "secure" versions provided by the win32 API (_snprintf_s/fprintf_s/sscanf_s)
    which mingw doesn't replace.

 glib/gwin32-private.c |  7 ++++---
 glib/gwin32.c         | 42 +++++++++++++++++++++---------------------
 tests/type-test.c     |  2 ++
 3 files changed, 27 insertions(+), 24 deletions(-)
---
diff --git a/glib/gwin32-private.c b/glib/gwin32-private.c
index 917bcd1b5..f7913b553 100644
--- a/glib/gwin32-private.c
+++ b/glib/gwin32-private.c
@@ -39,13 +39,14 @@ _g_win32_subst_pid_and_event (char       *debugger,
   gsize pid_str_len;
   char event_str[STR_BUFFER_SIZE] = {0};
   gsize event_str_len;
-#undef STR_BUFFER_SIZE
-  snprintf (pid_str, G_N_ELEMENTS (pid_str), "%lu", pid);
+
+  _snprintf_s (pid_str, STR_BUFFER_SIZE, G_N_ELEMENTS (pid_str), "%lu", pid);
   pid_str[G_N_ELEMENTS (pid_str) - 1] = 0;
   pid_str_len = strlen (pid_str);
-  snprintf (event_str, G_N_ELEMENTS (pid_str), "%Iu", event);
+  _snprintf_s (event_str, STR_BUFFER_SIZE, G_N_ELEMENTS (pid_str), "%Iu", event);
   event_str[G_N_ELEMENTS (pid_str) - 1] = 0;
   event_str_len = strlen (event_str);
+#undef STR_BUFFER_SIZE
 
   while (cmdline[i] != 0 && dbg_i < debugger_size)
     {
diff --git a/glib/gwin32.c b/glib/gwin32.c
index 7c724badb..a8847482c 100644
--- a/glib/gwin32.c
+++ b/glib/gwin32.c
@@ -1120,35 +1120,35 @@ g_win32_veh_handler (PEXCEPTION_POINTERS ExceptionInfo)
       return EXCEPTION_CONTINUE_EXECUTION;
     }
 
-  fprintf (stderr,
-           "Exception code=0x%lx flags=0x%lx at 0x%p",
-           er->ExceptionCode,
-           er->ExceptionFlags,
-           er->ExceptionAddress);
+  fprintf_s (stderr,
+             "Exception code=0x%lx flags=0x%lx at 0x%p",
+             er->ExceptionCode,
+             er->ExceptionFlags,
+             er->ExceptionAddress);
 
   switch (er->ExceptionCode)
     {
     case EXCEPTION_ACCESS_VIOLATION:
-      fprintf (stderr,
-               ". Access violation - attempting to %s at address 0x%p\n",
-               er->ExceptionInformation[0] == 0 ? "read data" :
-               er->ExceptionInformation[0] == 1 ? "write data" :
-               er->ExceptionInformation[0] == 8 ? "execute data" :
-               "do something bad",
-               (void *) er->ExceptionInformation[1]);
+      fprintf_s (stderr,
+                 ". Access violation - attempting to %s at address 0x%p\n",
+                 er->ExceptionInformation[0] == 0 ? "read data" :
+                 er->ExceptionInformation[0] == 1 ? "write data" :
+                 er->ExceptionInformation[0] == 8 ? "execute data" :
+                 "do something bad",
+                 (void *) er->ExceptionInformation[1]);
       break;
     case EXCEPTION_IN_PAGE_ERROR:
-      fprintf (stderr,
-               ". Page access violation - attempting to %s at address 0x%p with status %Ix\n",
-               er->ExceptionInformation[0] == 0 ? "read from an inaccessible page" :
-               er->ExceptionInformation[0] == 1 ? "write to an inaccessible page" :
-               er->ExceptionInformation[0] == 8 ? "execute data in page" :
-               "do something bad with a page",
-               (void *) er->ExceptionInformation[1],
-               er->ExceptionInformation[2]);
+      fprintf_s (stderr,
+                 ". Page access violation - attempting to %s at address 0x%p with status %Ix\n",
+                 er->ExceptionInformation[0] == 0 ? "read from an inaccessible page" :
+                 er->ExceptionInformation[0] == 1 ? "write to an inaccessible page" :
+                 er->ExceptionInformation[0] == 8 ? "execute data in page" :
+                 "do something bad with a page",
+                 (void *) er->ExceptionInformation[1],
+                 er->ExceptionInformation[2]);
       break;
     default:
-      fprintf (stderr, "\n");
+      fprintf_s (stderr, "\n");
       break;
     }
 
diff --git a/tests/type-test.c b/tests/type-test.c
index 43da39472..dceccdd9f 100644
--- a/tests/type-test.c
+++ b/tests/type-test.c
@@ -123,6 +123,7 @@ main (int   argc,
 #ifndef G_OS_WIN32
 #  define SCAN_FORMAT64 FORMAT64
 #else
+#  define sscanf sscanf_s
 #  define SCAN_FORMAT64 "%I64d %I64u\n"
 #endif
   string = g_strdup_printf (FORMAT64, gi64t1, gu64t1);
@@ -138,6 +139,7 @@ main (int   argc,
 #ifndef G_OS_WIN32
 #  define SCAN_FORMATSIZE FORMATSIZE
 #else
+#  define sscanf sscanf_s
 #  define SCAN_FORMATSIZE "%Id %Iu\n"
 #endif
   string = g_strdup_printf (FORMATSIZE, gsst1, gst1);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]