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

Re: Comments on the psmisc patch?



Svante Signell, le Mon 23 Apr 2012 15:42:47 +0200, a écrit :
> +#else
> +char* xreadlink(const char *filename) {
> +	int nchars, size = 128;
> +	char *buffer;
> +	if ((buffer=(char*)malloc(size)) == NULL) {
> +		errno = ENOMEM;

No need to set errno, malloc should have done so already.

> +		return NULL;
> +	}
> +	while ((nchars = readlink(filename,buffer,size)) == size) {
> +		size *= 2;
> +		if ((buffer = (char*)realloc(buffer,size)) == NULL) {

This loses the previous value of buffer. You need to store the the
result of realloc in another variable, and free the original buffer in
the error condition.

> +			errno = ENOMEM;
> +			return NULL;
> +		}
> +	}
> +	if (nchars < 0) {
> +		free (buffer);
> +		errno = EINVAL;
> +		return NULL;
> +	}
> +	buffer[nchars]='\0';
> +	return buffer;
> +}
> +#endif
> +
>  char* expandpath(const char * path)
>  {
> +#ifndef __GNU__
>  	char tmpbuf[PATH_MAX+1];
> +#else
> +	char *tmpbuf=NULL;
> +#endif
>  	const char *start, *end;
>  	char *curr, *dest;
>  	int deep = MAXSYMLINKS;
> @@ -1962,6 +1992,7 @@
>  	if (!path || *path == '\0')
>  		return (char*)0;
>  
> +#ifndef __GNU__
>  	curr = &real[0];
>  
>  	if (*path != '/') {
> @@ -1972,6 +2003,17 @@
>  		*curr = '/';
>  		dest = curr + 1;
>  	}
> +#else
> +	if (*path != '/') {
> +		if ((curr = get_current_dir_name()) == NULL)
> +			return (char*)0;
> +		dest = rawmemchr(curr, '\0');
> +	} else {
> +		curr = malloc(1 + 1);
> +		*curr = '/';
> +		dest = curr + 1;
> +	}
> +#endif

I'd say rather put ifndef around initial curr = , and use #ifndef/#else
around the getcwd() call, only, so that the rest of the code can be kept
factorized.

> @@ -1990,27 +2032,37 @@
>  				while ((--dest)[-1] != '/')
>  					;
>  		} else {
> +#ifndef __GNU__
>  			char lnkbuf[PATH_MAX+1];
> +#else
> +			char *lnkbuf = NULL;
> +#endif
>  			size_t len;
>  			ssize_t n;
>  
>  			if (dest[-1] != '/')
>  				*dest++ = '/';
>  
> +#ifndef __GNU__
>  			if (dest + (end - start) > curr + PATH_MAX) {
>  				errno = ENAMETOOLONG;
>  				return (char*)0;
>  			}
> +#endif
>  
>  			dest = mempcpy(dest, start, end - start);
>  			*dest = '\0';
>  
>  			if (deep-- < 0) {
>  				errno = ELOOP;
> +#ifdef __GNU__
> +				free(curr);
> +#endif
>  				return (char*)0;
>  			}
>  
>  			errno = 0;
> +#ifndef __GNU__
>  			if ((n = readlink(curr, lnkbuf, PATH_MAX)) < 0) {
>  				deep = MAXSYMLINKS;
>  				if (errno == EINVAL)
> @@ -2024,6 +2076,21 @@
>  				errno = ENAMETOOLONG;
>  				return (char*)0;
>  			}
> +#else
> +			lnkbuf = xreadlink(curr);
> +			if (lnkbuf == NULL) {
> +				deep = MAXSYMLINKS;
> +				if (errno == EINVAL)
> +					continue;	/* Not a symlink */
> +				free(lnkbuf);
> +				free(curr);
> +				return (char*)0;
> +			}
> +
> +			len = strlen(end);
> +			n = strlen(lnkbuf);
> +			tmpbuf = malloc(n + len + 1);
> +#endif
>  
>  			memmove(&tmpbuf[n], end, len + 1);
>  			path = end = memcpy(tmpbuf, lnkbuf, n);
> @@ -2033,6 +2100,9 @@
>  			else if (dest > curr + 1)
>  			    while ((--dest)[-1] != '/');
>  			
> +#ifdef __GNU__
> +	free(lnkbuf);

indentation issue


All that being said, fuser will not work for now since we don't have the
corresponding files in /proc.  Should we perhaps rather just disable the
build of fuser, instead trying to get a PATH_MAX patch applied?

Samuel


Reply to: