git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 11/15] contrib/git-credential-gnome-keyring.c: use glib memory allocation functions

From
Brandon Casey <drafnel@gmail.com>
Date
Sep 23, 2013, 05:08 UTC
Message-ID
<1379912891-12277-12-git-send-email-drafnel@gmail.com>
In-Reply-To
<1379912891-12277-1-git-send-email-drafnel@gmail.com>

Rather than roll our own, let's use the memory allocation/free routines provided by glib.

Signed-off-by: Brandon Casey <drafnel@gmail.com>
---
 .../gnome-keyring/git-credential-gnome-keyring.c   | 48 ++++++++--------------
 1 file changed, 16 insertions(+), 32 deletions(-)
diff --git a/contrib/credential/gnome-keyring/git-credential-gnome-keyring.c b/contrib/credential/gnome-keyring/git-credential-gnome-keyring.c
index 94a65b2..5b10e3e 100644
--- a/contrib/credential/gnome-keyring/git-credential-gnome-keyring.c
+++ b/contrib/credential/gnome-keyring/git-credential-gnome-keyring.c
@@ -27,7 +27,6 @@
 #include <string.h>
 #include <stdarg.h>
 #include <stdlib.h>
-#include <errno.h>
 #include <glib.h>
 #include <gnome-keyring.h>
 #include <gnome-keyring-memory.h>
@@ -83,21 +82,6 @@ static inline void error(const char *fmt, ...)
 	va_end(ap);
 }
 
-static inline void die_errno(int err)
-{
-	error("%s", strerror(err));
-	exit(EXIT_FAILURE);
-}
-
-static inline char *xstrdup(const char *str)
-{
-	char *ret = strdup(str);
-	if (!ret)
-		die_errno(errno);
-
-	return ret;
-}
-
 /* ----------------- GNOME Keyring functions ----------------- */
 
 /* create a special keyring option string, if path is given */
@@ -134,7 +118,7 @@ static int keyring_get(struct credential *c)
 				c->port,
 				&entries);
 
-	free(object);
+	g_free(object);
 
 	if (result == GNOME_KEYRING_RESULT_NO_MATCH)
 		return EXIT_SUCCESS;
@@ -154,7 +138,7 @@ static int keyring_get(struct credential *c)
 	c->password = gnome_keyring_memory_strdup(password_data->password);
 
 	if (!c->username)
-		c->username = xstrdup(password_data->user);
+		c->username = g_strdup(password_data->user);
 
 	gnome_keyring_network_password_list_free(entries);
 
@@ -192,7 +176,7 @@ static int keyring_store(struct credential *c)
 				c->password,
 				&item_id);
 
-	free(object);
+	g_free(object);
 	return EXIT_SUCCESS;
 }
 
@@ -226,7 +210,7 @@ static int keyring_erase(struct credential *c)
 				c->port,
 				&entries);
 
-	free(object);
+	g_free(object);
 
 	if (result == GNOME_KEYRING_RESULT_NO_MATCH)
 		return EXIT_SUCCESS;
@@ -278,10 +262,10 @@ static void credential_init(struct credential *c)
 
 static void credential_clear(struct credential *c)
 {
-	free(c->protocol);
-	free(c->host);
-	free(c->path);
-	free(c->username);
+	g_free(c->protocol);
+	g_free(c->host);
+	g_free(c->path);
+	g_free(c->username);
 	gnome_keyring_memory_free(c->password);
 
 	credential_init(c);
@@ -315,22 +299,22 @@ static int credential_read(struct credential *c)
 		*value++ = '\0';
 
 		if (!strcmp(key, "protocol")) {
-			free(c->protocol);
-			c->protocol = xstrdup(value);
+			g_free(c->protocol);
+			c->protocol = g_strdup(value);
 		} else if (!strcmp(key, "host")) {
-			free(c->host);
-			c->host = xstrdup(value);
+			g_free(c->host);
+			c->host = g_strdup(value);
 			value = strrchr(c->host,':');
 			if (value) {
 				*value++ = '\0';
 				c->port = atoi(value);
 			}
 		} else if (!strcmp(key, "path")) {
-			free(c->path);
-			c->path = xstrdup(value);
+			g_free(c->path);
+			c->path = g_strdup(value);
 		} else if (!strcmp(key, "username")) {
-			free(c->username);
-			c->username = xstrdup(value);
+			g_free(c->username);
+			c->username = g_strdup(value);
 		} else if (!strcmp(key, "password")) {
 			gnome_keyring_memory_free(c->password);
 			c->password = gnome_keyring_memory_strdup(value);
-- 
1.8.4.489.g545bc72
Previous: Brandon CaseyNext: Brandon Casey
Message 14 of 20 in “Make Gnome Credential helper more Gnome-y and support ancient distros”
  1. 00/15 Make Gnome Credential helper more Gnome-y and support ancient distrosBrandon Casey, Sep 23, 2013
  2. 01/15 contrib/git-credential-gnome-keyring.c: remove unnecessary pre-declarationsBrandon Casey, Sep 23, 2013
  3. 02/15 contrib/git-credential-gnome-keyring.c: remove unused die() functionBrandon Casey, Sep 23, 2013
  4. 03/15 contrib/git-credential-gnome-keyring.c: add static where applicableBrandon Casey, Sep 23, 2013
  5. 04/15 contrib/git-credential-gnome-keyring.c: exit non-zero when called incorrectlyBrandon Casey, Sep 23, 2013
  6. 05/15 contrib/git-credential-gnome-keyring.c: set Gnome application nameBrandon Casey, Sep 23, 2013
  7. 06/15 contrib/git-credential-gnome-keyring.c: strlen() returns size_t, not ssize_tBrandon Casey, Sep 23, 2013
  8. 07/15 contrib/git-credential-gnome-keyring.c: ensure buffer is non-empty before accessingBrandon Casey, Sep 23, 2013
  9. Felipe ContrerasSep 23, 2013
  10. Brandon CaseySep 23, 2013
  11. 08/15 contrib/git-credential-gnome-keyring.c: use gnome helpers in keyring_object()Brandon Casey, Sep 23, 2013
  12. 09/15 contrib/git-credential-gnome-keyring.c: use secure memory functions for passwdsBrandon Casey, Sep 23, 2013
  13. 10/15 contrib/git-credential-gnome-keyring.c: use secure memory for reading passwordsBrandon Casey, Sep 23, 2013
  14. 11/15 contrib/git-credential-gnome-keyring.c: use glib memory allocation functionsBrandon Casey, Sep 23, 2013
  15. 12/15 contrib/git-credential-gnome-keyring.c: use glib messaging functionsBrandon Casey, Sep 23, 2013
  16. 13/15 contrib/git-credential-gnome-keyring.c: report failure to store passwordBrandon Casey, Sep 23, 2013
  17. 14/15 contrib/git-credential-gnome-keyring.c: support ancient gnome-keyringBrandon Casey, Sep 23, 2013
  18. 15/15 contrib/git-credential-gnome-keyring.c: support really ancient gnome-keyringBrandon Casey, Sep 23, 2013
  19. John SzakmeisterSep 23, 2013
  20. Brandon CaseySep 23, 2013

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.