On Fri, Sep 16, 2016 at 12:12:50PM -0700, Junio C Hamano wrote:
> > +static int check_header_raw(struct mailinfo *mi,
> > + char *buf, size_t len,
> > + struct strbuf *hdr_data[], int overwrite) {
> > + const struct strbuf sb = {0, len, buf};
> > + return check_header(mi, &sb, hdr_data, overwrite);
> > +}
>
> IIUC, this is a helper for callers that do not have a strbuf but
> instead have <buf, len> pair to perform the same check_header() the
> callers that have strbuf can do.
>
> As check_header() uses the strbuf as a read-only entity, wrapping
> the <buf, len> pair in a temporary strbuf like this is safe.
>
> The incoming <buf> should conceptually be "const char *", but it's
> OK.
I think the "right" way to do this would be to continue taking a "char
*", and then strbuf_attach() it. That saves us from unexpectedly
violating any strbuf invariants.
If our assumption that check_header() does not touch the
contents turns out to be wrong, neither strategy would inform our
caller, though. I think you'd want something like:
assert(sb.buf == buf);
after check_header() returns (though I guess we are in theory protected
by the "const").
That being said...
> If check_header() didn't call any helper function that gets passed
> &sb as a strbuf, or if convertiong the helper function to take a
> <buf, len> pair instead, I would actually suggest refactoring this
> the other way around, though. That is, move the implementation of
> check_header() to this function, updating its reference to line->buf
> and line->len to reference to <buf> and <len>, and then make
> check_header() a thin wrapper that does
>
> check_header(mi, const struct strbuf *line,
> struct strbuf *hdr_data[], int overwrite)
> {
> return check_header_raw(mi, line->buf, line->len,
> hdr_data, overwrite);
> }
This is _way_ better, and it looks like check_header() could handle it
easily. Looking at it, I also suspect the cascading if in that function
could be made more pleasant by modeling cmp_header()'s interface after
skip_prefix_mem(), but that is totally orthogonal and optional.
-Peff