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

Re: [PATCH] files_for_each_reflog_ent_reverse(): close stream and free strbuf on error

From
Michael Haggerty <mhagger@alum.mit.edu>
Date
Apr 17, 2017, 07:05 UTC
Message-ID
<d26b6409-988e-386c-1fb3-914319ada8a6@alum.mit.edu>
In-Reply-To
<a77dfa47-3153-3f8c-3c37-30147f93e61b@web.de>
On 04/16/2017 06:55 PM, René Scharfe wrote:
> Exit the loop orderly through the cleanup code, instead of dashing out
> with logfp still open and sb leaking.
> 
> Found with Cppcheck.
Nice catch.
Show 18 quoted lines
> Signed-off-by: Rene Scharfe <l.s.r@web.de>
> ---
>  refs/files-backend.c | 20 ++++++++++++--------
>  1 file changed, 12 insertions(+), 8 deletions(-)
> 
> diff --git a/refs/files-backend.c b/refs/files-backend.c
> index 50188e92f9..2889f21568 100644
> --- a/refs/files-backend.c
> +++ b/refs/files-backend.c
> @@ -3159,8 +3159,8 @@ static int files_for_each_reflog_ent_reverse(struct ref_store *ref_store,
>  
>  	/* Jump to the end */
>  	if (fseek(logfp, 0, SEEK_END) < 0)
> -		return error("cannot seek back reflog for %s: %s",
> -			     refname, strerror(errno));
> +		ret = error("cannot seek back reflog for %s: %s",
> +			    refname, strerror(errno));
>  	pos = ftell(logfp);

It seems odd to call `ftell()` in the case that `fseek()` has failed, but it should be harmless.

Show 27 quoted lines
>  	while (!ret && 0 < pos) {
>  		int cnt;
> @@ -3170,13 +3170,17 @@ static int files_for_each_reflog_ent_reverse(struct ref_store *ref_store,
>  
>  		/* Fill next block from the end */
>  		cnt = (sizeof(buf) < pos) ? sizeof(buf) : pos;
> -		if (fseek(logfp, pos - cnt, SEEK_SET))
> -			return error("cannot seek back reflog for %s: %s",
> -				     refname, strerror(errno));
> +		if (fseek(logfp, pos - cnt, SEEK_SET)) {
> +			ret = error("cannot seek back reflog for %s: %s",
> +				    refname, strerror(errno));
> +			break;
> +		}
>  		nread = fread(buf, cnt, 1, logfp);
> -		if (nread != 1)
> -			return error("cannot read %d bytes from reflog for %s: %s",
> -				     cnt, refname, strerror(errno));
> +		if (nread != 1) {
> +			ret = error("cannot read %d bytes from reflog for %s: %s",
> +				    cnt, refname, strerror(errno));
> +			break;
> +		}
>  		pos -= cnt;
>  
>  		scanp = endp = buf + cnt;
> 
Reviewed-by: Michael Haggerty <mhagger@alum.mit.edu>
Michael
Previous: René Scharfe
Message 2 of 2 in “files_for_each_reflog_ent_reverse(): close stream and free strbuf on error”
  1. files_for_each_reflog_ent_reverse(): close stream and free strbuf on errorRené Scharfe, Apr 16, 2017
  2. Michael HaggertyApr 17, 2017

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.