Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
Abort if asprintf fails
Baptiste Daroussin committed 9 years ago
commit 721c1e295cf7037eba813caaf63250d7161cf75b
parent bd5adf3
9 files changed +38 -35
modified libpkg/elfhints.c
@@ -324,7 +324,7 @@ shlib_list_from_stage(const char *stage)
		return;

	for (i = 0; i < NELEM(stage_dirs); i++) {
-
		asprintf(&dir, "%s%s", stage, stage_dirs[i]);
+
		xasprintf(&dir, "%s%s", stage, stage_dirs[i]);
		scan_dirs_for_shlibs(&shlibs, 1, (const char **)&dir, true);
		free(dir);
	}
@@ -497,8 +497,7 @@ write_elf_hints(const char *hintsfile)
	FILE			*fp;
	int			 i;

-
	if (asprintf(&tempname, "%s.XXXXXX", hintsfile) == -1)
-
		errx(1, "Out of memory");
+
	xasprintf(&tempname, "%s.XXXXXX", hintsfile);
	if ((fd = mkstemp(tempname)) ==  -1)
		err(1, "mkstemp(%s)", tempname);
	if (fchmod(fd, 0444) == -1)
modified libpkg/pkg_checksum.c
@@ -255,7 +255,7 @@ pkg_checksum_generate(struct pkg *pkg, char *dest, size_t destlen,
	}

	while (pkg_deps(pkg, &dep) == EPKG_OK) {
-
		asprintf(&olduid, "%s~%s", dep->name, dep->origin);
+
		xasprintf(&olduid, "%s~%s", dep->name, dep->origin);
		pkg_checksum_add_entry("depend", olduid, &entries);
		free(olduid);
	}
@@ -835,7 +835,7 @@ pkg_checksum_generate_file(const char *path, pkg_checksum_type_t type)
	if (sum == NULL)
		return (NULL);

-
	asprintf(&cksum, "%d%c%s", type, PKG_CKSUM_SEPARATOR, sum);
+
	xasprintf(&cksum, "%d%c%s", type, PKG_CKSUM_SEPARATOR, sum);
	free(sum);

	return (cksum);
