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

Re: [PATCH] dir: do all size checks before seeking back and fix file closing

From
Linus Torvalds <torvalds@osdl.org>
Date
Aug 26, 2006, 22:13 UTC
Message-ID
<Pine.LNX.4.64.0608261509290.11811@g5.osdl.org>
In-Reply-To
<20060826141709.GC11601@diku.dk>
On Sat, 26 Aug 2006, Jonas Fonseca wrote:
Show 15 quoted lines
> 
> diff --git a/dir.c b/dir.c
> index d53d48f..ff8a2fb 100644
> --- a/dir.c
> +++ b/dir.c
> @@ -122,11 +122,11 @@ static int add_excludes_from_file_1(cons
>  	size = lseek(fd, 0, SEEK_END);
>  	if (size < 0)
>  		goto err;
> -	lseek(fd, 0, SEEK_SET);
>  	if (size == 0) {
>  		close(fd);
>  		return 0;
>  	}
> +	lseek(fd, 0, SEEK_SET);

I really think you'd be better off rewriting that to use "fstat()" instead. I don't know why it uses two lseek's, but it's wrong, and looks like some bad habit Junio picked up at some point.

Show 7 quoted lines
> @@ -146,7 +146,7 @@ static int add_excludes_from_file_1(cons
>  	return 0;
>  
>   err:
> -	if (0 <= fd)
> +	if (0 >= fd)
>  		close(fd);
That's wrong. 

Now, admittedly it's wrong because another bad habit Junio picked up (doing comparisons with constants in the wrong order), so please write it as

	if (fd >= 0)
		close(fd);
instead.
Junio: I realize that you claim that you learnt that syntax from an 
authorative source, but he was _wrong_. Doing the constant first is more 
likely to cause bugs, rather than less. Compilers will warn about the
	if (x = 0)
		..
kind of bug, and putting the constant first just confuses humans.

It's more important to _not_ confuse humans than it is to try to avoid an uncommon error that compilers can and do warn about anyway.

			Linus
Previous: Jonas FonsecaNext: Jakub Narebski
Message 4 of 10 in “dir: do all size checks before seeking back and fix file closing”
  1. dir: do all size checks before seeking back and fix file closingJonas Fonseca, Aug 26, 2006
  2. Mitchell Blank JrAug 26, 2006
  3. Jonas FonsecaAug 26, 2006
  4. Linus TorvaldsAug 26, 2006
  5. Jakub NarebskiAug 26, 2006
  6. Junio C HamanoAug 27, 2006
  7. Junio C HamanoAug 27, 2006
  8. Linus TorvaldsAug 27, 2006
  9. Junio C HamanoAug 27, 2006
  10. Use fstat instead of fseekJonas Fonseca, Aug 27, 2006

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.