git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Atharva Raykar <raykar.ath@gmail.com>
To: Git List <git@vger.kernel.org>
Cc: Christian Couder <christian.couder@gmail.com>,
	"brian m. carlson" <sandals@crustytoothpaste.net>,
	Heiko Voigt <hvoigt@hvoigt.net>
Subject: [BUG?] submodule-config.c:config_from() has left me confused
Date: Thu, 22 Jul 2021 15:53:35 +0530	[thread overview]
Message-ID: <FB8C1154-6092-48C7-96B8-6B6D9A28D9AA@gmail.com> (raw)

Hi Git Developers,

While I was working on converting submodule code from shell to C, I had
a question about the submodule-config API.

The usual way of obtaining the submodule configuration is by using the
'submodule_from_path()' or 'submodule_from_name()' functions. Taking the
example of the former, a call to it would look something like:

const struct submodule *sub = submodule_from_path(repo, oid, path-to-submod);

So my first question is what is the exact purpose of the second
argument?

The API docs say it should be a tree-ish:

   Given a tree-ish in the superproject and a path, return the submodule that
   is bound at the path in the named tree.

My guess is that the tree-ish that we pass lets us obtain the submodule
configuration for that specific commit or tree in the superproject.

Looking at the underlying implementation[1], it seems to me that the 'oid'
argument treats the 'null_oid()' specially to mean "get the configuration from
the latest revision".

While I had these questions, Christian noticed a possible bug in the
implementation[2]:

---8<------8<------8<------8<---

> It seems to me that config_from() in submodule-config.c accepts a path or a
> tree oid and submodule_from_path() just calls config_from().
> 
> By the way it seems to me that config_from() is buggy when it's passed a
> tree oid as it does:
> 
> ```
>         if (!gitmodule_oid_from_commit(treeish_name, &oid, &rev))
>                 goto out;
> ```
> 
> but gitmodule_oid_from_commit() returns 0 on success so instead of doing a
> `goto out` which will error out, it should continue to proceed using the oid
> it got. So I think it should be something like:
> 
> ```
>         if (gitmodule_oid_from_commit(treeish_name, &oid, &rev)) {
>                 switch (lookup_type) {
>                 case lookup_name:
>                         submodule = cache_lookup_name(cache, &oid, key);
>                         break;
>                 case lookup_path:
>                         submodule = cache_lookup_path(cache, &oid, key);
>                         break;
>                 }
>                 if (submodule)
>                         goto out;
>         }
> ```
> 
> Or maybe the gitmodule_oid_from_commit() call should be moved after the switch ().

---8<------8<------8<------8<---

I do agree that it looks wrong, but despite that, it still seems to pass 't7411' [3][4].

This should have failed according to my understanding:

cat >super/expect <<EOF
Submodule name: 'a' for path 'a'
Submodule name: 'a' for path 'b'
Submodule name: 'submodule' for path 'submodule'
Submodule name: 'submodule' for path 'submodule'
EOF

test_expect_success 'test parsing and lookup of submodule config by path' '
	(cd super &&
		test-tool submodule-config \
			HEAD^ a \
			HEAD b \
			HEAD^ submodule \
			HEAD submodule \
				>actual &&
		test_cmp expect actual
	)
'

Since HEAD and HEAD^ are valid tree-ish objects, the gitmodule_oid_from_commit()
line should have returned early with a NULL object, but that does not happen.
The output seems to match what I would expect from the API.

Applying Christian's suggestions pass the same test as well.

I also wonder what is the situation with the case where, the oid is non-null
and invalid?

If we look at the code again:

static int gitmodule_oid_from_commit(const struct object_id *treeish_name,
				     struct object_id *gitmodules_oid,
				     struct strbuf *rev)
{
	int ret = 0;

	if (is_null_oid(treeish_name)) {
		oidclr(gitmodules_oid);
		return 1;
	}

	strbuf_addf(rev, "%s:.gitmodules", oid_to_hex(treeish_name));
	if (get_oid(rev->buf, gitmodules_oid) >= 0)
		ret = 1;

	return ret;
}

...what happens to the value of gitmodules_oid when get_oid() fails?

*If* it is set to NULL, it would probably lead to undefined behaviour when
we try to hash the oid for the submodule cache lookup in [1]. If it is set
to 'null_oid()' or zero'd out (like with oidclr()), then it should return
the submodule from the latest revision, which does not seem desirable, as
the input is invalid.

Is this a bug in my understanding or in the program? Or is it a bit of both?

Footnotes:
---------

[1]: https://github.com/git/git/blob/daab8a564f8bbac55f70f8bf86c070e001a9b006/submodule-config.c#L545-L615
[2]: https://github.com/tfidfwastaken/git/commit/7ad7a9d1d03653aadfdc87b60e3a152b1cb37f22#r53637226
[3]: https://github.com/git/git/blob/master/t/t7411-submodule-config.sh
[4]: https://github.com/git/git/blob/master/t/helper/test-submodule-config.c

---
Atharva Raykar
ಅಥರ್ವ ರಾಯ್ಕರ್
अथर्व रायकर


             reply	other threads:[~2021-07-22 10:23 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-07-22 10:23 Atharva Raykar [this message]
2021-07-22 10:59 ` [BUG?] submodule-config.c:config_from() has left me confused Ævar Arnfjörð Bjarmason

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=FB8C1154-6092-48C7-96B8-6B6D9A28D9AA@gmail.com \
    --to=raykar.ath@gmail.com \
    --cc=christian.couder@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=hvoigt@hvoigt.net \
    --cc=sandals@crustytoothpaste.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).