@@ -900,7 +900,7 @@ pkg_checksum_generate_fileat(int rootfd, const char *path,
	if (sum == NULL)
		return (NULL);

-
	asprintf(&cksum, "%d%c%s", type, PKG_CKSUM_SEPARATOR, sum);
+
	xasprintf(&cksum, "%d%c%s", type, PKG_CKSUM_SEPARATOR, sum);
	free(sum);

	return (cksum);
modified libpkg/pkg_config.c
@@ -898,7 +898,7 @@ pkg_ini(const char *path, const char *reposdir, pkg_init_flags flags)
			tmp = NULL;
			if (c[i].def != NULL && c[i].def[0] == '/' &&
			    pkg_rootdir != NULL) {
-
				asprintf(&tmp, "%s%s", pkg_rootdir, c[i].def);
+
				xasprintf(&tmp, "%s%s", pkg_rootdir, c[i].def);
			}
			obj = ucl_object_fromstring_common(
			    c[i].def != NULL ? tmp != NULL ? tmp : c[i].def : "", 0, UCL_STRING_TRIM);
modified libpkg/pkg_jobs.c
@@ -755,7 +755,7 @@ pkg_jobs_process_remote_pkg(struct pkg_jobs *j, struct pkg *rp,
			DL_FOREACH(nit, cur) {
				if (cur->pkg->type != PKG_INSTALLED) {
					free(cur->pkg->reason);
-
					asprintf(&cur->pkg->reason, "vulnerability found");
+
					xasprintf(&cur->pkg->reason, "vulnerability found");
				}
			}
			/* Also process all rdeps recursively */
@@ -1083,7 +1083,7 @@ pkg_jobs_need_upgrade(struct pkg *rp, struct pkg *lp)
	/* Compare archs */
	if (strcmp (lp->arch, rp->arch) != 0) {
		free(rp->reason);
-
		asprintf(&rp->reason, "ABI changed: '%s' -> '%s'",
+
		xasprintf(&rp->reason, "ABI changed: '%s' -> '%s'",
		    lp->arch, rp->arch);
		assert(rp->reason != NULL);
		return (true);
@@ -1096,13 +1096,13 @@ pkg_jobs_need_upgrade(struct pkg *rp, struct pkg *lp)
		if (ret1 != ret2) {
			free(rp->reason);
			if (ro == NULL)
-
				asprintf(&rp->reason, "option removed: %s",
+
				xasprintf(&rp->reason, "option removed: %s",
				    lo->key);
			else if (lo == NULL)
-
				asprintf(&rp->reason, "option added: %s",
+
				xasprintf(&rp->reason, "option added: %s",
				    ro->key);
			else
-
				asprintf(&rp->reason, "option changed: %s",
+
				xasprintf(&rp->reason, "option changed: %s",
				    ro->key);
			assert(rp->reason != NULL);
			return (true);
@@ -1111,7 +1111,7 @@ pkg_jobs_need_upgrade(struct pkg *rp, struct pkg *lp)
			if (strcmp(lo->key, ro->key) != 0 ||
			    strcmp(lo->value, ro->value) != 0) {
				free(rp->reason);
-
				asprintf(&rp->reason, "options changed");
+
				xasprintf(&rp->reason, "options changed");
				return (true);
			}
		}
@@ -1126,13 +1126,13 @@ pkg_jobs_need_upgrade(struct pkg *rp, struct pkg *lp)
		if (ret1 != ret2) {
			free(rp->reason);
			if (rd == NULL)
-
				asprintf(&rp->reason, "direct dependency removed: %s",
+
				xasprintf(&rp->reason, "direct dependency removed: %s",
				    ld->name);
			else if (ld == NULL)
-
				asprintf(&rp->reason, "direct dependency added: %s",
+
				xasprintf(&rp->reason, "direct dependency added: %s",
				    rd->name);
			else
-
				asprintf(&rp->reason, "direct dependency changed: %s",
+
				xasprintf(&rp->reason, "direct dependency changed: %s",
				    rd->name);
			assert (rp->reason != NULL);
			return (true);
@@ -1141,7 +1141,7 @@ pkg_jobs_need_upgrade(struct pkg *rp, struct pkg *lp)
			if ((strcmp(rd->name, ld->name) != 0) ||
			    (strcmp(rd->origin, ld->origin) != 0)) {
				free(rp->reason);
-
				asprintf(&rp->reason, "direct dependency changed: %s",
+
				xasprintf(&rp->reason, "direct dependency changed: %s",
				    rd->name);
				assert (rp->reason != NULL);
				return (true);
modified libpkg/pkg_macho.c
@@ -104,7 +104,7 @@ analyse_macho(struct pkg *pkg, const char *fpath,
			// XXX MACTODO: To get things working, we're shoving library metadata into the library name;
			// these should instead be added as supported attributes of package shared library declarations.
			char *libname;
-
			asprintf(&libname, "%s.%s", march->mat_install_name, ai->name);
+
			xasprintf(&libname, "%s.%s", march->mat_install_name, ai->name);
			pkg_addshlib_provided(pkg, libname);
			is_shlib = true;
		}
@@ -136,7 +136,7 @@ analyse_macho(struct pkg *pkg, const char *fpath,
			// these should instead be added as supported attributes of package shared library declarations.
			// XXX: This duplicates (and must be kept identical to) the libname construction above.
			char *libname;
-
			asprintf(&libname, "%s.%s", cmd->mlt_install_name, ai->name);
+
			xasprintf(&libname, "%s.%s", cmd->mlt_install_name, ai->name);

			action(actdata, pkg, fpath, libname, is_shlib);
		}
@@ -278,7 +278,7 @@ pkg_analyse_files(struct pkgdb *db, struct pkg *pkg, const char *stage)
			free(fpath);

		if (stage != NULL)
-
			asprintf(&fpath, "%s/%s", stage, file->path);
+
			xasprintf(&fpath, "%s/%s", stage, file->path);
		else
			fpath = file->path;

@@ -394,13 +394,7 @@ pkg_get_myarch(char *dest, size_t sz)
	cpu_name = macho_get_arch_name(cpu_type);

	/* Produce the result */
-
	asprintf(&spec, "%s:%lld:%s", os_name, major_version, cpu_name);
-
	if (spec == NULL) {
-
		pkg_emit_error("asprintf() failed to allocate output string");
-
		ret = EPKG_FATAL;
-
		goto cleanup;
-
	}
-

+
	xasprintf(&spec, "%s:%lld:%s", os_name, major_version, cpu_name);
	strlcpy(dest, spec, sz);

cleanup:
modified libpkg/pkg_printf.c
@@ -3065,7 +3065,7 @@ pkg_vasprintf(char **ret, const char * restrict format, va_list ap)
	if (buf)
		buf = pkg_utstring_vprintf(buf, format, ap);
	if (buf && utstring_len(buf) > 0) {
-
		count = asprintf(ret, "%s", utstring_body(buf));
+
		count = xasprintf(ret, "%s", utstring_body(buf));
	} else {
		count = -1;
		*ret = NULL;
modified libpkg/pkgdb.c
@@ -1213,12 +1213,7 @@ run_transaction(sqlite3 *sqlite, const char *query, const char *savepoint)

	assert(sqlite != NULL);

-
	asprintf(&sql, "%s %s", query, savepoint != NULL ? savepoint : "");
-
	if (sql == NULL) {
-
		pkg_emit_error("out of memory");
-
		return (EPKG_FATAL);
-
	}
-

+
	xasprintf(&sql, "%s %s", query, savepoint != NULL ? savepoint : "");
	pkg_debug(4, "Pkgdb: running '%s'", sql);
	ret = sqlite3_prepare_v2(sqlite, sql, strlen(sql) + 1, &stmt, NULL);

modified libpkg/pkgdb_iterator.c
@@ -275,7 +275,7 @@ pkgdb_load_deps(sqlite3 *sqlite, struct pkg *pkg)
				clause = pkg_deps_formula_tosql(fit);

				if (clause) {
-
					asprintf(&formula_sql, "%s%s", formula_preamble, clause);
+
					xasprintf(&formula_sql, "%s%s", formula_preamble, clause);
					pkg_debug(4, "Pkgdb: running '%s'", sql);
					ret = sqlite3_prepare_v2(sqlite, sql, -1, &stmt, NULL);

modified libpkg/private/xmalloc.h
@@ -40,4 +40,19 @@ static inline char *xstrndup(const char *str, size_t n)
		abort();
	return (s);
}
+

+
static inline int xasprintf(char **ret, const char *fmt, ...)
+
{
+
	va_list ap;
+
	int i;
+

+
	va_start(ap, fmt);
+
	i = vasprintf(ret, fmt, ap);
+
	va_end(ap);
+

+
	if (i < 0 || *ret == NULL)
+
		abort();
+

+
	return (i);
+
}
#endif