Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20200803151150.wv4zbj7jkwjdfsvp@wolfsden.cz>
Date: Mon, 3 Aug 2020 17:11:50 +0200
From: Wolf <wolf@...fsden.cz>
To: musl@...ts.openwall.com
Cc: Ariadne Conill <ariadne@...eferenced.org>
Subject: Re: [PATCH v4] implement recallocarray(3)

On 2020-08-02 16:54:26 -0600, Ariadne Conill wrote:
> +void *recallocarray(void *ptr, size_t om, size_t m, size_t n)
> +{
> +	void *newptr;
> +	size_t old_size, new_size;
> +
> +	if (n && m > -1 / n) {
> +		errno = ENOMEM;
> +		return 0;
> +	}
> +	new_size = m * n;
> +
> +	if (n && om > -1 / n) {
> +		errno = EINVAL;
> +		return 0;
> +	}
> +	old_size = om * n;
> +
> +	newptr = calloc(m, n);
> +	if (!newptr)
> +		return ptr;

From reading openbsd's source I would say this should return 0 (or
newptr), not ptr. Otherwise I'm not sure how the caller could tell if
the resize failed or not.

> +
> +	if (new_size <= old_size) {
> +		memcpy((char *) newptr, ptr, new_size);
> +	}
> +	else {
> +		memcpy((char *) newptr, ptr, old_size);
> +		memset((char *) newptr + old_size, 0, new_size - old_size);
> +	}
> +
> +	memset(ptr, 0, old_size);
> +	free(ptr);
> +
> +	return newptr;
> +}

W.

-- 
There are only two hard things in Computer Science:
cache invalidation, naming things and off-by-one errors.

Download attachment "signature.asc" of type "application/pgp-signature" (834 bytes)

Powered by blists - more mailing lists

Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.