[glib/glib-2-20] Preserve errno



commit 0bfd98400387acb2eaddbe4bdac4e821e7e4f0f4
Author: Christian Persch <chpe gnome org>
Date:   Thu Aug 20 15:13:43 2009 +0200

    Preserve errno
    
    When using errno in g_set_error with _(), preserve errno. Bug #592457.

 glib/gconvert.c     |   22 +++++++++++++++-------
 glib/gdir.c         |   13 +++++++++----
 glib/gspawn-win32.c |    9 ++++++---
 glib/gspawn.c       |   27 +++++++++++++++++++--------
 4 files changed, 49 insertions(+), 22 deletions(-)
---
diff --git a/glib/gconvert.c b/glib/gconvert.c
index 7aefe24..7a20cb5 100644
--- a/glib/gconvert.c
+++ b/glib/gconvert.c
@@ -633,10 +633,13 @@ g_convert_with_iconv (const gchar *str,
 	      have_error = TRUE;
 	      break;
 	    default:
-	      if (error)
-		g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_FAILED,
-			     _("Error during conversion: %s"),
-			     g_strerror (errno));
+              {
+                int errsv = errno;
+                
+                g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_FAILED,
+                             _("Error during conversion: %s"),
+                             g_strerror (errsv));
+              }
 	      have_error = TRUE;
 	      break;
 	    }
@@ -940,9 +943,14 @@ g_convert_with_fallback (const gchar *str,
 		}
 	      /* fall thru if p is NULL */
 	    default:
-	      g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_FAILED,
-			   _("Error during conversion: %s"),
-			   g_strerror (errno));
+              {
+                int errsv = errno;
+
+                g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_FAILED,
+                             _("Error during conversion: %s"),
+                             g_strerror (errsv));
+              }
+
 	      have_error = TRUE;
 	      break;
 	    }
diff --git a/glib/gdir.c b/glib/gdir.c
index b704f30..c6997ae 100644
--- a/glib/gdir.c
+++ b/glib/gdir.c
@@ -80,6 +80,7 @@ g_dir_open (const gchar  *path,
             GError      **error)
 {
   GDir *dir;
+  int errsv;
 #ifdef G_OS_WIN32
   wchar_t *wpath;
 #else
@@ -103,12 +104,13 @@ g_dir_open (const gchar  *path,
     return dir;
 
   /* error case */
+  errsv = errno;
 
   g_set_error (error,
 	       G_FILE_ERROR,
-	       g_file_error_from_errno (errno),
+	       g_file_error_from_errno (errsv),
 	       _("Error opening directory '%s': %s"),
-	       path, g_strerror (errno));
+	       path, g_strerror (errsv));
   
   g_free (dir);
       
@@ -122,13 +124,16 @@ g_dir_open (const gchar  *path,
     return dir;
 
   /* error case */
+  errsv = errno;
+
   utf8_path = g_filename_to_utf8 (path, -1,
 				  NULL, NULL, NULL);
+
   g_set_error (error,
                G_FILE_ERROR,
-               g_file_error_from_errno (errno),
+               g_file_error_from_errno (errsv),
                _("Error opening directory '%s': %s"),
-	       utf8_path, g_strerror (errno));
+	       utf8_path, g_strerror (errsv));
 
   g_free (utf8_path);
   g_free (dir);
diff --git a/glib/gspawn-win32.c b/glib/gspawn-win32.c
index ba7970b..dc48a25 100644
--- a/glib/gspawn-win32.c
+++ b/glib/gspawn-win32.c
@@ -294,9 +294,11 @@ make_pipe (gint     p[2],
 {
   if (_pipe (p, 4096, _O_BINARY) < 0)
     {
+      int errsv = errno;
+
       g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_FAILED,
                    _("Failed to create pipe for communicating with child process (%s)"),
-                   g_strerror (errno));
+                   g_strerror (errsv));
       return FALSE;
     }
   else
@@ -330,11 +332,12 @@ read_helper_report (int      fd,
           
       if (chunk < 0)
         {
+          int errsv = errno;
+
           /* Some weird shit happened, bail out */
-              
           g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_FAILED,
                        _("Failed to read from child pipe (%s)"),
-                       g_strerror (errno));
+                       g_strerror (errsv));
 
           return FALSE;
         }
diff --git a/glib/gspawn.c b/glib/gspawn.c
index 406ef2b..1425450 100644
--- a/glib/gspawn.c
+++ b/glib/gspawn.c
@@ -182,11 +182,13 @@ read_data (GString *str,
     goto again;
   else if (bytes < 0)
     {
+      int errsv = errno;
+
       g_set_error (error,
                    G_SPAWN_ERROR,
                    G_SPAWN_ERROR_READ,
                    _("Failed to read data from child process (%s)"),
-                   g_strerror (errno));
+                   g_strerror (errsv));
       
       return READ_FAILED;
     }
@@ -317,13 +319,15 @@ g_spawn_sync (const gchar          *working_directory,
 
       if (ret < 0 && errno != EINTR)
         {
+          int errsv = errno;
+
           failed = TRUE;
 
           g_set_error (error,
                        G_SPAWN_ERROR,
                        G_SPAWN_ERROR_READ,
                        _("Unexpected error in select() reading data from a child process (%s)"),
-                       g_strerror (errno));
+                       g_strerror (errsv));
               
           break;
         }
@@ -400,13 +404,15 @@ g_spawn_sync (const gchar          *working_directory,
         {
           if (!failed) /* avoid error pileups */
             {
+              int errsv = errno;
+
               failed = TRUE;
                   
               g_set_error (error,
                            G_SPAWN_ERROR,
                            G_SPAWN_ERROR_READ,
                            _("Unexpected error in waitpid() (%s)"),
-                           g_strerror (errno));
+                           g_strerror (errsv));
             }
         }
     }
@@ -1124,13 +1130,14 @@ read_ints (int      fd,
           
       if (chunk < 0)
         {
+          int errsv = errno;
+
           /* Some weird shit happened, bail out */
-              
           g_set_error (error,
                        G_SPAWN_ERROR,
                        G_SPAWN_ERROR_FAILED,
                        _("Failed to read from child pipe (%s)"),
-                       g_strerror (errno));
+                       g_strerror (errsv));
 
           return FALSE;
         }
@@ -1190,12 +1197,14 @@ fork_exec_with_pipes (gboolean              intermediate_child,
   pid = fork ();
 
   if (pid < 0)
-    {      
+    {
+      int errsv = errno;
+
       g_set_error (error,
                    G_SPAWN_ERROR,
                    G_SPAWN_ERROR_FORK,
                    _("Failed to fork (%s)"),
-                   g_strerror (errno));
+                   g_strerror (errsv));
 
       goto cleanup_and_fail;
     }
@@ -1390,11 +1399,13 @@ fork_exec_with_pipes (gboolean              intermediate_child,
 
           if (n_ints < 1)
             {
+              int errsv = errno;
+
               g_set_error (error,
                            G_SPAWN_ERROR,
                            G_SPAWN_ERROR_FAILED,
                            _("Failed to read enough data from child pid pipe (%s)"),
-                           g_strerror (errno));
+                           g_strerror (errsv));
               goto cleanup_and_fail;
             }
           else



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