Date: 07 Jul 1999 22:57:07 +0200 From: Assar Westerlund <assar@sics.se> To: Dag-Erling Smorgrav <des@flood.ping.uio.no> Cc: Jamie Howard <howardjp@wam.umd.edu>, freebsd-hackers@FreeBSD.ORG, tech-userlevel@netbsd.org, tech@openbsd.org Subject: Re: Replacement for grep(1) (part 2) Message-ID: <5laet8b2l8.fsf@assaris.sics.se> In-Reply-To: Dag-Erling Smorgrav's message of "07 Jul 1999 21:15:05 %2B0200" References: <Pine.GSO.4.10.9907052110250.13873-100000@uther.wam.umd.edu> <xzp7locthir.fsf@flood.ping.uio.no> <xzp1zektgp2.fsf@flood.ping.uio.no>
index | next in thread | previous in thread | raw e-mail
Dag-Erling Smorgrav <des@flood.ping.uio.no> writes:
> - if ((realpat = malloc(strlen(pattern) + sizeof("^(") +
> - sizeof(")$") + 1)) == NULL)
> - err(1, "malloc");
> + realpat = grep_malloc(strlen(pattern) + sizeof("^(")
> + + sizeof(")$") + 1);
> strcpy(realpat, "^(");
> strcat(realpat, pattern);
> strcat(realpat, ")$");
Why not just use asprintf?
> +void *
> +grep_malloc(size_t size)
> +{
> + void *ptr;
> +
> + if ((ptr = malloc(size)) == NULL) {
> + errno = ENOMEM;
> + err(1, "malloc");
> + }
> + return ptr;
> +}
In this case it doesn't matter but in general this function is wrong.
malloc(0) can return NULL.
And besides, I really don't think this is a grep function but actually
is useful for programs that don't have any strategy for handling out
of memory errors and might as well die (with a descriptive error
message, of course). Let's call it emalloc and let's put in somewhere
where it can be used.
void *
emalloc (size_t sz)
{
void *tmp = malloc (sz);
if (tmp == NULL && sz != 0) {
errno = ENOMEM;
err (1, "malloc %lu", (unsigned long)sz);
}
return tmp;
}
/assar
To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-hackers" in the body of the message
help
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?5laet8b2l8.fsf>
