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

Re: [PATCH] config.c: change the function signature of `git_config_string()`

From
Jeff King <peff@peff.net>
Date
Jul 22, 2014, 11:07 UTC
Message-ID
<20140722110720.GA386@peff.net>
In-Reply-To
<1406026196-17877-1-git-send-email-tanayabh@gmail.com>
On Tue, Jul 22, 2014 at 03:49:56AM -0700, Tanay Abhra wrote:
Show 5 quoted lines
> `git_config_string()` output parameter `dest` is declared as a const
> which is unnecessary as the caller of the function is given a strduped
> string which can be modified without causing any harm.
> 
> Thus, remove the const from the function signature.

You are correct that it is unnecessary. However, this patch alone is not sufficient because of the way const-ness in C works. If I have:

  static const char *some_global;
then with your patch, calling:
  git_config_string(&some_global, var, value);

will complain that we are passing a pointer to "const char *", not a pointer to "char *". And indeed, compiling with your patch introduces a ton of compiler warnings.

We would have to convert each of the variables we pass to it to:
  static char *some_global;
That's not so bad, but:
  static char *some_global = "some_default_value";

is wrong. Such a global sometimes points to const storage (i.e., initially), and sometimes to allocated storage (if it was loaded from config). We simply keep the latter as a const pointer (since we would not bother to free it at the end of the program anyway), and that decision influences git_config_string, which is just a helper for setting such variables anyway.

So I would not mind lifting this unnecessary restriction on git_config_string, but I do not see a way to do it without making the rest of the code much uglier (and I do not see a particular advantage in modifying git_config_string here that would make it worth the trouble).

-Peff
Previous: Tanay AbhraNext: Tanay Abhra
Message 2 of 10 in “config.c: change the function signature of `git_config_string()`”
  1. config.c: change the function signature of `git_config_string()`Tanay Abhra, Jul 22, 2014
  2. Jeff KingJul 22, 2014
  3. Tanay AbhraJul 22, 2014
  4. Matthieu MoyJul 22, 2014
  5. Tanay AbhraJul 22, 2014
  6. Junio C HamanoJul 22, 2014
  7. Matthieu MoyJul 22, 2014
  8. Junio C HamanoJul 22, 2014
  9. Matthieu MoyJul 23, 2014
  10. Junio C HamanoJul 23, 2014

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.