[gnome-online-accounts/gnome-3-18] imap-auth-login, smtp-auth, utils: Always set the GError on failure



commit e83e12ac54bee3b8a506daf9072a4b0209d515b0
Author: Debarshi Ray <debarshir gnome org>
Date:   Tue Nov 24 19:13:40 2015 +0100

    imap-auth-login, smtp-auth, utils: Always set the GError on failure
    
    Bad things will happen if we return FALSE to indicate a failure, but
    don't set the GError.
    
    We are internally using g_data_input_stream_read_line which returns
    NULL when there is no content to read, and it won't set its GError
    because this isn't a failure in itself. However, our methods have
    different semantics. Any unexpected absence of content is a failure,
    and we promise to return FALSE with the GError set. Therefore, we
    should handle this ourself.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=758361

 src/goabackend/goaimapauthlogin.c |   18 +++++++++---------
 src/goabackend/goasmtpauth.c      |   12 ++++++------
 src/goabackend/goautils.c         |   28 ++++++++++++++++++++++++++++
 src/goabackend/goautils.h         |    7 ++++++-
 4 files changed, 49 insertions(+), 16 deletions(-)
---
diff --git a/src/goabackend/goaimapauthlogin.c b/src/goabackend/goaimapauthlogin.c
index c0013fb..c561d4e 100644
--- a/src/goabackend/goaimapauthlogin.c
+++ b/src/goabackend/goaimapauthlogin.c
@@ -506,7 +506,7 @@ goa_imap_auth_login_run_sync (GoaMailAuth         *auth,
 
   if (!self->greeting_absent)
     {
-      response = g_data_input_stream_read_line (input, NULL, cancellable, error);
+      response = goa_utils_data_input_stream_read_line (input, NULL, cancellable, error);
       if (response == NULL)
         goto out;
       g_debug ("< %s", response);
@@ -527,7 +527,7 @@ goa_imap_auth_login_run_sync (GoaMailAuth         *auth,
 
   /* Check if LOGIN is supported or not */
 
-  response = g_data_input_stream_read_line (input, NULL, cancellable, error);
+  response = goa_utils_data_input_stream_read_line (input, NULL, cancellable, error);
   if (response == NULL)
     goto out;
   g_debug ("< %s", response);
@@ -535,7 +535,7 @@ goa_imap_auth_login_run_sync (GoaMailAuth         *auth,
     goto out;
   g_clear_pointer (&response, g_free);
 
-  response = g_data_input_stream_read_line (input, NULL, cancellable, error);
+  response = goa_utils_data_input_stream_read_line (input, NULL, cancellable, error);
   if (response == NULL)
     goto out;
   g_debug ("< %s", response);
@@ -552,7 +552,7 @@ goa_imap_auth_login_run_sync (GoaMailAuth         *auth,
   g_clear_pointer (&request, g_free);
 
   /* Skip post-login CAPABILITY, if any */
-  response = g_data_input_stream_read_line (input, NULL, cancellable, error);
+  response = goa_utils_data_input_stream_read_line (input, NULL, cancellable, error);
   if (response == NULL)
     goto out;
   g_debug ("< %s", response);
@@ -560,7 +560,7 @@ goa_imap_auth_login_run_sync (GoaMailAuth         *auth,
     goto check_login_response;
   g_clear_pointer (&response, g_free);
 
-  response = g_data_input_stream_read_line (input, NULL, cancellable, error);
+  response = goa_utils_data_input_stream_read_line (input, NULL, cancellable, error);
   if (response == NULL)
     goto out;
   g_debug ("< %s", response);
@@ -604,7 +604,7 @@ goa_imap_auth_login_starttls_sync (GoaMailAuth         *auth,
 
   /* Check the greeting */
 
-  response = g_data_input_stream_read_line (input, NULL, cancellable, error);
+  response = goa_utils_data_input_stream_read_line (input, NULL, cancellable, error);
   if (response == NULL)
     goto out;
   g_debug ("< %s", response);
@@ -624,7 +624,7 @@ goa_imap_auth_login_starttls_sync (GoaMailAuth         *auth,
 
   /* Check if STARTTLS is supported or not */
 
-  response = g_data_input_stream_read_line (input, NULL, cancellable, error);
+  response = goa_utils_data_input_stream_read_line (input, NULL, cancellable, error);
   if (response == NULL)
     goto out;
   g_debug ("< %s", response);
@@ -632,7 +632,7 @@ goa_imap_auth_login_starttls_sync (GoaMailAuth         *auth,
     goto out;
   g_clear_pointer (&response, g_free);
 
-  response = g_data_input_stream_read_line (input, NULL, cancellable, error);
+  response = goa_utils_data_input_stream_read_line (input, NULL, cancellable, error);
   if (response == NULL)
     goto out;
   g_debug ("< %s", response);
@@ -648,7 +648,7 @@ goa_imap_auth_login_starttls_sync (GoaMailAuth         *auth,
     goto out;
   g_clear_pointer (&request, g_free);
 
-  response = g_data_input_stream_read_line (input, NULL, cancellable, error);
+  response = goa_utils_data_input_stream_read_line (input, NULL, cancellable, error);
   if (response == NULL)
     goto out;
   g_debug ("< %s", response);
diff --git a/src/goabackend/goasmtpauth.c b/src/goabackend/goasmtpauth.c
index a736435..0161baf 100644
--- a/src/goabackend/goasmtpauth.c
+++ b/src/goabackend/goasmtpauth.c
@@ -191,7 +191,7 @@ smtp_auth_check_greeting (GDataInputStream *input, GCancellable *cancellable, GE
   ret = FALSE;
 
  greeting_again:
-  response = g_data_input_stream_read_line (input, NULL, cancellable, error);
+  response = goa_utils_data_input_stream_read_line (input, NULL, cancellable, error);
   if (response == NULL)
     goto out;
   g_debug ("< %s", response);
@@ -638,7 +638,7 @@ goa_smtp_auth_run_sync (GoaMailAuth         *auth,
   /* Check which SASL mechanisms are supported */
 
  ehlo_again:
-  response = g_data_input_stream_read_line (input, NULL, cancellable, error);
+  response = goa_utils_data_input_stream_read_line (input, NULL, cancellable, error);
   if (response == NULL)
     goto out;
   g_debug ("< %s", response);
@@ -706,7 +706,7 @@ goa_smtp_auth_run_sync (GoaMailAuth         *auth,
         goto out;
       g_clear_pointer (&request, g_free);
 
-      response = g_data_input_stream_read_line (input, NULL, cancellable, error);
+      response = goa_utils_data_input_stream_read_line (input, NULL, cancellable, error);
       if (response == NULL)
         goto out;
       g_debug ("< %s", response);
@@ -727,7 +727,7 @@ goa_smtp_auth_run_sync (GoaMailAuth         *auth,
       g_clear_pointer (&request, g_free);
     }
 
-  response = g_data_input_stream_read_line (input, NULL, cancellable, error);
+  response = goa_utils_data_input_stream_read_line (input, NULL, cancellable, error);
   if (response == NULL)
     goto out;
   g_debug ("< %s", response);
@@ -793,7 +793,7 @@ goa_smtp_auth_starttls_sync (GoaMailAuth         *auth,
   /* Check if STARTTLS is supported or not */
 
  ehlo_again:
-  response = g_data_input_stream_read_line (input, NULL, cancellable, error);
+  response = goa_utils_data_input_stream_read_line (input, NULL, cancellable, error);
   if (response == NULL)
     goto out;
   g_debug ("< %s", response);
@@ -828,7 +828,7 @@ goa_smtp_auth_starttls_sync (GoaMailAuth         *auth,
     goto out;
   g_clear_pointer (&request, g_free);
 
-  response = g_data_input_stream_read_line (input, NULL, cancellable, error);
+  response = goa_utils_data_input_stream_read_line (input, NULL, cancellable, error);
   if (response == NULL)
     goto out;
   g_debug ("< %s", response);
diff --git a/src/goabackend/goautils.c b/src/goabackend/goautils.c
index 2433bec..deb8236 100644
--- a/src/goabackend/goautils.c
+++ b/src/goabackend/goautils.c
@@ -126,6 +126,34 @@ goa_utils_check_duplicate (GoaClient              *client,
   return ret;
 }
 
+gchar *
+goa_utils_data_input_stream_read_line (GDataInputStream  *stream,
+                                       gsize             *length,
+                                       GCancellable      *cancellable,
+                                       GError           **error)
+{
+  GError *local_error = NULL;
+  gchar *ret = NULL;
+
+  ret = g_data_input_stream_read_line (stream, length, cancellable, &local_error);
+
+  /* Handle g_data_input_stream_read_line returning NULL without
+   * setting an error when there was no content to read.
+   */
+  if (ret == NULL && local_error == NULL)
+    {
+      g_set_error (&local_error,
+                   GOA_ERROR,
+                   GOA_ERROR_FAILED, /* TODO: more specific */
+                   _("Could not parse response"));
+    }
+
+  if (local_error != NULL)
+    g_propagate_error (error, local_error);
+
+  return ret;
+}
+
 void
 goa_utils_set_dialog_title (GoaProvider *provider, GtkDialog *dialog, gboolean add_account)
 {
diff --git a/src/goabackend/goautils.h b/src/goabackend/goautils.h
index b69265b..ca3671b 100644
--- a/src/goabackend/goautils.h
+++ b/src/goabackend/goautils.h
@@ -1,6 +1,6 @@
 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
 /*
- * Copyright (C) 2012, 2013, 2014 Red Hat, Inc.
+ * Copyright (C) 2012, 2013, 2014, 2015 Red Hat, Inc.
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -47,6 +47,11 @@ gboolean         goa_utils_check_duplicate (GoaClient              *client,
                                             GoaPeekInterfaceFunc    func,
                                             GError                **error);
 
+gchar           *goa_utils_data_input_stream_read_line (GDataInputStream  *stream,
+                                                        gsize             *length,
+                                                        GCancellable      *cancellable,
+                                                        GError           **error);
+
 void             goa_utils_set_dialog_title (GoaProvider *provider, GtkDialog *dialog, gboolean add_account);
 
 gboolean         goa_utils_delete_credentials_sync (GoaProvider    *provider,


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