[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Comments on the pax patch?



On Wed, Oct 12, 2011 at 09:03:13PM +0200, Svante Signell wrote:
> Attached is a patch to make pax-20090728 build under GNU/Hurd. Function
> calls changed are readlink()->xreadlink() (new, same as for psmisc) and
> realpath()->canonicalize_file_name().

[...]

> +char* xreadlink(const char *filename) {
> +	int nchars, size = 128;
> +	char *buffer;
> +	if ((buffer=(char*)malloc(size)) == NULL) {
> +		errno = ENOMEM;
> +		return NULL;
> +	}
> +	while ((nchars = readlink(filename,buffer,size)) == size) {
> +		size *= 2;
> +		if ((buffer = (char*)realloc(buffer,size)) == NULL) {
> +			errno = ENOMEM;
> +			return NULL;
> +		}
> +	}
> +	if (nchars < 0) {
> +		free (buffer);
> +		errno = EINVAL;
> +		return NULL;
> +	}
> +	buffer[nchars]='\0';
> +	return buffer;
> +}

Calling realloc() using the same pointer for both input and result is
almost always an error. If it fails, you've just lost your previous
reference and are unable to release it or do whatever is needed. It's
just a detail since such allocations almost never fail, and when they
do, you're probably screwed anyway, but it still hurts the mind.

-- 
Richard Braun


Reply to: