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

Re: [PATCHv3 6/7] submodule--helper: add remote-branch helper

From
Stefan Beller <sbeller@google.com>
Date
Aug 3, 2016, 20:11 UTC
Message-ID
<CAGZ79kYW0dwUh=QpKL9W+cv8k_Z9Vc2wxoLMqud5brDtS9DYpA@mail.gmail.com>
In-Reply-To
<20160803162512.veyff5g52kaqwlgx@sigill.intra.peff.net>
On Wed, Aug 3, 2016 at 9:25 AM, Jeff King <peff@peff.net> wrote:
Show 22 quoted lines
> On Thu, Jul 28, 2016 at 05:44:08PM -0700, Stefan Beller wrote:
>
>> +static const char *remote_submodule_branch(const char *path)
>> +{
>> +     const struct submodule *sub;
>> +     gitmodules_config();
>> +     git_config(submodule_config, NULL);
>> +
>> +     sub = submodule_from_path(null_sha1, path);
>> +     if (!sub->branch)
>> +             return "master";
>> +
>> +     return sub->branch;
>> +}
>
> Coverity complains about "sub" being NULL here, and indeed, it seems
> like an easy segfault:
>
>   $ ./git submodule--helper remote-branch foo
>   Segmentation fault
>
> I guess this should return NULL in that case. But then this...

I thought about checking for (!sub && !sub->branch) to return "master"; However I disregarded that implementation as the submodule-config is reliable for any correct input. Though we need to handle wrong input as well. So how about:

  if (!sub)
    die(_("A submodule doesn't exist at path %s"), path);
  if (!sub->branch)
    return "master";
...
I think that explains best why we even check for !sub.
Show 16 quoted lines
>
>> +static int resolve_remote_submodule_branch(int argc, const char **argv,
>> +             const char *prefix)
>> +{
>> +     struct strbuf sb = STRBUF_INIT;
>> +     if (argc != 2)
>> +             die("submodule--helper remote-branch takes exactly one arguments, got %d", argc);
>> +
>> +     printf("%s", remote_submodule_branch(argv[1]));
>> +     strbuf_release(&sb);
>> +     return 0;
>> +}
>
> would need to handle the NULL return, as well. So maybe "master" or the
> empty string would be better. I haven't followed the topic closely
> enough to have an opinion.

Oh I see, this can turn into a discussion what the API for remote_submodule_branch is. I think handling the NULL here and dying makes more sense as then we can keep remote_submodule_branch pure to its intended use case. (If in the far future we have all the submodule stuff in C, we may want to call remote_submodule_branch when we already know that the path is valid, so no need to check it in there. Also not dying in there is more libified.)

Thanks for this review, Stefan

>
> -Peff
Previous: Jeff KingNext: Jeff King
Message 10 of 12 in “[PATCHv3 0/7] submodule update: allow '.' for branch value”
  1. Stefan BellerJul 29, 2016
  2. 1/7 t7406: future proof tests with hard coded depthStefan Beller, Jul 29, 2016
  3. 2/7 submodule update: respect depth in subsequent fetchesStefan Beller, Jul 29, 2016
  4. 4/7 submodule--helper: fix usage string for relative-pathStefan Beller, Jul 29, 2016
  5. 5/7 submodule-config: keep configured branch aroundStefan Beller, Jul 29, 2016
  6. 3/7 submodule update: narrow scope of local variableStefan Beller, Jul 29, 2016
  7. 7/7 submodule update: allow '.' for branch valueStefan Beller, Jul 29, 2016
  8. 6/7 submodule--helper: add remote-branch helperStefan Beller, Jul 29, 2016
  9. Jeff KingAug 3, 2016
  10. Stefan BellerAug 3, 2016
  11. Jeff KingAug 3, 2016
  12. Junio C HamanoAug 1, 2016

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.