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

Re: [PATCH v15 13/27] bisect--helper: `bisect_start` shell function partially in C

From
Stephan Beyer <s-beyer@gmx.net>
Date
Nov 20, 2016, 20:01 UTC
Message-ID
<52f6241f-e584-d830-ca66-084dc509c7fe@gmx.net>
In-Reply-To
<01020157c38b1ad3-ea75ed97-2514-427e-8e57-9f10efd4e6e9-000000@eu-west-1.amazonses.com>
Hi,
On 10/14/2016 04:14 PM, Pranit Bauva wrote:
Show 45 quoted lines
> diff --git a/builtin/bisect--helper.c b/builtin/bisect--helper.c
> index 6a5878c..1d3e17f 100644
> --- a/builtin/bisect--helper.c
> +++ b/builtin/bisect--helper.c
> @@ -403,6 +408,205 @@ static int bisect_terms(struct bisect_terms *terms, const char **argv, int argc)
>  	return 0;
>  }
>  
> +static int bisect_start(struct bisect_terms *terms, int no_checkout,
> +			const char **argv, int argc)
> +{
> +	int i, has_double_dash = 0, must_write_terms = 0, bad_seen = 0;
> +	int flags, pathspec_pos, retval = 0;
> +	struct string_list revs = STRING_LIST_INIT_DUP;
> +	struct string_list states = STRING_LIST_INIT_DUP;
> +	struct strbuf start_head = STRBUF_INIT;
> +	struct strbuf bisect_names = STRBUF_INIT;
> +	struct strbuf orig_args = STRBUF_INIT;
> +	const char *head;
> +	unsigned char sha1[20];
> +	FILE *fp = NULL;
> +	struct object_id oid;
> +
> +	if (is_bare_repository())
> +		no_checkout = 1;
> +
> +	for (i = 0; i < argc; i++) {
> +		if (!strcmp(argv[i], "--")) {
> +			has_double_dash = 1;
> +			break;
> +		}
> +	}
> +
> +	for (i = 0; i < argc; i++) {
> +		const char *commit_id = xstrfmt("%s^{commit}", argv[i]);
> +		const char *arg = argv[i];
> +		if (!strcmp(argv[i], "--")) {
> +			has_double_dash = 1;
> +			break;
> +		} else if (!strcmp(arg, "--no-checkout")) {
> +			no_checkout = 1;
> +		} else if (!strcmp(arg, "--term-good") ||
> +			 !strcmp(arg, "--term-old")) {
> +			must_write_terms = 1;
> +			terms->term_good = xstrdup(argv[++i]);
All these xstrdup() for the terms here and below will leak memory.

I recommend to use xstrdup() also at (*) below, and use free(terms->term_good) above this line (and for every occurrence below, of course).

> +		} else if (skip_prefix(arg, "--term-good=", &arg)) {
> +			must_write_terms = 1;
> +			terms->term_good = xstrdup(arg);
[...]
Show 7 quoted lines
> @@ -497,6 +705,11 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
>  			die(_("--bisect-terms requires 0 or 1 argument"));
>  		res = bisect_terms(&terms, argv, argc);
>  		break;
> +	case BISECT_START:
> +		terms.term_good = "good";
> +		terms.term_bad = "bad";
Here is (*): use xstrdup("good") etc.
And then, as already mentioned for another patch, free(terms.*) below.

I personally am a friend of small functions and would prefer something like as follows... (This is a comment about several patches of your series, not only this one.)

First, replace the current set_terms() by
static void set_terms(struct bisect_terms *terms, const char *bad,
                                                 const char *good)
{
	terms->term_good = xstrdup(good);
	terms->term_bad = xstrdup(bad);
}

ie, without calling write_terms(...). And then replace the *current* set_terms() calls by set_terms(...); write_terms(...); calls.

Second, add
static void get_default_terms(struct bisect_terms *terms)
{
	set_terms(terms, "bad", "good");
}

and use this instead of the two lines quoted above (and all its other occurrences).

Third, use the new set_terms() everywhere instead of settings terms members directly (with the exception of get_terms()).

This sounds like a safer variant (with respect to leaks and handling them) to me than doing it the current way.

~Stephan
Previous: Pranit BauvaNext: Stephan Beyer
Message 33 of 82 in “bisect--helper: use OPT_CMDMODE instead of OPT_BOOL”
  1. 01/27 bisect--helper: use OPT_CMDMODE instead of OPT_BOOLPranit Bauva, Oct 14, 2016
  2. 06/27 wrapper: move is_empty_file() and rename it as is_empty_or_missing_file()Pranit Bauva, Oct 14, 2016
  3. 05/27 t6030: explicitly test for bisection cleanupPranit Bauva, Oct 14, 2016
  4. 22/27 bisect--helper: `bisect_log` shell function in CPranit Bauva, Oct 14, 2016
  5. Stephan BeyerNov 17, 2016
  6. Pranit BauvaDec 6, 2016
  7. 10/27 bisect--helper: `check_and_set_terms` shell function in CPranit Bauva, Oct 14, 2016
  8. Stephan BeyerNov 17, 2016
  9. Pranit BauvaDec 6, 2016
  10. 23/27 bisect--helper: `bisect_replay` shell function in CPranit Bauva, Oct 14, 2016
  11. Stephan BeyerNov 22, 2016
  12. Pranit BauvaDec 6, 2016
  13. Stephan BeyerDec 6, 2016
  14. Stephan BeyerDec 6, 2016
  15. Christian CouderDec 7, 2016
  16. 04/27 bisect--helper: `bisect_clean_state` shell function in CPranit Bauva, Oct 14, 2016
  17. Stephan BeyerNov 15, 2016
  18. Junio C HamanoNov 15, 2016
  19. Stephan BeyerNov 15, 2016
  20. Pranit BauvaNov 16, 2016
  21. 09/27 bisect--helper: `bisect_write` shell function in CPranit Bauva, Oct 14, 2016
  22. Stephan BeyerNov 17, 2016
  23. Pranit BauvaDec 6, 2016
  24. 20/27 bisect--helper: retire `--check-expected-revs` subcommandPranit Bauva, Oct 14, 2016
  25. 12/27 bisect--helper: `get_terms` & `bisect_terms` shell function in CPranit Bauva, Oct 14, 2016
  26. Stephan BeyerNov 17, 2016
  27. Pranit BauvaDec 6, 2016
  28. Stephan BeyerDec 6, 2016
  29. Pranit BauvaDec 7, 2016
  30. 13/27 bisect--helper: `bisect_start` shell function partially in CPranit Bauva, Oct 14, 2016
  31. Stephan BeyerNov 15, 2016
  32. Pranit BauvaNov 16, 2016
  33. Stephan BeyerNov 20, 2016
  34. Stephan BeyerNov 20, 2016
  35. 18/27 bisect--helper: `bisect_autostart` shell function in CPranit Bauva, Oct 14, 2016
  36. Stephan BeyerNov 20, 2016
  37. Pranit BauvaDec 6, 2016
  38. 21/27 bisect--helper: retire `--write-terms` subcommandPranit Bauva, Oct 14, 2016
  39. 19/27 bisect--helper: `bisect_state` & `bisect_head` shell function in CPranit Bauva, Oct 14, 2016
  40. Stephan BeyerNov 22, 2016
  41. Pranit BauvaDec 6, 2016
  42. Stephan BeyerDec 6, 2016
  43. Pranit BauvaDec 8, 2016
  44. 14/27 t6030: no cleanup with bad merge basePranit Bauva, Oct 14, 2016
  45. Junio C HamanoOct 14, 2016
  46. Pranit BauvaOct 15, 2016
  47. 15/27 bisect--helper: `bisect_next` and `bisect_auto_next` shell function in CPranit Bauva, Oct 14, 2016
  48. Stephan BeyerNov 20, 2016
  49. Pranit BauvaDec 31, 2016
  50. Stephan BeyerNov 21, 2016
  51. Pranit BauvaDec 31, 2016
  52. Stephan BeyerJan 1, 2017
  53. Pranit BauvaJan 1, 2017
  54. 07/27 bisect--helper: `bisect_reset` shell function in CPranit Bauva, Oct 14, 2016
  55. Stephan BeyerNov 16, 2016
  56. Pranit BauvaNov 17, 2016
  57. 17/27 bisect--helper: retire `--next-all` subcommandPranit Bauva, Oct 14, 2016
  58. 24/27 bisect--helper: retire `--bisect-write` subcommandPranit Bauva, Oct 14, 2016
  59. 03/27 bisect--helper: `write_terms` shell function in CPranit Bauva, Oct 14, 2016
  60. 02/27 bisect: rewrite `check_term_format` shell function in CPranit Bauva, Oct 14, 2016
  61. Stephan BeyerNov 14, 2016
  62. Pranit BauvaNov 15, 2016
  63. 26/27 bisect--helper: retire `--bisect-auto-next` subcommandPranit Bauva, Oct 14, 2016
  64. 16/27 bisect--helper: retire `--bisect-clean-state` subcommandPranit Bauva, Oct 14, 2016
  65. 08/27 bisect--helper: `is_expected_rev` & `check_expected_revs` shell function in CPranit Bauva, Oct 14, 2016
  66. Stephan BeyerNov 16, 2016
  67. Pranit BauvaDec 6, 2016
  68. Pranit BauvaDec 16, 2016
  69. Stephan BeyerDec 17, 2016
  70. Pranit BauvaDec 16, 2016
  71. Stephan BeyerDec 17, 2016
  72. 25/27 bisect--helper: retire `--bisect-autostart` subcommandPranit Bauva, Oct 14, 2016
  73. Pranit BauvaOct 14, 2016
  74. 11/27 bisect--helper: `bisect_next_check` & bisect_voc shell function in CPranit Bauva, Oct 14, 2016
  75. Stephan BeyerNov 17, 2016
  76. Pranit BauvaDec 6, 2016
  77. 27/27 bisect--helper: remove the dequote in bisect_start()Pranit Bauva, Oct 14, 2016
  78. Junio C HamanoOct 27, 2016
  79. Christian CouderOct 27, 2016
  80. Matthieu MoyOct 28, 2016
  81. Stephan BeyerNov 15, 2016
  82. Junio C HamanoNov 16, 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.