Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
xmalloc: use its functions everywhere
Baptiste Daroussin committed 2 months ago
commit e5baad401179407634d2de6a9e154ba9a3c37895
parent 95dca8e
7 files changed +21 -55
modified libpkg/pkg_ports.c
@@ -241,7 +241,7 @@ lua_meta(lua_State *L,
	int ret;
	luaL_argcheck(L, n == 1, n > 1 ? 2 : n,
	    "takes exactly one argument");
-
	char *str = strdup(luaL_checkstring(L, 1));
+
	char *str = xstrdup(luaL_checkstring(L, 1));
	lua_getglobal(L, "plist");
	struct plist *p = lua_touserdata(L, -1);
	lua_getglobal(L, "attrs");
modified libpkg/pkg_sandbox.c
@@ -49,6 +49,7 @@
# endif
#endif

+
#include <xmalloc.h>
#include "pkg.h"

int
@@ -140,20 +141,12 @@ pkg_handle_sandboxed_get_string(pkg_sandbox_cb func, char **result, int64_t *len
		 * We use blocking IO here as if the child is terminated we would have
		 * EINTR here
		 */
-
		buf = malloc(BUFSIZ);
-
		if (buf == NULL) {
-
			warn("malloc failed");
-
			return (EPKG_FATAL);
-
		}
+
		buf = xmalloc(BUFSIZ);
		allocated_len = BUFSIZ;
		do {
			if (off >= allocated_len) {
				allocated_len *= 2;
-
				buf = realloc(buf, allocated_len);
-
				if (buf == NULL) {
-
					warn("realloc failed");
-
					return (EPKG_FATAL);
-
				}
+
				buf = xrealloc(buf, allocated_len);
			}

			r = read(pair[1], buf + off, allocated_len - off);
modified libpkg/repo/binary/fetch.c
@@ -277,15 +277,13 @@ cleanup:
		(void)pkg_repo_binary_create_symlink(pkg, dest, dir);
	}
	else if (mirror && symlink && dir != NULL) {
-
		alldir = strdup(dir);
-
		if (alldir != NULL) {
-
			char *hashed = strstr(alldir, "/Hashed");
-
			if (hashed != NULL) {
-
				*hashed = '\0';
-
				(void)pkg_repo_binary_create_symlink(pkg, dest, alldir);
-
			}
-
			free(alldir);
+
		alldir = xstrdup(dir);
+
		char *hashed = strstr(alldir, "/Hashed");
+
		if (hashed != NULL) {
+
			*hashed = '\0';
+
			(void)pkg_repo_binary_create_symlink(pkg, dest, alldir);
		}
+
		free(alldir);
	}

	free(dir);
modified src/main.c
@@ -465,9 +465,7 @@ expand_aliases(int argc, char ***argv)
	spaces = pkg_utils_count_spaces(alias_value);
	arglen = strlen(alias_value) + 1;
	veclen = sizeof(char *) * (spaces + argc + 1);
-
	buf = malloc(veclen + arglen);
-
	if (buf == NULL)
-
		err(EXIT_FAILURE, "expanding aliases");
+
	buf = xmalloc(veclen + arglen);

	newargv = (char **) buf;
	args = (char *) (buf + veclen);
modified src/updating.c
@@ -21,6 +21,7 @@
#include <ctype.h>
#include <regex.h>

+
#include <xmalloc.h>
#include "pkgcli.h"

struct regex_cache {
@@ -53,9 +54,7 @@ convert_re(const char *src)
	char *q;
	bool brace_flag = false;
	size_t len = strlen(src);
-
	char *buf = malloc(len*2+1);
-
	if (buf == NULL)
-
		return NULL;
+
	char *buf = xmalloc(len*2+1);

	for (p=src, q=buf; p < src+len; p++) {
		switch (*p) {
@@ -103,9 +102,7 @@ matcher(const char *affects, const char *origin, bool ignorecase)
	vec_t(struct regex_cache *) cache = vec_init();

	len = strlen(affects);
-
	buf = strdup(affects);
-
	if (buf == NULL)
-
		return 0;
+
	buf = xstrdup(affects);

	for (count = 0, was_spc = true, p = buf; p < buf + len ; p++) {
		if (isspace(*p)) {
@@ -120,11 +117,7 @@ matcher(const char *affects, const char *origin, bool ignorecase)
		}
	}

-
	words = malloc(sizeof(char*)*count);
-
	if (words == NULL) {
-
		free(buf);
-
		return 0;
-
	}
+
	words = xmalloc(sizeof(char*)*count);

	for (i = 0, was_spc = true, p = buf; p < buf + len ; p++) {
		if (*p == '\0') {
@@ -176,15 +169,8 @@ matcher(const char *affects, const char *origin, bool ignorecase)
			}
		}
		if (found == 0) {
-
			if ((ent = malloc(sizeof(struct regex_cache))) == NULL) {
-
				ret = 0;
-
				goto out;
-
			}
-
			if ((ent->pattern = strdup(words[i])) == NULL) {
-
				regex_cache_free(ent);
-
				ret = 0;
-
				goto out;
-
			}
+
			ent = xmalloc(sizeof(struct regex_cache));
+
			ent->pattern = xstrdup(words[i]);
			re = convert_re(words[i]);
			if (re == NULL) {
				regex_cache_free(ent);
@@ -317,7 +303,7 @@ exec_updating(int argc, char **argv)
		}
	} else {
		while (*argv) {
-
			char *orig = strdup(*argv);
+
			char *orig = xstrdup(*argv);
			vec_push(&origins, orig);
			argv++;
		}
@@ -326,7 +312,7 @@ exec_updating(int argc, char **argv)
	while (getline(&line, &linecap, fd) > 0) {
		if (strspn(line, "0123456789:") == 9) {
			free(dateline);
-
			dateline = strdup(line);
+
			dateline = xstrdup(line);
			found = 0;
			head = 1;
		} else if (head == 0) {
modified src/utils.c
@@ -826,12 +826,7 @@ set_jobs_summary_pkg(struct pkg_jobs *jobs, struct pkg *new_pkg,
	if (old_pkg != NULL)
		pkg_get(old_pkg, PKG_ATTR_FLATSIZE, &oldflatsize);

-
	it = malloc(sizeof (*it));
-
	if (it == NULL) {
-
		fprintf(stderr, "malloc failed for "
-
				"pkg_solved_display: %s", strerror (errno));
-
		return;
-
	}
+
	it = xmalloc(sizeof (*it));
	it->new = new_pkg;
	it->old = old_pkg;
	it->solved_type = type;
modified src/which.c
@@ -135,11 +135,7 @@ exec_which(int argc, char **argv)
					goto cleanup;
				}

-
				p = malloc(pathlen);
-
				if (p == NULL) {
-
					retcode = EXIT_FAILURE;
-
					goto cleanup;
-
				}
+
				p = xmalloc(pathlen);
				strlcpy(p, path, pathlen);

				match = NULL;