git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH] Improve function dir.c:trim_trailing_spaces()

From
Jeff King <peff@peff.net>
Date
Jun 2, 2014, 06:47 UTC
Message-ID
<20140602064706.GB27254@sigill.intra.peff.net>
In-Reply-To
<1401549691-20788-1-git-send-email-pasha.bolokhov@gmail.com>
On Sat, May 31, 2014 at 08:21:31AM -0700, Pasha Bolokhov wrote:
Show 11 quoted lines
> +	char *p, *last_space = NULL;
> +
> +	for (p = buf; *p; p++)
> +		if (*p == ' ') {
> +			if (!last_space)
> +				last_space = p;
> +		} else {
> +			if (*p == '\\')
> +				p++;
> +			last_space = NULL;
> +		}

Your backslash-escape works here by incrementing "p" an extra time. So we move past the backslash to the next character (which is escaped), and then the for-loop increments it again to the character beyond that, which is the next one worth considering.

What happens if we are parsing a string with an unmatched backslash at the end of the string, like:

  foo\

We consider the end-of-string NUL to be escaped, skip it, and then keep reading whatever random bytes are in memory after the string.

The original version did not have a problem with this because it used a length, which meant that "i < len" caught this case.

I think you either need to insert an extra check for "!p[1]" when moving past the escaped character, or move back to a length-based check.

-Peff
Previous: Pasha Bolokhov
Message 2 of 2 in “Improve function dir.c:trim_trailing_spaces()”
  1. Improve function dir.c:trim_trailing_spaces()Pasha Bolokhov, May 31, 2014
  2. Jeff KingJun 2, 2014

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.