[gnome-commander] Converted temporary password variable from string to char array



commit 7ea1fdaa5bc52f6b6b087015cc5a7ae38240b7aa
Author: Uwe Scholz <uwescholz src gnome org>
Date:   Mon May 4 16:10:07 2015 +0200

    Converted temporary password variable from string to char array

 src/gnome-cmd-con.cc       |   29 +++++++++++++++++++++++------
 src/gnome-cmd-con.h        |    4 ++--
 src/gnome-cmd-file-list.cc |    1 +
 3 files changed, 26 insertions(+), 8 deletions(-)
---
diff --git a/src/gnome-cmd-con.cc b/src/gnome-cmd-con.cc
index 2cfa5fe..bd6b3ed 100644
--- a/src/gnome-cmd-con.cc
+++ b/src/gnome-cmd-con.cc
@@ -618,14 +618,30 @@ GnomeKeyringAttributeList *gnome_cmd_con_create_keyring_attributes (const gchar
     return attributes;
 }
 
-void response_callback (GtkDialog *dialog, int response_id, std::string *password)
+/**
+ * This callback function takes a const char* pointer and points it to a
+ * newly created char array of the password the user has typed in the
+ * associated dialog entry. The memory where @c password points to has
+ * to be freed elsewhere.
+ */
+void set_password_callback (GtkDialog *dialog, int response_id, const char **password)
 {
     switch (response_id)
     {
         case GTK_RESPONSE_OK:
        {
+           const char *passwd = NULL; /* local pointer pointing to const-defined memory area */
+
            const gchar *entry = gtk_entry_get_text (GTK_ENTRY (lookup_widget (GTK_WIDGET (dialog), 
"password")));
-           password->assign(entry);
+           int length = strlen(entry);
+           if (( passwd = (const char *) malloc(length * sizeof(char) + 1)) == NULL)
+           {
+                DEBUG ('m', "Not enough memory for temporary storing the password!\n");
+               break;
+           }
+       
+           strcpy((char*) passwd, entry);
+           *password = passwd;
        }
             break;
 
@@ -640,11 +656,12 @@ void response_callback (GtkDialog *dialog, int response_id, std::string *passwor
 }
 
 /**
- * A small dialog for setting the password
+ * A small dialog for setting the password. The const char pointer to
+ * the password has to be freed outside of this function!
  */
-const std::string* GnomeCmdCon::gnome_cmd_con_set_password()
+const char* GnomeCmdCon::gnome_cmd_con_set_password()
 {
-    std::string *password = NULL;
+    const char *password = NULL;
     GtkWidget *table;
     GtkWidget *entry;
     GtkWidget *label;
@@ -707,7 +724,7 @@ const std::string* GnomeCmdCon::gnome_cmd_con_set_password()
 
     gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_OK);
 
-    g_signal_connect (dialog, "response", G_CALLBACK (response_callback), password);
+    g_signal_connect (dialog, "response", G_CALLBACK (set_password_callback), (gpointer) &password);
     
     gtk_dialog_run (GTK_DIALOG (dialog));
 
diff --git a/src/gnome-cmd-con.h b/src/gnome-cmd-con.h
index d203724..67745a9 100644
--- a/src/gnome-cmd-con.h
+++ b/src/gnome-cmd-con.h
@@ -121,8 +121,8 @@ struct GnomeCmdCon
     GnomeKeyringAttributeList *create_keyring_attributes();
 
     friend XML::xstream &operator << (XML::xstream &xml, GnomeCmdCon &con);
-    const std::string   *password;
-    const std::string   *gnome_cmd_con_set_password();
+    const char          *password;
+    const char          *gnome_cmd_con_set_password();
 };
 
 struct GnomeCmdConClass
diff --git a/src/gnome-cmd-file-list.cc b/src/gnome-cmd-file-list.cc
index 1ca4d41..5af845d 100644
--- a/src/gnome-cmd-file-list.cc
+++ b/src/gnome-cmd-file-list.cc
@@ -2519,6 +2519,7 @@ void GnomeCmdFileList::set_connection (GnomeCmdCon *new_con, GnomeCmdDir *start_
        if (new_con->auth != GnomeCmdCon::NOT_REQUIRED)
        {
            new_con->password = new_con->gnome_cmd_con_set_password(); //TODO: password should be a private 
member variable of GnomeCmdCon someday!
+           g_return_if_fail (new_con->password);
        }
         create_con_open_progress_dialog (this);
         g_timeout_add (gnome_cmd_data.gui_update_rate, (GSourceFunc) update_con_open_progress, this);


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