[pacman-dev,1/2] Use STRDUP for error checking in more places

Message ID 20200413122711.247310-1-allan@archlinux.org
State Accepted, archived
Headers show
Series [pacman-dev,1/2] Use STRDUP for error checking in more places | expand

Commit Message

Allan McRae April 13, 2020, 12:27 p.m. UTC
Use STRDUP() over strdup() to catch memory allocation errors.

There are still some instances of strdup left, but these are in functions
that currently have no error path and would require a larger rework.

Signed-off-by: Allan McRae <allan@archlinux.org>
---
 lib/libalpm/be_package.c | 10 +++++++---
 lib/libalpm/handle.c     |  2 +-
 lib/libalpm/util.c       |  3 ++-
 3 files changed, 10 insertions(+), 5 deletions(-)

Patch

diff --git a/lib/libalpm/be_package.c b/lib/libalpm/be_package.c
index 4832966b..38ba365d 100644
--- a/lib/libalpm/be_package.c
+++ b/lib/libalpm/be_package.c
@@ -201,11 +201,15 @@  static int parse_descfile(alpm_handle_t *handle, struct archive *a, alpm_pkg_t *
 			} else if(strcmp(key, "pkgdesc") == 0) {
 				STRDUP(newpkg->desc, ptr, return -1);
 			} else if(strcmp(key, "group") == 0) {
-				newpkg->groups = alpm_list_add(newpkg->groups, strdup(ptr));
+				char *tmp = NULL;
+				STRDUP(tmp, ptr, return -1);
+				newpkg->groups = alpm_list_add(newpkg->groups, tmp);
 			} else if(strcmp(key, "url") == 0) {
 				STRDUP(newpkg->url, ptr, return -1);
 			} else if(strcmp(key, "license") == 0) {
-				newpkg->licenses = alpm_list_add(newpkg->licenses, strdup(ptr));
+				char *tmp = NULL;
+				STRDUP(tmp, ptr, return -1);
+				newpkg->licenses = alpm_list_add(newpkg->licenses, tmp);
 			} else if(strcmp(key, "builddate") == 0) {
 				newpkg->builddate = _alpm_parsedate(ptr);
 			} else if(strcmp(key, "packager") == 0) {
@@ -660,7 +664,7 @@  alpm_pkg_t *_alpm_pkg_load_internal(alpm_handle_t *handle,
 
 	/* internal fields for package struct */
 	newpkg->origin = ALPM_PKG_FROM_FILE;
-	newpkg->origin_data.file = strdup(pkgfile);
+	STRDUP(newpkg->origin_data.file, pkgfile, goto error);
 	newpkg->ops = get_file_pkg_ops();
 	newpkg->handle = handle;
 	newpkg->infolevel = INFRQ_BASE | INFRQ_DESC | INFRQ_SCRIPTLET;
diff --git a/lib/libalpm/handle.c b/lib/libalpm/handle.c
index 8ae08d11..e3b2c911 100644
--- a/lib/libalpm/handle.c
+++ b/lib/libalpm/handle.c
@@ -105,7 +105,7 @@  int _alpm_handle_lock(alpm_handle_t *handle)
 	ASSERT(handle->lockfd < 0, return 0);
 
 	/* create the dir of the lockfile first */
-	dir = strdup(handle->lockfile);
+	STRDUP(dir, handle->lockfile, return -1);
 	ptr = strrchr(dir, '/');
 	if(ptr) {
 		*ptr = '\0';
diff --git a/lib/libalpm/util.c b/lib/libalpm/util.c
index cebc87ec..cb838e43 100644
--- a/lib/libalpm/util.c
+++ b/lib/libalpm/util.c
@@ -350,7 +350,8 @@  int _alpm_unpack(alpm_handle_t *handle, const char *path, const char *prefix,
 
 		/* If specific files were requested, skip entries that don't match. */
 		if(list) {
-			char *entry_prefix = strdup(entryname);
+			char *entry_prefix = NULL;
+			STRDUP(entry_prefix, entryname, ret = 1; goto cleanup);
 			char *p = strstr(entry_prefix,"/");
 			if(p) {
 				*(p + 1) = '\0';