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

Re: [PATCH] strbuf_read_file(): preserve errno across close() call

From
René Scharfe <l.s.r@web.de>
Date
Feb 23, 2018, 22:55 UTC
Message-ID
<612cc15c-624c-0963-4d12-aed0993ea16c@web.de>
In-Reply-To
<xmqqk1v3s64y.fsf@gitster-ct.c.googlers.com>
Am 23.02.2018 um 23:17 schrieb Junio C Hamano:
Show 35 quoted lines
> René Scharfe <l.s.r@web.de> writes:
> 
>> +#define IGNORE_ERROR(expr) do { int e_ = errno; expr; errno = e_; } while (0)
> 
> The macro certainly is a cute idea, but ...
> 
>> @@ -391,7 +393,7 @@ ssize_t strbuf_read(struct strbuf *sb, int fd, size_t hint)
>>   
>>   		if (got < 0) {
>>   			if (oldalloc == 0)
>> -				strbuf_release(sb);
>> +				IGNORE_ERROR(strbuf_release(sb));
>>   			else
>>   				strbuf_setlen(sb, oldlen);
>>   			return -1;
> 
> ... ideally, I would imagine that we wish we could write this hunk
> to something that expands to:
> 
> 		if (got < 0) {
> 			do {
>                                  int e_ = errno;
>                                  if (oldalloc == 0)
>                                          strbuf_release(sb);
>                                  else
>                                          strbuf_setlen(sb, oldlen);
>                                  errno = e_;
> 			} while (0);
> 			return -1;
> 
> no?  That is (1) we do not want to rely too much on knowing that
> strbuf_setlen() is very thin and does not touch errno, and hence (2)
> we want to mark not just a single expr but a block as "we know we
> got an error and errno from that error is more precious than what we
> do in this block to clean thihngs up".

Relying on that internal knowledge should be OK in strbuf.c, but in this specific example we could of course do:

			if (oldalloc == 0)
				IGNORE_ERROR(strbuf_release(sb));
			else
				IGNORE_ERROR(strbuf_setlen(sb, oldlen));

I guess ignoring errors of whole blocks is not that common, based on a quick search (git grep -W int.*_errno). And in such a case we could factor that code out into a separate function, if really needed. Or continue saving errno explicitly.

Compilers should be smart enough to avoid saving and restoring errno between multiple uses of that macro, e.g. code like this would only do it once, from what I saw when experimenting with the Compiler Explorer (https://godbolt.org/):

	IGNORE_ERROR(close(fd1));
	IGNORE_ERROR(close(fd2));
Show 9 quoted lines
> Of course, a pair of macros
> 
> 	#define IGNORE_ERROR_BEGIN do { int e_ = errno
> 	#define IGNORE_ERROR_END errno = e_; } while (0)
> 
> is probably the only way to do so in C, and that is already too ugly
> to live, so we cannot achieve the ideal.
> 
> So I dunno..
*shudder*
Show 15 quoted lines
> 
>> @@ -617,9 +619,11 @@ ssize_t strbuf_read_file(struct strbuf *sb, const char *path, size_t hint)
>>   	if (fd < 0)
>>   		return -1;
>>   	len = strbuf_read(sb, fd, hint);
>> -	close(fd);
>> -	if (len < 0)
>> +	if (len < 0) {
>> +		IGNORE_ERROR(close(fd));
>>   		return -1;
>> +	}
>> +	close(fd);
>>   
>>   	return len;
>>   }
Previous: Junio C HamanoNext: Jeff King
Message 7 of 8 in “sequencer: factor out strbuf_read_file_or_whine()”
  1. sequencer: factor out strbuf_read_file_or_whine()René Scharfe, Feb 22, 2018
  2. Junio C HamanoFeb 22, 2018
  3. Jeff KingFeb 23, 2018
  4. strbuf_read_file(): preserve errno across close() callJeff King, Feb 23, 2018
  5. René ScharfeFeb 23, 2018
  6. Junio C HamanoFeb 23, 2018
  7. René ScharfeFeb 23, 2018
  8. Jeff KingFeb 26, 2018

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.