[tepl] EncodingConverter: fix fixme about errno



commit 57592b16eefd0fade02f053f8077f9e0b6d95729
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Mon Nov 4 11:10:00 2019 +0100

    EncodingConverter: fix fixme about errno
    
    See for example the doc of g_strerror(), it recommends to save the value
    of errno as soon as possible.
    
    And in the errno(3) manpage, we can read:
    "The value of errno is never set to zero  by  any  system
    call or library function."
    so it means that to recover from an error, we need to set errno to 0
    just after saving its value, so that further functions that can
    potentially set errno will not return a garbage errno value.

 tepl/tepl-encoding-converter.c | 22 +++++++++++++---------
 1 file changed, 13 insertions(+), 9 deletions(-)
---
diff --git a/tepl/tepl-encoding-converter.c b/tepl/tepl-encoding-converter.c
index 6c23ba5..93a9f20 100644
--- a/tepl/tepl-encoding-converter.c
+++ b/tepl/tepl-encoding-converter.c
@@ -1,7 +1,7 @@
 /*
  * This file is part of Tepl, a text editor library.
  *
- * Copyright 2016 - Sébastien Wilmet <swilmet gnome org>
+ * Copyright 2016, 2019 - Sébastien Wilmet <swilmet gnome org>
  *
  * Tepl is free software; you can redistribute it and/or modify it under
  * the terms of the GNU Lesser General Public License as published by the
@@ -39,8 +39,6 @@
  *   callback is called.
  */
 
-/* FIXME: I think errno should be reset to 0 after an errno error. */
-
 struct _TeplEncodingConverterPrivate
 {
        GIConv conv;
@@ -296,7 +294,10 @@ _tepl_encoding_converter_open (TeplEncodingConverter  *converter,
 
        if (converter->priv->conv == (GIConv)-1)
        {
-               if (errno == EINVAL)
+               gint saved_errno = errno;
+               errno = 0;
+
+               if (saved_errno == EINVAL)
                {
                        g_set_error (error,
                                     G_CONVERT_ERROR,
@@ -313,7 +314,7 @@ _tepl_encoding_converter_open (TeplEncodingConverter  *converter,
                                     _("Could not open converter from “%s” to “%s”: %s"),
                                     from_codeset,
                                     to_codeset,
-                                    g_strerror (errno));
+                                    g_strerror (saved_errno));
                }
 
                return FALSE;
@@ -352,17 +353,20 @@ read_inbuf (TeplEncodingConverter  *converter,
 
                if (iconv_ret == (gsize)-1)
                {
+                       gint saved_errno = errno;
+                       errno = 0;
+
                        /* outbuf full */
-                       if (errno == E2BIG)
+                       if (saved_errno == E2BIG)
                        {
                                flush_outbuf (converter);
                                continue;
                        }
-                       else if (errno == EINVAL)
+                       else if (saved_errno == EINVAL)
                        {
                                return RESULT_INCOMPLETE_INPUT;
                        }
-                       else if (errno == EILSEQ)
+                       else if (saved_errno == EILSEQ)
                        {
                                g_set_error_literal (error,
                                                     G_CONVERT_ERROR,
@@ -377,7 +381,7 @@ read_inbuf (TeplEncodingConverter  *converter,
                                             G_IO_ERROR,
                                             G_IO_ERROR_FAILED,
                                             _("Error when converting data: %s"),
-                                            g_strerror (errno));
+                                            g_strerror (saved_errno));
 
                                return RESULT_ERROR;
                        }


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