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

Re: [PATCH] [GSoC] Use strchrnul to save additional scan of string

From
Jeff King <peff@peff.net>
Date
Mar 15, 2014, 02:49 UTC
Message-ID
<20140315024946.GA7031@sigill.intra.peff.net>
In-Reply-To
<CACqsmRbYwEKrF2+NbpLLnx2zrovwFV+OQe64Zpzg5WGpRqGfJw@mail.gmail.com>
On Sat, Mar 15, 2014 at 06:19:08AM +0530, Shubham Chaudhary wrote:
> From c422507408824403ed18e89ec0bbc32b8764e09c Mon Sep 17 00:00:00 2001
You can drop this line; it's just part of the mbox format.
> From: Shubham Chaudhary <shubham.chaudhary@kdemail.net>
> Date: Sat, 15 Mar 2014 05:56:18 +0530
> Subject: [PATCH] [GSoC] Use strchrnul to save additional scan of string
And these are redundant with your mail headers. You can drop all of them.

Your patch also appears to be whitespace-damaged (it looks like extra wrawpping). Consider using git-send-email, which takes care of all of these issues.

Show 12 quoted lines
> diff --git a/archive.c b/archive.c
> index 346f3b2..d196215 100644
> --- a/archive.c
> +++ b/archive.c
> @@ -259,8 +259,8 @@ static void parse_treeish_arg(const char **argv,
>  	/* Remotes are only allowed to fetch actual refs */
>  	if (remote) {
>  		char *ref = NULL;
> -		const char *colon = strchr(name, ':');
> -		int refnamelen = colon ? colon - name : strlen(name);
> +		const char *colon = strchrnul(name, ':');
> +		int refnamelen = colon - name;

This one is pretty straightforward, as we do not need ever look at "colon" after this. But this one:

Show 20 quoted lines
> diff --git a/builtin/mailinfo.c b/builtin/mailinfo.c
> index 2c3cd8e..f901cf3 100644
> --- a/builtin/mailinfo.c
> +++ b/builtin/mailinfo.c
> @@ -902,12 +902,8 @@ static void output_header_lines(FILE *fout, const
> char *hdr, const struct strbuf
>  {
>  	const char *sp = data->buf;
>  	while (1) {
> -		char *ep = strchr(sp, '\n');
> -		int len;
> -		if (!ep)
> -			len = strlen(sp);
> -		else
> -			len = ep - sp;
> +		char *ep = strchrnul(sp, '\n');
> +		int len = ep - sp;
>  		fprintf(fout, "%s: %.*s\n", hdr, len, sp);
>  		if (!ep)
>  			break;

...does not look right. Before your patch, "ep" pointed to a newline, or NULL if we did not find one. After the fprintf, we try to break out of the loop if we did not find a newline by checking "!ep". But that will never trigger after your patch, and we loop forever reading bogus data past the end of the string.

I didn't check the other sites; this might be the only problematic one, but each one needs to be examined by hand. Please double-check the result by running "make test", which does find this bug.

-Peff
Previous: Shubham Chaudhary
Message 2 of 2 in “[GSoC] Use strchrnul to save additional scan of string”
  1. [GSoC] Use strchrnul to save additional scan of stringShubham Chaudhary, Mar 15, 2014
  2. Jeff KingMar 15, 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.