Dag-Erling Smorgrav <[EMAIL PROTECTED]> 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 [EMAIL PROTECTED]
with "unsubscribe freebsd-hackers" in the body of the message