[pacman-dev] [PATCH 2/2] lib/libalpm/be_sync.c: Close memory leaks when mallocing while out of memory

ivy.foster at gmail.com ivy.foster at gmail.com
Thu Sep 8 00:22:49 UTC 2016


From: Ivy Foster <ivy.foster at gmail.com>

Signed-off-by: Ivy Foster <ivy.foster at gmail.com>
---
 lib/libalpm/be_sync.c | 42 ++++++++++++++++++++++++++++++++++--------
 1 file changed, 34 insertions(+), 8 deletions(-)

diff --git a/lib/libalpm/be_sync.c b/lib/libalpm/be_sync.c
index ee438f8..f61668e 100644
--- a/lib/libalpm/be_sync.c
+++ b/lib/libalpm/be_sync.c
@@ -222,12 +222,24 @@ int SYMEXPORT alpm_db_update(int force, alpm_db_t *db)
 	dbext = db->handle->dbext;
 
 	len = strlen(syncpath) + strlen(db->treename) + strlen(dbext) + 2;
-	/* TODO fix leak syncpath and umask unset */
-	MALLOC(newdb, len, RET_ERR(handle, ALPM_ERR_MEMORY, -1));
+	MALLOC(newdb, len,
+		{
+			free(syncpath);
+			umask(oldmask);
+			RET_ERR(handle, ALPM_ERR_MEMORY, -1);
+		}
+	);
 	snprintf(newdb, len, "%s/%s%s", syncpath, db->treename, dbext);
+
 	len += 4;
-	/* TODO fix leak syncpath and umask unset */
-	MALLOC(olddb, len, RET_ERR(handle, ALPM_ERR_MEMORY, -1));
+	MALLOC(olddb, len,
+		{
+			free(syncpath);
+			free(newdb);
+			umask(oldmask);
+			RET_ERR(handle, ALPM_ERR_MEMORY, -1);
+		}
+	);
 	snprintf(olddb, len, "%s.bak", newdb);
 	if (rename(newdb, olddb) == -1) {
 		ret = -1;
@@ -247,8 +259,15 @@ int SYMEXPORT alpm_db_update(int force, alpm_db_t *db)
 
 		/* print server + filename into a buffer */
 		len = strlen(server) + strlen(db->treename) + strlen(dbext) + 2;
-		/* TODO fix leak syncpath and umask unset */
-		MALLOC(payload.fileurl, len, RET_ERR(handle, ALPM_ERR_MEMORY, -1));
+		MALLOC(payload.fileurl, len,
+			{
+				free(newdb);
+				free(olddb);
+				free(syncpath);
+				umask(oldmask);
+				RET_ERR(handle, ALPM_ERR_MEMORY, -1);
+			}
+		);
 		snprintf(payload.fileurl, len, "%s/%s%s", server, db->treename, dbext);
 		payload.handle = handle;
 		payload.force = force;
@@ -287,8 +306,15 @@ int SYMEXPORT alpm_db_update(int force, alpm_db_t *db)
 				len = strlen(server) + strlen(db->treename) + strlen(dbext) + 6;
 			}
 
-			/* TODO fix leak syncpath and umask unset */
-			MALLOC(payload.fileurl, len, RET_ERR(handle, ALPM_ERR_MEMORY, -1));
+			MALLOC(payload.fileurl, len,
+				{
+					free(newdb);
+					free(olddb);
+					free(syncpath);
+					umask(oldmask);
+					RET_ERR(handle, ALPM_ERR_MEMORY, -1);
+				}
+			);
 
 			if(final_db_url != NULL) {
 				snprintf(payload.fileurl, len, "%s.sig", final_db_url);
-- 
2.9.3


More information about the pacman-dev mailing list