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

Re: [PATCH] range-diff: support reading mbox files

From
Johannes Schindelin <johannes.schindelin@gmx.de>
Date
Nov 18, 2022, 12:53 UTC
Message-ID
<nn1s836q-nro7-10rp-6r89-170p9r942062@tzk.qr>
In-Reply-To
<50d8767b-cb8c-74c5-b280-43472fa074a2@dunelm.org.uk>
Hi Phillip,
On Thu, 17 Nov 2022, Phillip Wood wrote:
Show 17 quoted lines
> On 16/11/2022 14:40, Phillip Wood wrote:
> > > +        } else if (state == MBOX_IN_DIFF) {
> > > +            switch (line[0]) {
> > > +            case '\0':
> > > +                continue; /* ignore empty lines after diff */
> > > +            case '+':
> > > +            case '-':
> > > +            case ' ':
> > > +                if (!old_count && !new_count)
> > > +                    break;
> > 
> > This shouldn't happen in a well formed diff. Below we happily accept bad
> > counts, is there a reason to reject them here?
> 
> I think this might be picking up the "--" at the end of the patch as we don't
> want to break here at the end of a hunk. If so then a comment would be
> helpful.
Agreed. And yes, it is picking up the "-- " line at the end of the patch.
Show 9 quoted lines
> > > +                if (old_count && line[0] != '+')
> > > +                    old_count--;
> > > +                if (new_count && line[0] != '-')
> > > +                    new_count--;
> > 
> > The diff is malformed if old_count == 0 and we see '-' or ' ' or new_count
> > == 0 and we see '+' or ' '. The code is careful not to decrement the count
> > in that case so I think it is harmless to accept diffs with bad line counts
> > in the hunk header.

I might be overly cautious here, but as you mentioned elsewhere, it is really bad if a `size_t` is decremented below 0, and `new_count`/`old_count` are of that type.

Show 11 quoted lines
> > > +                /* fallthrough */
> > > +            case '\\':
> > > +                strbuf_addstr(&buf, line);
> > > +                strbuf_addch(&buf, '\n');
> > > +                util->diffsize++;
> > 
> > I think this might be a better place to break if old_count and new_count are
> > both zero.
> 
> It would be the right place to break at the end of each hunk, but I don't
> think we want to do that.

It would not even be the right place to break here then: think of the `\ No newline at end of file` lines: they come after the preceding line decremented `old_count`/`new_count`, yet we still want them to be part of the diff.

Show 25 quoted lines
> 
> > > +                continue;
> > > +            case '@':
> > > +                if (parse_hunk_header(line, &old_count,
> > > +                              &new_count, &p))
> > > +                    break;
> > > +
> > > +                strbuf_addstr(&buf, "@@");
> > > +                if (current_filename && *p)
> > > +                    strbuf_addf(&buf, " %s:",
> > > +                            current_filename);
> > > +                strbuf_addstr(&buf, p);
> > > +                strbuf_addch(&buf, '\n');
> > > +                util->diffsize++;
> > > +                continue;
> > > +            }
> > 
> > This is effectively the `default:` clause as it is executed when we don't
> > handle the line above. We ignore the contents of this line which makes me
> > wonder what happens if it is the start of another diff.
> 
> We'll pick that up earlier with "if (starts_with(line, "diff --git"))"
> 
> We only get here at the end of a patch (assuming it has the "--" line from
> format-patch)
We also get here in case of garbage in the middle of a diff ;-)

Thank you for setting a fantastic example how to review code in a constructive, helpful manner! Dscho

Previous: Phillip WoodNext: Johannes Schindelin
Message 5 of 16 in “range-diff: support reading mbox files”
  1. range-diff: support reading mbox filesJohannes Schindelin via GitGitGadget, Nov 15, 2022
  2. Taylor BlauNov 16, 2022
  3. Phillip WoodNov 16, 2022
  4. Phillip WoodNov 17, 2022
  5. Johannes SchindelinNov 18, 2022
  6. Johannes SchindelinNov 18, 2022
  7. Ævar Arnfjörð BjarmasonNov 17, 2022
  8. Johannes SchindelinNov 18, 2022
  9. range-diff: support reading mbox filesJohannes Schindelin via GitGitGadget, Nov 19, 2022
  10. Phillip WoodNov 21, 2022
  11. Johannes SchindelinNov 22, 2022
  12. Phillip WoodNov 22, 2022
  13. range-diff: support reading mbox filesJohannes Schindelin via GitGitGadget, Nov 22, 2022
  14. Phillip WoodNov 22, 2022
  15. Junio C HamanoNov 22, 2022
  16. Junio C HamanoMar 3, 2023

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.