[glib: 1/2] gmessages: Handle unused results from fputs and fwrite




commit 977b734e0bd7e2bbe49b02017acaadbcabfb56ee
Author: Marco Trevisan (TreviƱo) <mail 3v1n0 net>
Date:   Tue Sep 20 15:57:27 2022 +0200

    gmessages: Handle unused results from fputs and fwrite
    
    Fixes: #2758

 glib/gmessages.c | 72 +++++++++++++++++++++++++++++++++-----------------------
 1 file changed, 43 insertions(+), 29 deletions(-)
---
diff --git a/glib/gmessages.c b/glib/gmessages.c
index bbea253751..55c7dfd35c 100644
--- a/glib/gmessages.c
+++ b/glib/gmessages.c
@@ -601,7 +601,12 @@ static void
 write_string (FILE        *stream,
              const gchar *string)
 {
-  fputs (string, stream);
+  if (fputs (string, stream) == EOF)
+    {
+      /* Something failed, but it's not an error we can handle at glib level
+       * so let's just continue without the compiler blaming us
+       */
+    }
 }
 
 static void
@@ -612,8 +617,12 @@ write_string_sized (FILE        *stream,
   /* Is it nul-terminated? */
   if (length < 0)
     write_string (stream, string);
-  else
-    fwrite (string, 1, length, stream);
+  else if (fwrite (string, 1, length, stream) < (size_t) length)
+    {
+      /* Something failed, but it's not an error we can handle at glib level
+       * so let's just continue without the compiler blaming us
+       */
+    }
 }
 
 static GLogDomain*
@@ -3316,6 +3325,35 @@ g_set_print_handler (GPrintFunc func)
   return old_print_func;
 }
 
+static void
+print_string (FILE        *stream,
+              const gchar *string)
+{
+  const gchar *charset;
+  int ret;
+
+  if (g_get_console_charset (&charset))
+    {
+      /* charset is UTF-8 already */
+      ret = fputs (string, stream);
+    }
+  else
+    {
+      gchar *converted_string = strdup_convert (string, charset);
+
+      ret = fputs (converted_string, stream);
+      g_free (converted_string);
+    }
+
+  /* In case of failure we can just return early, but there's nothing else
+   * we can do at this level
+   */
+  if (ret == EOF)
+    return;
+
+  fflush (stream);
+}
+
 /**
  * g_print:
  * @format: the message format. See the printf() documentation
@@ -3353,20 +3391,8 @@ g_print (const gchar *format,
   if (local_glib_print_func)
     local_glib_print_func (string);
   else
-    {
-      const gchar *charset;
-
-      if (g_get_console_charset (&charset))
-        fputs (string, stdout); /* charset is UTF-8 already */
-      else
-        {
-          gchar *lstring = strdup_convert (string, charset);
+    print_string (stdout, string);
 
-          fputs (lstring, stdout);
-          g_free (lstring);
-        }
-      fflush (stdout);
-    }
   g_free (string);
 }
 
@@ -3432,20 +3458,8 @@ g_printerr (const gchar *format,
   if (local_glib_printerr_func)
     local_glib_printerr_func (string);
   else
-    {
-      const gchar *charset;
-
-      if (g_get_console_charset (&charset))
-        fputs (string, stderr); /* charset is UTF-8 already */
-      else
-        {
-          gchar *lstring = strdup_convert (string, charset);
+    print_string (stderr, string);
 
-          fputs (lstring, stderr);
-          g_free (lstring);
-        }
-      fflush (stderr);
-    }
   g_free (string);
 }
 


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