[pacman-dev] [PATCH] Prevent warnings with unchecked asprintf and FORTIFY_SOURCE

Andres P aepd87 at gmail.com
Sun Jun 27 21:56:28 EDT 2010


Instead of throwing away the return value, make a single function handle
errors.

Signed-off-by: Andres P <aepd87 at gmail.com>
---

I strongly disagree with making this void, but until all these void
funcions get a rework then I guess this is better than outright ignoring.

 src/pacman/util.c |   33 ++++++++++++++++++++++-----------
 1 files changed, 22 insertions(+), 11 deletions(-)

diff --git a/src/pacman/util.c b/src/pacman/util.c
index de1b162..1b2f451 100644
--- a/src/pacman/util.c
+++ b/src/pacman/util.c
@@ -44,6 +44,17 @@
 #include "conf.h"
 #include "callback.h"
 
+/* error handling in a single fn please */
+void vw_asprintf(char **str, const char *fmt, ...)
+{
+	va_list args;
+	va_start(args, fmt);
+	if(vasprintf(str, fmt, args) != 0) {
+		pm_fprintf(stderr, PM_LOG_ERROR,  _("failed to allocate string\n"));
+		/* goto halt */
+	}
+	va_end(args);
+}
 
 int trans_init(pmtransflag_t flags)
 {
@@ -530,10 +541,10 @@ void display_targets(const alpm_list_t *pkgs, int install)
 			double mbsize = 0.0;
 			mbsize = alpm_pkg_get_size(pkg) / (1024.0 * 1024.0);
 
-			asprintf(&str, "%s-%s [%.2f MB]", alpm_pkg_get_name(pkg),
+			vw_asprintf(&str, "%s-%s [%.2f MB]", alpm_pkg_get_name(pkg),
 					alpm_pkg_get_version(pkg), mbsize);
 		} else {
-			asprintf(&str, "%s-%s", alpm_pkg_get_name(pkg),
+			vw_asprintf(&str, "%s-%s", alpm_pkg_get_name(pkg),
 					alpm_pkg_get_version(pkg));
 		}
 		targets = alpm_list_add(targets, str);
@@ -544,7 +555,7 @@ void display_targets(const alpm_list_t *pkgs, int install)
 	mbisize = isize / (1024.0 * 1024.0);
 
 	if(install) {
-		asprintf(&str, _("Targets (%d):"), alpm_list_count(targets));
+		vw_asprintf(&str, _("Targets (%d):"), alpm_list_count(targets));
 		list_display(str, targets);
 		free(str);
 		printf("\n");
@@ -554,7 +565,7 @@ void display_targets(const alpm_list_t *pkgs, int install)
 			printf(_("Total Installed Size:   %.2f MB\n"), mbisize);
 		}
 	} else {
-		asprintf(&str, _("Remove (%d):"), alpm_list_count(targets));
+		vw_asprintf(&str, _("Remove (%d):"), alpm_list_count(targets));
 		list_display(str, targets);
 		free(str);
 		printf("\n");
@@ -588,14 +599,14 @@ static char *pkg_get_location(pmpkg_t *pkg)
 			dburl = alpm_db_get_url(db);
 			if(dburl) {
 				char *pkgurl = NULL;
-				asprintf(&pkgurl, "%s/%s", dburl, alpm_pkg_get_filename(pkg));
+				vw_asprintf(&pkgurl, "%s/%s", dburl, alpm_pkg_get_filename(pkg));
 				return(pkgurl);
 			}
 		case PM_OP_UPGRADE:
 			return(strdup(alpm_pkg_get_filename(pkg)));
 		default:
 			string = NULL;
-			asprintf(&string, "%s-%s", alpm_pkg_get_name(pkg), alpm_pkg_get_version(pkg));
+			vw_asprintf(&string, "%s-%s", alpm_pkg_get_name(pkg), alpm_pkg_get_version(pkg));
 			return(string);
 	}
 }
@@ -646,7 +657,7 @@ void print_packages(const alpm_list_t *packages)
 			char *size;
 			double mbsize = 0.0;
 			mbsize = pkg_get_size(pkg) / (1024.0 * 1024.0);
-			asprintf(&size, "%.2f", mbsize);
+			vw_asprintf(&size, "%.2f", mbsize);
 			string = strreplace(temp, "%s", size);
 			free(size);
 			free(temp);
@@ -793,16 +804,16 @@ int pm_vasprintf(char **string, pmloglevel_t level, const char *format, va_list
 	/* print a prefix to the message */
 	switch(level) {
 		case PM_LOG_DEBUG:
-			asprintf(string, "debug: %s", msg);
+			vw_asprintf(string, "debug: %s", msg);
 			break;
 		case PM_LOG_ERROR:
-			asprintf(string, _("error: %s"), msg);
+			vw_asprintf(string, _("error: %s"), msg);
 			break;
 		case PM_LOG_WARNING:
-			asprintf(string, _("warning: %s"), msg);
+			vw_asprintf(string, _("warning: %s"), msg);
 			break;
 		case PM_LOG_FUNCTION:
-			asprintf(string, _("function: %s"), msg);
+			vw_asprintf(string, _("function: %s"), msg);
 			break;
 		default:
 			break;
-- 
1.7.1



More information about the pacman-dev mailing list