git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Jeff King <peff@peff.net>
To: Junio C Hamano <gitster@pobox.com>
Cc: Stefan Beller <sbeller@google.com>,
	git@vger.kernel.org, jrnieder@gmail.com, Jens.Lehmann@web.de
Subject: Re: [PATCH 1/8] submodule-config: keep update strategy around
Date: Wed, 3 Feb 2016 22:15:27 -0500	[thread overview]
Message-ID: <20160204031527.GB21105@sigill.intra.peff.net> (raw)
In-Reply-To: <xmqqy4b1l7cd.fsf@gitster.mtv.corp.google.com>

On Wed, Feb 03, 2016 at 03:09:06PM -0800, Junio C Hamano wrote:

> > +	} else if (!strcmp(item.buf, "update")) {
> > +		if (!value)
> > +			ret = config_error_nonbool(var);
> > +		else if (!me->overwrite && submodule->update != NULL)
> > +			warn_multiple_config(me->commit_sha1, submodule->name,
> > +					     "update");
> > +		else {
> > +			free((void *) submodule->update);
> > +			submodule->update = xstrdup(value);
> 
> This is a tangent, but whenever I see us needing to cast the
> constness away to call free(), it makes me wonder how much value we
> are getting by marking the field as a pointer to "const" in the
> first place.  With this patch alone, we cannot really see it,
> because we cannot see how it is used.

I suspect we may have just inherited this from other config code. We
typically use a const pointer to declare config strings, because
git_config_string() takes a pointer to a const pointer for its
destination. And we do that because some strings need BSS defaults, and
we cannot assign a string literal to a non-const pointer without a cast
(and if we did, it is inviting somebody to accidentally free() the
string literal).

So _somebody_ generally has to cast to cover all situations. But here we
are not using git_config_string() at all, so I think we could get away
with a non-const pointer.

As a tangent to your tangent, another problem with git_config_string()
and const pointers is that we never free the "old" value for a string,
and we leak it.  It's usually OK in practice because people do not have
unbounded repetition of their config keys. We don't free because it
would be an error to do so for a default-assigned string literal. But
for any string variable which defaults to NULL, it would be fine.

IOW, git_config_string (and probably git_config_pathname) could be
implemented like this:

  int git_config_string(char **dest, const char *var, const char *value)
  {
	if (!value)
		return config_error_nonbool(var);
	free(*dest);
	*dest = xstrdup(value);
	return 0;
  }

and then:

  char *git_foo;
  ...
      return git_config_string(&git_foo, var, value);

would do the right thing. But I'm not sure how much that would ripple
through the code-base. Any string who wants:

  const char *git_foo = "default";

would have to use another function (and keep the leak), or convert its
uses to treat NULL the same as "default". I have a suspicion that _most_
sites would prefer it the non-const way, and it would be a net win. But
I think you'd have to replace the function, then convert all of the
variables used (which the compiler will helpfully point out to you
because of the type mismatch), and then see how many are broken (which
the compiler will also tell you).

-Peff

  reply	other threads:[~2016-02-04  3:15 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-02-02 17:51 [PATCHv7 0/8] Expose submodule parallelism to the user Stefan Beller
2016-02-02 17:51 ` [PATCH 1/8] submodule-config: keep update strategy around Stefan Beller
2016-02-03 23:09   ` Junio C Hamano
2016-02-04  3:15     ` Jeff King [this message]
2016-02-02 17:51 ` [PATCH 2/8] submodule-config: drop check against NULL Stefan Beller
2016-02-03 23:09   ` Junio C Hamano
2016-02-02 17:51 ` [PATCH 3/8] submodule-config: remove name_and_item_from_var Stefan Beller
2016-02-03 23:09   ` Junio C Hamano
2016-02-02 17:51 ` [PATCH 4/8] submodule-config: introduce parse_generic_submodule_config Stefan Beller
2016-02-03 23:09   ` Junio C Hamano
2016-02-03 23:26     ` Stefan Beller
2016-02-04  0:51       ` Junio C Hamano
2016-02-02 17:51 ` [PATCH 5/8] fetching submodules: respect `submodule.fetchJobs` config option Stefan Beller
2016-02-03 23:09   ` Junio C Hamano
2016-02-02 17:51 ` [PATCH 6/8] git submodule update: have a dedicated helper for cloning Stefan Beller
2016-02-03 23:24   ` Junio C Hamano
2016-02-03 23:41     ` Stefan Beller
2016-02-04  0:54       ` Junio C Hamano
2016-02-04 20:22         ` Stefan Beller
2016-02-04 21:57           ` Junio C Hamano
2016-02-02 17:51 ` [PATCH 7/8] submodule update: expose parallelism to the user Stefan Beller
2016-02-02 17:51 ` [PATCH 8/8] clone: allow an explicit argument for parallel submodule clones Stefan Beller
  -- strict thread matches above, loose matches on Subject: below --
2015-12-14 22:54 [PATCHv6 0/8] Expose submodule parallelism to the user Stefan Beller
2015-12-14 22:54 ` [PATCH 1/8] submodule-config: keep update strategy around Stefan Beller

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=20160204031527.GB21105@sigill.intra.peff.net \
    --to=peff@peff.net \
    --cc=Jens.Lehmann@web.de \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=jrnieder@gmail.com \
    --cc=sbeller@google.com \
    /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).