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

Re: [PATCH 02/15] read-cache: Improve readability

From
Stefan Beller <sbeller@google.com>
Date
Mar 21, 2015, 05:11 UTC
Message-ID
<CAGZ79kaLJgC6rWdRSeVVQSBYn0soJuzqnrK_bNtgAPWimPYE8w@mail.gmail.com>
In-Reply-To
<xmqqbnjnaso7.fsf@gitster.dls.corp.google.com>
On Fri, Mar 20, 2015 at 9:19 PM, Junio C Hamano <gitster@pobox.com> wrote:
Show 42 quoted lines
> Stefan Beller <sbeller@google.com> writes:
>
>> Signed-off-by: Stefan Beller <sbeller@google.com>
>> ---
>>  read-cache.c | 4 +---
>>  1 file changed, 1 insertion(+), 3 deletions(-)
>>
>> diff --git a/read-cache.c b/read-cache.c
>> index f72ea9f..769897e 100644
>> --- a/read-cache.c
>> +++ b/read-cache.c
>> @@ -703,9 +703,7 @@ int add_to_index(struct index_state *istate, const char *path, struct stat *st,
>>                   !hashcmp(alias->sha1, ce->sha1) &&
>>                   ce->ce_mode == alias->ce_mode);
>>
>> -     if (pretend)
>> -             ;
>> -     else if (add_index_entry(istate, ce, add_option))
>> +     if (!pretend && add_index_entry(istate, ce, add_option))
>>               return error("unable to add %s to index",path);
>>       if (verbose && !was_same)
>>               printf("add '%s'\n", path);
>
> I have a moderately strong feeling against this change, as the code
> was done this way quite deliberately to keep it readable, namely, to
> avoid using && to concatenate two boolean expressions that are in
> totally different class inside condition part of if/while, where A
> is a precondition guard for B (i.e. you cannot evaluate B unless A
> holds) and B is called primarily for its side effect.  The problem
> is that, once you start liberally doing
>
>         if (A && B && C && D ...)
>
> with booleans with mixed semantics (guards and actions), it will
> quickly get harder to tell which one is which.
>
> I could have written it as
>
>         if (!pretend) {
>                 if (add_index_entry(...))
>                         return error(...);
>         }

This makes sense to point out the different semantics to me. Maybe I have read too much of the refs code lately as there we have these long chains which combine precondition with error checking. :/ That's why I thought it would be global to git to not care much about this semantics distinction.

Show 16 quoted lines
>
> and that would have been just as readable as the original; it
> clearly separates the guard (i.e. only do the add-index thing when
> we are not pretending) and the operation that is done for the side
> effect.
>
> But I find the original tells you "if pretend mode, do *nothing*"
> and "otherwise, try add_index_entry() and act on its error" very
> clearly.  Of course, I am biased as the original is my code from
> 38ed1d89 ("git-add -n -u" should not add but just report,
> 2008-05-21).
>
> FYI, between preference and taste, I'd say this one is much closer
> to the latter than the former.
>
> By the way, aren't we leaking ce when we are merely pretending?

Yes we are, that's how I found this spot. (coverity pointed out ce was leaking, so I was refactoring to actually make it easier to fix it, and then heavily reordered the patch series afterwards. That spot was forgotten apparently.

>
>
Previous: Junio C HamanoNext: Junio C Hamano
Message 11 of 44 in “Fixing memory leaks”
  1. 00/15 Fixing memory leaksStefan Beller, Mar 21, 2015
  2. 01/15 read-cache: fix memleakStefan Beller, Mar 21, 2015
  3. Junio C HamanoMar 21, 2015
  4. Stefan BellerMar 23, 2015
  5. 1/2 read-cache: fix memleakStefan Beller, Mar 23, 2015
  6. 2/2 read-cache.c: fix a memleak in add_to_indexStefan Beller, Mar 23, 2015
  7. Junio C HamanoMar 23, 2015
  8. Junio C HamanoMar 23, 2015
  9. 02/15 read-cache: Improve readabilityStefan Beller, Mar 21, 2015
  10. Junio C HamanoMar 21, 2015
  11. Stefan BellerMar 21, 2015
  12. Junio C HamanoMar 22, 2015
  13. 03/15 read-cache: free cache entry in add_to_index in case of early returnStefan Beller, Mar 21, 2015
  14. Junio C HamanoMar 21, 2015
  15. Stefan BellerMar 21, 2015
  16. Junio C HamanoMar 22, 2015
  17. 04/15 update-index: fix a memleakStefan Beller, Mar 21, 2015
  18. Junio C HamanoMar 21, 2015
  19. update-index: Don't copy memory aroundStefan Beller, Mar 23, 2015
  20. Junio C HamanoMar 23, 2015
  21. 05/15 builtin/apply.c: fix a memleakStefan Beller, Mar 21, 2015
  22. Junio C HamanoMar 21, 2015
  23. builtin/apply.c: fix a memleak (Fixup, squashable)Stefan Beller, Mar 23, 2015
  24. Junio C HamanoMar 23, 2015
  25. 06/15 merge-blobs.c: Fix a memleakStefan Beller, Mar 21, 2015
  26. 07/15 merge-recursive: fix memleaksStefan Beller, Mar 21, 2015
  27. Junio C HamanoMar 21, 2015
  28. 08/15 http-push: Remove unneeded cleanupStefan Beller, Mar 21, 2015
  29. 09/15 http: release the memory of a http pack request as wellStefan Beller, Mar 21, 2015
  30. Junio C HamanoMar 22, 2015
  31. Stefan BellerMar 24, 2015
  32. Junio C HamanoMar 24, 2015
  33. 10/15 commit.c: fix a memory leakStefan Beller, Mar 21, 2015
  34. Junio C HamanoMar 21, 2015
  35. Duy NguyenMar 24, 2015
  36. Re* [PATCH 10/15] commit.c: fix a memory leakJunio C Hamano, Mar 24, 2015
  37. Stefan BellerMar 24, 2015
  38. 11/15 builtin/check-attr: fix a memleakStefan Beller, Mar 21, 2015
  39. Junio C HamanoMar 21, 2015
  40. 12/15 builtin/merge-base: fix memleakStefan Beller, Mar 21, 2015
  41. 13/15 builtin/unpack-file: fix a memleakStefan Beller, Mar 21, 2015
  42. 14/15 builtin/cat-file: free memleakStefan Beller, Mar 21, 2015
  43. 15/15 ls-files: fix a memleakStefan Beller, Mar 21, 2015
  44. Junio C HamanoMar 21, 2015

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.