[PATCH] Quote colons in filenames/paths

Alan Coopersmith alan.coopersmith at oracle.com
Sat May 5 18:05:10 UTC 2018


Sorry for the slow response, but as you may have guessed by now, makedepend
is one of the components no one is actively maintaining.  But since we've got
a number of fixes in git, we should probably do a release soon, and I saw
this still sitting in the queue.

This seems like a good idea, but there's a couple minor issues in the
implementation.  Instead of prolonging this further with a runaround,
I've gone ahead and fixed them, and will send out a revised patch for
review momentarily.

On 02/ 2/16 09:31 AM, Antonio Larrosa wrote:
> Makefile doesn't like colons in filenames/paths so they must
> be quoted in the output. Otherwise makedepend doesn't work with
> full paths that contain a colon.
> ---
>  pr.c | 42 ++++++++++++++++++++++++++++++++++++++++--
>  1 file changed, 40 insertions(+), 2 deletions(-)
> 
> diff --git a/pr.c b/pr.c
> index 04abef9..619be45 100644
> --- a/pr.c
> +++ b/pr.c
> @@ -63,6 +63,43 @@ add_include(struct filepointer *filep, struct inclist *file,
>  	}
>  }
>  
> +/**
> + * Replaces all ":" occurrences in @p input with "\:" using @p outputbuffer (of size @p bufsize) 
> + * possibly to hold the result. @p returns the string with quoted colons
> + */
> +const char *quoteColons(const char *input, char *outputbuffer, int bufsize)

This made gcc 7.3 complain:

pr.c:70:13: warning: no previous prototype for ‘quoteColons’ [-Wmissing-prototypes]
 const char *quoteColons(const char *input, char *outputbuffer, int bufsize)
             ^~~~~~~~~~~

so I stuck a "static" in front to make it happy.

> +{
> +        const char *tmp=input;
> +        const char *loc;
> +        char *output=outputbuffer;
> +
> +        loc = strchr(input, ':');
> +        if (loc == NULL) {
> +                return input;
> +        }
> +
> +        tmp=input;
> +        while (loc != NULL && bufsize > loc-tmp+2 ) {
> +                memcpy(output, tmp, loc-tmp);
> +                output+=loc-tmp;
> +                bufsize-=loc-tmp+2;
> +                tmp=loc+1;
> +                *output='\\';
> +                output++;
> +                *output=':';
> +                output++;
> +                loc = strchr(tmp, ':');
> +        }
> +
> +        if (strlen(tmp) <= bufsize)
> +           strcpy(output, tmp);
> +        else {
> +           strncpy(output, tmp, bufsize-1);
> +           output[bufsize]=0;
> +        }
> +        return outputbuffer;
> +}
> +
>  static void
>  pr(struct inclist *ip, const char *file, const char *base)
>  {
> @@ -70,18 +107,19 @@ pr(struct inclist *ip, const char *file, const char *base)
>  	static int	current_len;
>  	register int	len, i;
>  	char	buf[ BUFSIZ ];
> +	char	quotebuf[ BUFSIZ ];
>  
>  	printed = TRUE;
>  	len = strlen(ip->i_file)+1;
>  	if (current_len + len > width || file != lastfile) {
>  		lastfile = file;
>  		snprintf(buf, sizeof(buf), "\n%s%s%s: %s",
> -			 objprefix, base, objsuffix, ip->i_file);
> +			 objprefix, base, objsuffix, quoteColons(ip->i_file, quotebuf, sizeof(quotebuf)));
>  		len = current_len = strlen(buf);

This branch has the cosmetic issue that if quotes were inserted, they aren't
counted when checking to see if the filename fits on the current line or if
a new line should be started.

>  	}
>  	else {
>  		buf[0] = ' ';
> -		strcpy(buf+1, ip->i_file);
> +		strcpy(buf+1, quoteColons(ip->i_file, quotebuf, sizeof(quotebuf)));
>  		current_len += len;
>  	}
>  	fwrite(buf, len, 1, stdout);

This branch has the bigger issue that if quotes were inserted, they're not
counted towards the number of bytes that need to be written by fwrite().

I fixed both by moving the call to quoteColons up to the top and then making
len be the strlen of it's result.

I also fixed a potential 1 byte overflow in the second branch by replacing
strcpy with snprintf.  (Previously you could append as many as BUFSIZ characters
to buf after the initial space, but buf only has room for BUFSIZ, not BUFSIZ + 1
there.  Unlikely to matter, but still enough to trigger static analyzers.)

-- 
	-Alan Coopersmith-               alan.coopersmith at oracle.com
	 Oracle Solaris Engineering - https://blogs.oracle.com/alanc


More information about the xorg-devel mailing list