{"thread":{"id":"58876","subject":"[PATCH 0/3] diff: build parseopts array on demand","startedAt":"2022-11-30T18:01:41Z","lastAt":"2022-12-01T23:04:23Z","messageCount":23,"participants":["René Scharfe","Junio C Hamano","ZheNing Hu","Ævar Arnfjörð Bjarmason","Eric Sunshine"],"isPatch":true,"patchVersion":1,"patchTotal":3},"messages":[{"id":"468260","messageId":"0620b4a6-b80a-d8a0-5a35-857b9fe0c986@web.de","threadId":"58876","inReplyTo":null,"subject":"[PATCH 0/3] diff: build parseopts array on demand","fromName":"René Scharfe","fromEmail":"l.s.r@web.de","sentAt":"2022-11-30T18:01:22Z","receivedAt":"2022-11-30T18:01:41Z","isPatch":true,"sender":{"key":"l.s.r@web.de","avatar":"https://avatars.githubusercontent.com/u/26122331?v=4"},"body":"Calling repo_init_revisions() and release_revisions() in that order\nleaks the memory allocated for the parseopts array in the embedded\nstruct diff_options member.  Get rid of that leak by reducing the\nlifetime of that array.\n\nOriginal patch:\nhttps://lore.kernel.org/git/4fd82dc6-e0f8-0638-5b10-16bfef39a171@web.de/\n\nSubmitted separately from that thread because it's independent enough.\n\n  diff: factor out add_diff_options()\n  diff: let prep_parse_options() return parseopt array\n  diff: remove parseopts member of struct diff_options\n\n builtin/range-diff.c |  2 +-\n diff-no-index.c      |  3 +--\n diff.c               | 26 +++++++++++++++-----------\n diff.h               |  1 +\n 4 files changed, 18 insertions(+), 14 deletions(-)\n\n--\n2.38.1\n"},{"id":"468261","messageId":"19be5afa-63e8-5f11-c5f4-6f391677e4c4@web.de","threadId":"58876","inReplyTo":"0620b4a6-b80a-d8a0-5a35-857b9fe0c986@web.de","subject":"[PATCH 1/3] diff: factor out add_diff_options()","fromName":"René Scharfe","fromEmail":"l.s.r@web.de","sentAt":"2022-11-30T18:03:26Z","receivedAt":"2022-11-30T18:03:43Z","isPatch":true,"sender":{"key":"l.s.r@web.de","avatar":"https://avatars.githubusercontent.com/u/26122331?v=4"},"body":"Add a function for appending the parseopts member of struct diff_options\nto a struct option array.  Use it in two sites instead of accessing the\nparseopts member directly.  Decoupling callers from diff internals like\nthat allows us to change the latter.\n\nSigned-off-by: René Scharfe <l.s.r@web.de>\n---\n builtin/range-diff.c | 2 +-\n diff-no-index.c      | 3 +--\n diff.c               | 6 ++++++\n diff.h               | 1 +\n 4 files changed, 9 insertions(+), 3 deletions(-)\n\ndiff --git a/builtin/range-diff.c b/builtin/range-diff.c\nindex e2a74efb42..aecfae12d3 100644\n--- a/builtin/range-diff.c\n+++ b/builtin/range-diff.c\n@@ -47,7 +47,7 @@ int cmd_range_diff(int argc, const char **argv, const char *prefix)\n\n \trepo_diff_setup(the_repository, &diffopt);\n\n-\toptions = parse_options_concat(range_diff_options, diffopt.parseopts);\n+\toptions = add_diff_options(range_diff_options, &diffopt);\n \targc = parse_options(argc, argv, prefix, options,\n \t\t\t     builtin_range_diff_usage, PARSE_OPT_KEEP_DASHDASH);\n\ndiff --git a/diff-no-index.c b/diff-no-index.c\nindex 18edbdf4b5..05fafd0019 100644\n--- a/diff-no-index.c\n+++ b/diff-no-index.c\n@@ -255,8 +255,7 @@ int diff_no_index(struct rev_info *revs,\n \t};\n \tstruct option *options;\n\n-\toptions = parse_options_concat(no_index_options,\n-\t\t\t\t       revs->diffopt.parseopts);\n+\toptions = add_diff_options(no_index_options, &revs->diffopt);\n \targc = parse_options(argc, argv, revs->prefix, options,\n \t\t\t     diff_no_index_usage, 0);\n \tif (argc != 2) {\ndiff --git a/diff.c b/diff.c\nindex 1054a4b732..e01129f0ea 100644\n--- a/diff.c\n+++ b/diff.c\n@@ -5693,6 +5693,12 @@ static void prep_parse_options(struct diff_options *options)\n \tmemcpy(options->parseopts, parseopts, sizeof(parseopts));\n }\n\n+struct option *add_diff_options(const struct option *parseopts,\n+\t\t\t\tstruct diff_options *options)\n+{\n+\treturn parse_options_concat(parseopts, options->parseopts);\n+}\n+\n int diff_opt_parse(struct diff_options *options,\n \t\t   const char **av, int ac, const char *prefix)\n {\ndiff --git a/diff.h b/diff.h\nindex fd33caeb25..c20a1ad76d 100644\n--- a/diff.h\n+++ b/diff.h\n@@ -539,6 +539,7 @@ int git_diff_ui_config(const char *var, const char *value, void *cb);\n #define diff_setup(diffopts) repo_diff_setup(the_repository, diffopts)\n #endif\n void repo_diff_setup(struct repository *, struct diff_options *);\n+struct option *add_diff_options(const struct option *, struct diff_options *);\n int diff_opt_parse(struct diff_options *, const char **, int, const char *);\n void diff_setup_done(struct diff_options *);\n int git_config_rename(const char *var, const char *value);\n--\n2.38.1\n"},{"id":"468263","messageId":"b5f611f5-86e2-6b11-20a4-a847c97b145f@web.de","threadId":"58876","inReplyTo":"0620b4a6-b80a-d8a0-5a35-857b9fe0c986@web.de","subject":"[PATCH 2/3] diff: let prep_parse_options() return parseopt array","fromName":"René Scharfe","fromEmail":"l.s.r@web.de","sentAt":"2022-11-30T18:04:08Z","receivedAt":"2022-11-30T18:04:25Z","isPatch":true,"sender":{"key":"l.s.r@web.de","avatar":"https://avatars.githubusercontent.com/u/26122331?v=4"},"body":"prep_parse_options() sets the parseopts member of struct diff_options.\nLet it return the pointer instead and have its caller do the assignment\ninstead and rename the function to get_diff_parseopts() to reflect that\nchange.  This allows using it in other places and with other variables.\n\nSigned-off-by: René Scharfe <l.s.r@web.de>\n---\n diff.c | 9 ++++-----\n 1 file changed, 4 insertions(+), 5 deletions(-)\n\ndiff --git a/diff.c b/diff.c\nindex e01129f0ea..e469d5d2a0 100644\n--- a/diff.c\n+++ b/diff.c\n@@ -4615,7 +4615,7 @@ static void run_checkdiff(struct diff_filepair *p, struct diff_options *o)\n \tbuiltin_checkdiff(name, other, attr_path, p->one, p->two, o);\n }\n\n-static void prep_parse_options(struct diff_options *options);\n+static struct option *get_diff_parseopts(struct diff_options *options);\n\n void repo_diff_setup(struct repository *r, struct diff_options *options)\n {\n@@ -4663,7 +4663,7 @@ void repo_diff_setup(struct repository *r, struct diff_options *options)\n \toptions->color_moved = diff_color_moved_default;\n \toptions->color_moved_ws_handling = diff_color_moved_ws_default;\n\n-\tprep_parse_options(options);\n+\toptions->parseopts = get_diff_parseopts(options);\n }\n\n static const char diff_status_letters[] = {\n@@ -5419,7 +5419,7 @@ static int diff_opt_rotate_to(const struct option *opt, const char *arg, int uns\n \treturn 0;\n }\n\n-static void prep_parse_options(struct diff_options *options)\n+static struct option *get_diff_parseopts(struct diff_options *options)\n {\n \tstruct option parseopts[] = {\n \t\tOPT_GROUP(N_(\"Diff output format options\")),\n@@ -5689,8 +5689,7 @@ static void prep_parse_options(struct diff_options *options)\n \t\tOPT_END()\n \t};\n\n-\tALLOC_ARRAY(options->parseopts, ARRAY_SIZE(parseopts));\n-\tmemcpy(options->parseopts, parseopts, sizeof(parseopts));\n+\treturn parse_options_dup(parseopts);\n }\n\n struct option *add_diff_options(const struct option *parseopts,\n--\n2.38.1\n"},{"id":"468264","messageId":"d226d3bc-fb15-58a4-f516-bda51a912228@web.de","threadId":"58876","inReplyTo":"0620b4a6-b80a-d8a0-5a35-857b9fe0c986@web.de","subject":"[PATCH 3/3] diff: remove parseopts member of struct diff_options","fromName":"René Scharfe","fromEmail":"l.s.r@web.de","sentAt":"2022-11-30T18:04:53Z","receivedAt":"2022-11-30T18:05:09Z","isPatch":true,"sender":{"key":"l.s.r@web.de","avatar":"https://avatars.githubusercontent.com/u/26122331?v=4"},"body":"repo_diff_setup() builds the struct option array with git diff's command\nline options and stores a pointer to it in the parseopts member of\nstruct diff_options.  The array is freed by diff_setup_done(), but not\nby release_revisions().  repo_init_revisions() calls repo_diff_setup(),\nthus calling repo_init_revisions() then release_revisions() leaks it.\n\nWe could free the array in release_revisions() as well to plug that\nleak, but there is a better way: Only build it when needed.  Move the\nget_diff_parseopts() calls to the two places that use the array, free it\nafter use and get rid of the parseopts member.\n\nSigned-off-by: René Scharfe <l.s.r@web.de>\n---\n diff.c | 17 ++++++++---------\n 1 file changed, 8 insertions(+), 9 deletions(-)\n\ndiff --git a/diff.c b/diff.c\nindex e469d5d2a0..6415c4dc2d 100644\n--- a/diff.c\n+++ b/diff.c\n@@ -4615,8 +4615,6 @@ static void run_checkdiff(struct diff_filepair *p, struct diff_options *o)\n \tbuiltin_checkdiff(name, other, attr_path, p->one, p->two, o);\n }\n\n-static struct option *get_diff_parseopts(struct diff_options *options);\n-\n void repo_diff_setup(struct repository *r, struct diff_options *options)\n {\n \tmemcpy(options, &default_diff_options, sizeof(*options));\n@@ -4662,8 +4660,6 @@ void repo_diff_setup(struct repository *r, struct diff_options *options)\n\n \toptions->color_moved = diff_color_moved_default;\n \toptions->color_moved_ws_handling = diff_color_moved_ws_default;\n-\n-\toptions->parseopts = get_diff_parseopts(options);\n }\n\n static const char diff_status_letters[] = {\n@@ -4821,8 +4817,6 @@ void diff_setup_done(struct diff_options *options)\n \t\t\toptions->filter = ~filter_bit[DIFF_STATUS_FILTER_AON];\n \t\toptions->filter &= ~options->filter_not;\n \t}\n-\n-\tFREE_AND_NULL(options->parseopts);\n }\n\n int parse_long_opt(const char *opt, const char **argv,\n@@ -5695,21 +5689,27 @@ static struct option *get_diff_parseopts(struct diff_options *options)\n struct option *add_diff_options(const struct option *parseopts,\n \t\t\t\tstruct diff_options *options)\n {\n-\treturn parse_options_concat(parseopts, options->parseopts);\n+\tstruct option *diff_parseopts = get_diff_parseopts(options);\n+\tstruct option *result = parse_options_concat(parseopts, diff_parseopts);\n+\tfree(diff_parseopts);\n+\treturn result;\n }\n\n int diff_opt_parse(struct diff_options *options,\n \t\t   const char **av, int ac, const char *prefix)\n {\n+\tstruct option *diff_parseopts = get_diff_parseopts(options);\n+\n \tif (!prefix)\n \t\tprefix = \"\";\n\n-\tac = parse_options(ac, av, prefix, options->parseopts, NULL,\n+\tac = parse_options(ac, av, prefix, diff_parseopts, NULL,\n \t\t\t   PARSE_OPT_KEEP_DASHDASH |\n \t\t\t   PARSE_OPT_KEEP_UNKNOWN_OPT |\n \t\t\t   PARSE_OPT_NO_INTERNAL_HELP |\n \t\t\t   PARSE_OPT_ONE_SHOT |\n \t\t\t   PARSE_OPT_STOP_AT_NON_OPTION);\n+\tfree(diff_parseopts);\n\n \treturn ac;\n }\n@@ -6518,7 +6518,6 @@ void diff_free(struct diff_options *options)\n \tdiff_free_file(options);\n \tdiff_free_ignore_regex(options);\n \tclear_pathspec(&options->pathspec);\n-\tFREE_AND_NULL(options->parseopts);\n }\n\n void diff_flush(struct diff_options *options)\n--\n2.38.1\n"},{"id":"468298","messageId":"xmqqh6ygszpm.fsf@gitster.g","threadId":"58876","inReplyTo":"0620b4a6-b80a-d8a0-5a35-857b9fe0c986@web.de","subject":"Re: [PATCH 0/3] diff: build parseopts array on demand","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2022-12-01T01:02:45Z","receivedAt":"2022-12-01T01:02:53Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"René Scharfe <l.s.r@web.de> writes:\n\n> Calling repo_init_revisions() and release_revisions() in that order\n> leaks the memory allocated for the parseopts array in the embedded\n> struct diff_options member.  Get rid of that leak by reducing the\n> lifetime of that array.\n\nMakes sense.\n"},{"id":"468299","messageId":"xmqqcz93ud8o.fsf@gitster.g","threadId":"58876","inReplyTo":"d226d3bc-fb15-58a4-f516-bda51a912228@web.de","subject":"Re: [PATCH 3/3] diff: remove parseopts member of struct diff_options","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2022-12-01T01:25:11Z","receivedAt":"2022-12-01T01:25:16Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"René Scharfe <l.s.r@web.de> writes:\n\n> repo_diff_setup() builds the struct option array with git diff's command\n> line options and stores a pointer to it in the parseopts member of\n> struct diff_options.  The array is freed by diff_setup_done(), but not\n> by release_revisions().  repo_init_revisions() calls repo_diff_setup(),\n> thus calling repo_init_revisions() then release_revisions() leaks it.\n>\n> We could free the array in release_revisions() as well to plug that\n> leak, but there is a better way: Only build it when needed.  Move the\n> get_diff_parseopts() calls to the two places that use the array, free it\n> after use and get rid of the parseopts member.\n>\n> Signed-off-by: René Scharfe <l.s.r@web.de>\n> ---\n>  diff.c | 17 ++++++++---------\n>  1 file changed, 8 insertions(+), 9 deletions(-)\n\nI think this hunk is missing?\n\n diff.h | 1 -\n 1 file changed, 1 deletion(-)\n\ndiff --git c/diff.h w/diff.h\nindex 5229f20486..6840499844 100644\n--- c/diff.h\n+++ w/diff.h\n@@ -394,7 +394,6 @@ struct diff_options {\n \tunsigned color_moved_ws_handling;\n \n \tstruct repository *repo;\n-\tstruct option *parseopts;\n \tstruct strmap *additional_path_headers;\n \n \tint no_free;\n"},{"id":"468306","messageId":"20b5d6d1-ec09-e09f-4f92-5925fb9c96f8@web.de","threadId":"58876","inReplyTo":"xmqqcz93ud8o.fsf@gitster.g","subject":"Re: [PATCH 3/3] diff: remove parseopts member of struct diff_options","fromName":"René Scharfe","fromEmail":"l.s.r@web.de","sentAt":"2022-12-01T07:52:37Z","receivedAt":"2022-12-01T07:52:57Z","isPatch":true,"sender":{"key":"l.s.r@web.de","avatar":"https://avatars.githubusercontent.com/u/26122331?v=4"},"body":"Am 01.12.22 um 02:25 schrieb Junio C Hamano:\n> René Scharfe <l.s.r@web.de> writes:\n>\n>> repo_diff_setup() builds the struct option array with git diff's command\n>> line options and stores a pointer to it in the parseopts member of\n>> struct diff_options.  The array is freed by diff_setup_done(), but not\n>> by release_revisions().  repo_init_revisions() calls repo_diff_setup(),\n>> thus calling repo_init_revisions() then release_revisions() leaks it.\n>>\n>> We could free the array in release_revisions() as well to plug that\n>> leak, but there is a better way: Only build it when needed.  Move the\n>> get_diff_parseopts() calls to the two places that use the array, free it\n>> after use and get rid of the parseopts member.\n>>\n>> Signed-off-by: René Scharfe <l.s.r@web.de>\n>> ---\n>>  diff.c | 17 ++++++++---------\n>>  1 file changed, 8 insertions(+), 9 deletions(-)\n>\n> I think this hunk is missing?\n\nYes. O_o\n\n>\n>  diff.h | 1 -\n>  1 file changed, 1 deletion(-)\n>\n> diff --git c/diff.h w/diff.h\n> index 5229f20486..6840499844 100644\n> --- c/diff.h\n> +++ w/diff.h\n> @@ -394,7 +394,6 @@ struct diff_options {\n>  \tunsigned color_moved_ws_handling;\n>\n>  \tstruct repository *repo;\n> -\tstruct option *parseopts;\n>  \tstruct strmap *additional_path_headers;\n>\n>  \tint no_free;\n"},{"id":"468314","messageId":"ea838ae7-b635-d4d2-d9df-e96b3d8980af@web.de","threadId":"58876","inReplyTo":"0620b4a6-b80a-d8a0-5a35-857b9fe0c986@web.de","subject":"[PATCH v2 0/3] diff: build parseopts array on demand","fromName":"René Scharfe","fromEmail":"l.s.r@web.de","sentAt":"2022-12-01T13:39:54Z","receivedAt":"2022-12-01T13:40:27Z","isPatch":true,"sender":{"key":"l.s.r@web.de","avatar":"https://avatars.githubusercontent.com/u/26122331?v=4"},"body":"Calling repo_init_revisions() and release_revisions() in that order\nleaks the memory allocated for the parseopts array in the embedded\nstruct diff_options member.  Get rid of that leak by reducing the\nlifetime of that array.\n\nOriginal patch:\nhttps://lore.kernel.org/git/4fd82dc6-e0f8-0638-5b10-16bfef39a171@web.de/\n\nSubmitted separately from that thread because it's independent enough.\n\nChange since v1:\n- Actually remove the parseopts member.  Its removal got lost during\n  refactoring in v1.  Thank you for spotting that, Junio!\n\n  diff: factor out add_diff_options()\n  diff: let prep_parse_options() return parseopt array\n  diff: remove parseopts member from struct diff_options\n\n builtin/range-diff.c |  2 +-\n diff-no-index.c      |  3 +--\n diff.c               | 26 +++++++++++++++-----------\n diff.h               |  2 +-\n 4 files changed, 18 insertions(+), 15 deletions(-)\n\nRange-Diff gegen v1:\n1:  630f95320f = 1:  4dc8b2632b diff: factor out add_diff_options()\n2:  4b56fa795c = 2:  10903d355e diff: let prep_parse_options() return parseopt array\n3:  7e54e4370a ! 3:  24bd18ae79 diff: remove parseopts member from struct diff_options\n    @@ diff.c: void diff_free(struct diff_options *options)\n      }\n\n      void diff_flush(struct diff_options *options)\n    +\n    + ## diff.h ##\n    +@@ diff.h: struct diff_options {\n    + \tunsigned color_moved_ws_handling;\n    +\n    + \tstruct repository *repo;\n    +-\tstruct option *parseopts;\n    + \tstruct strmap *additional_path_headers;\n    +\n    + \tint no_free;\n--\n2.38.1\n"},{"id":"468315","messageId":"ed84a688-e1fd-945d-7dfc-10d051652c77@web.de","threadId":"58876","inReplyTo":"ea838ae7-b635-d4d2-d9df-e96b3d8980af@web.de","subject":"[PATCH v2 1/3] diff: factor out add_diff_options()","fromName":"René Scharfe","fromEmail":"l.s.r@web.de","sentAt":"2022-12-01T13:42:05Z","receivedAt":"2022-12-01T13:42:21Z","isPatch":true,"sender":{"key":"l.s.r@web.de","avatar":"https://avatars.githubusercontent.com/u/26122331?v=4"},"body":"Add a function for appending the parseopts member of struct diff_options\nto a struct option array.  Use it in two sites instead of accessing the\nparseopts member directly.  Decoupling callers from diff internals like\nthat allows us to change the latter.\n\nSigned-off-by: René Scharfe <l.s.r@web.de>\n---\n builtin/range-diff.c | 2 +-\n diff-no-index.c      | 3 +--\n diff.c               | 6 ++++++\n diff.h               | 1 +\n 4 files changed, 9 insertions(+), 3 deletions(-)\n\ndiff --git a/builtin/range-diff.c b/builtin/range-diff.c\nindex e2a74efb42..aecfae12d3 100644\n--- a/builtin/range-diff.c\n+++ b/builtin/range-diff.c\n@@ -47,7 +47,7 @@ int cmd_range_diff(int argc, const char **argv, const char *prefix)\n\n \trepo_diff_setup(the_repository, &diffopt);\n\n-\toptions = parse_options_concat(range_diff_options, diffopt.parseopts);\n+\toptions = add_diff_options(range_diff_options, &diffopt);\n \targc = parse_options(argc, argv, prefix, options,\n \t\t\t     builtin_range_diff_usage, PARSE_OPT_KEEP_DASHDASH);\n\ndiff --git a/diff-no-index.c b/diff-no-index.c\nindex 18edbdf4b5..05fafd0019 100644\n--- a/diff-no-index.c\n+++ b/diff-no-index.c\n@@ -255,8 +255,7 @@ int diff_no_index(struct rev_info *revs,\n \t};\n \tstruct option *options;\n\n-\toptions = parse_options_concat(no_index_options,\n-\t\t\t\t       revs->diffopt.parseopts);\n+\toptions = add_diff_options(no_index_options, &revs->diffopt);\n \targc = parse_options(argc, argv, revs->prefix, options,\n \t\t\t     diff_no_index_usage, 0);\n \tif (argc != 2) {\ndiff --git a/diff.c b/diff.c\nindex 1054a4b732..e01129f0ea 100644\n--- a/diff.c\n+++ b/diff.c\n@@ -5693,6 +5693,12 @@ static void prep_parse_options(struct diff_options *options)\n \tmemcpy(options->parseopts, parseopts, sizeof(parseopts));\n }\n\n+struct option *add_diff_options(const struct option *parseopts,\n+\t\t\t\tstruct diff_options *options)\n+{\n+\treturn parse_options_concat(parseopts, options->parseopts);\n+}\n+\n int diff_opt_parse(struct diff_options *options,\n \t\t   const char **av, int ac, const char *prefix)\n {\ndiff --git a/diff.h b/diff.h\nindex fd33caeb25..c20a1ad76d 100644\n--- a/diff.h\n+++ b/diff.h\n@@ -539,6 +539,7 @@ int git_diff_ui_config(const char *var, const char *value, void *cb);\n #define diff_setup(diffopts) repo_diff_setup(the_repository, diffopts)\n #endif\n void repo_diff_setup(struct repository *, struct diff_options *);\n+struct option *add_diff_options(const struct option *, struct diff_options *);\n int diff_opt_parse(struct diff_options *, const char **, int, const char *);\n void diff_setup_done(struct diff_options *);\n int git_config_rename(const char *var, const char *value);\n--\n2.38.1\n"},{"id":"468316","messageId":"090e9e6b-00ea-c23b-aa2f-6d9f3d79c22a@web.de","threadId":"58876","inReplyTo":"ea838ae7-b635-d4d2-d9df-e96b3d8980af@web.de","subject":"[PATCH v2 2/3] diff: let prep_parse_options() return parseopt array","fromName":"René Scharfe","fromEmail":"l.s.r@web.de","sentAt":"2022-12-01T13:43:07Z","receivedAt":"2022-12-01T13:43:26Z","isPatch":true,"sender":{"key":"l.s.r@web.de","avatar":"https://avatars.githubusercontent.com/u/26122331?v=4"},"body":"prep_parse_options() sets the parseopts member of struct diff_options.\nLet it return the pointer instead and have its caller do the assignment\ninstead and rename the function to get_diff_parseopts() to reflect that\nchange.  This allows using it in other places and with other variables.\n\nSigned-off-by: René Scharfe <l.s.r@web.de>\n---\n diff.c | 9 ++++-----\n 1 file changed, 4 insertions(+), 5 deletions(-)\n\ndiff --git a/diff.c b/diff.c\nindex e01129f0ea..e469d5d2a0 100644\n--- a/diff.c\n+++ b/diff.c\n@@ -4615,7 +4615,7 @@ static void run_checkdiff(struct diff_filepair *p, struct diff_options *o)\n \tbuiltin_checkdiff(name, other, attr_path, p->one, p->two, o);\n }\n\n-static void prep_parse_options(struct diff_options *options);\n+static struct option *get_diff_parseopts(struct diff_options *options);\n\n void repo_diff_setup(struct repository *r, struct diff_options *options)\n {\n@@ -4663,7 +4663,7 @@ void repo_diff_setup(struct repository *r, struct diff_options *options)\n \toptions->color_moved = diff_color_moved_default;\n \toptions->color_moved_ws_handling = diff_color_moved_ws_default;\n\n-\tprep_parse_options(options);\n+\toptions->parseopts = get_diff_parseopts(options);\n }\n\n static const char diff_status_letters[] = {\n@@ -5419,7 +5419,7 @@ static int diff_opt_rotate_to(const struct option *opt, const char *arg, int uns\n \treturn 0;\n }\n\n-static void prep_parse_options(struct diff_options *options)\n+static struct option *get_diff_parseopts(struct diff_options *options)\n {\n \tstruct option parseopts[] = {\n \t\tOPT_GROUP(N_(\"Diff output format options\")),\n@@ -5689,8 +5689,7 @@ static void prep_parse_options(struct diff_options *options)\n \t\tOPT_END()\n \t};\n\n-\tALLOC_ARRAY(options->parseopts, ARRAY_SIZE(parseopts));\n-\tmemcpy(options->parseopts, parseopts, sizeof(parseopts));\n+\treturn parse_options_dup(parseopts);\n }\n\n struct option *add_diff_options(const struct option *parseopts,\n--\n2.38.1\n"},{"id":"468317","messageId":"ec70aee7-2cca-c184-930d-e61a6f4597c3@web.de","threadId":"58876","inReplyTo":"ea838ae7-b635-d4d2-d9df-e96b3d8980af@web.de","subject":"[PATCH v2 3/3] diff: remove parseopts member from struct diff_options","fromName":"René Scharfe","fromEmail":"l.s.r@web.de","sentAt":"2022-12-01T13:44:00Z","receivedAt":"2022-12-01T13:44:15Z","isPatch":true,"sender":{"key":"l.s.r@web.de","avatar":"https://avatars.githubusercontent.com/u/26122331?v=4"},"body":"repo_diff_setup() builds the struct option array with git diff's command\nline options and stores a pointer to it in the parseopts member of\nstruct diff_options.  The array is freed by diff_setup_done(), but not\nby release_revisions().  Thus calling only repo_diff_setup() and\nrelease_revisions() leaks that array.\n\nWe could free it in release_revisions() as well to plug that leak, but\nthere is a better way: Only build it when needed.  Move the\nget_diff_parseopts() calls to the two places that use the array, free it\nafter use and get rid of the parseopts member.\n\nSigned-off-by: René Scharfe <l.s.r@web.de>\n---\n diff.c | 17 ++++++++---------\n diff.h |  1 -\n 2 files changed, 8 insertions(+), 10 deletions(-)\n\ndiff --git a/diff.c b/diff.c\nindex e469d5d2a0..6415c4dc2d 100644\n--- a/diff.c\n+++ b/diff.c\n@@ -4615,8 +4615,6 @@ static void run_checkdiff(struct diff_filepair *p, struct diff_options *o)\n \tbuiltin_checkdiff(name, other, attr_path, p->one, p->two, o);\n }\n\n-static struct option *get_diff_parseopts(struct diff_options *options);\n-\n void repo_diff_setup(struct repository *r, struct diff_options *options)\n {\n \tmemcpy(options, &default_diff_options, sizeof(*options));\n@@ -4662,8 +4660,6 @@ void repo_diff_setup(struct repository *r, struct diff_options *options)\n\n \toptions->color_moved = diff_color_moved_default;\n \toptions->color_moved_ws_handling = diff_color_moved_ws_default;\n-\n-\toptions->parseopts = get_diff_parseopts(options);\n }\n\n static const char diff_status_letters[] = {\n@@ -4821,8 +4817,6 @@ void diff_setup_done(struct diff_options *options)\n \t\t\toptions->filter = ~filter_bit[DIFF_STATUS_FILTER_AON];\n \t\toptions->filter &= ~options->filter_not;\n \t}\n-\n-\tFREE_AND_NULL(options->parseopts);\n }\n\n int parse_long_opt(const char *opt, const char **argv,\n@@ -5695,21 +5689,27 @@ static struct option *get_diff_parseopts(struct diff_options *options)\n struct option *add_diff_options(const struct option *parseopts,\n \t\t\t\tstruct diff_options *options)\n {\n-\treturn parse_options_concat(parseopts, options->parseopts);\n+\tstruct option *diff_parseopts = get_diff_parseopts(options);\n+\tstruct option *result = parse_options_concat(parseopts, diff_parseopts);\n+\tfree(diff_parseopts);\n+\treturn result;\n }\n\n int diff_opt_parse(struct diff_options *options,\n \t\t   const char **av, int ac, const char *prefix)\n {\n+\tstruct option *diff_parseopts = get_diff_parseopts(options);\n+\n \tif (!prefix)\n \t\tprefix = \"\";\n\n-\tac = parse_options(ac, av, prefix, options->parseopts, NULL,\n+\tac = parse_options(ac, av, prefix, diff_parseopts, NULL,\n \t\t\t   PARSE_OPT_KEEP_DASHDASH |\n \t\t\t   PARSE_OPT_KEEP_UNKNOWN_OPT |\n \t\t\t   PARSE_OPT_NO_INTERNAL_HELP |\n \t\t\t   PARSE_OPT_ONE_SHOT |\n \t\t\t   PARSE_OPT_STOP_AT_NON_OPTION);\n+\tfree(diff_parseopts);\n\n \treturn ac;\n }\n@@ -6518,7 +6518,6 @@ void diff_free(struct diff_options *options)\n \tdiff_free_file(options);\n \tdiff_free_ignore_regex(options);\n \tclear_pathspec(&options->pathspec);\n-\tFREE_AND_NULL(options->parseopts);\n }\n\n void diff_flush(struct diff_options *options)\ndiff --git a/diff.h b/diff.h\nindex c20a1ad76d..41eb2c3d42 100644\n--- a/diff.h\n+++ b/diff.h\n@@ -394,7 +394,6 @@ struct diff_options {\n \tunsigned color_moved_ws_handling;\n\n \tstruct repository *repo;\n-\tstruct option *parseopts;\n \tstruct strmap *additional_path_headers;\n\n \tint no_free;\n--\n2.38.1\n"},{"id":"468319","messageId":"CAOLTT8SpF=i6Lwc28SWSDf2uOqoxLpMHiVS5GefwYcCHigL2NQ@mail.gmail.com","threadId":"58876","inReplyTo":"19be5afa-63e8-5f11-c5f4-6f391677e4c4@web.de","subject":"Re: [PATCH 1/3] diff: factor out add_diff_options()","fromName":"ZheNing Hu","fromEmail":"adlternative@gmail.com","sentAt":"2022-12-01T14:11:59Z","receivedAt":"2022-12-01T14:12:17Z","isPatch":true,"sender":{"key":"adlternative@gmail.com","avatar":"https://avatars.githubusercontent.com/u/58138461?v=4"},"body":"Hi,\n\nRené Scharfe <l.s.r@web.de> 于2022年12月1日周四 02:13写道：\n>\n> Add a function for appending the parseopts member of struct diff_options\n> to a struct option array.  Use it in two sites instead of accessing the\n> parseopts member directly.  Decoupling callers from diff internals like\n> that allows us to change the latter.\n>\n> Signed-off-by: René Scharfe <l.s.r@web.de>\n> ---\n>  builtin/range-diff.c | 2 +-\n>  diff-no-index.c      | 3 +--\n>  diff.c               | 6 ++++++\n>  diff.h               | 1 +\n>  4 files changed, 9 insertions(+), 3 deletions(-)\n>\n> diff --git a/builtin/range-diff.c b/builtin/range-diff.c\n> index e2a74efb42..aecfae12d3 100644\n> --- a/builtin/range-diff.c\n> +++ b/builtin/range-diff.c\n> @@ -47,7 +47,7 @@ int cmd_range_diff(int argc, const char **argv, const char *prefix)\n>\n>         repo_diff_setup(the_repository, &diffopt);\n>\n> -       options = parse_options_concat(range_diff_options, diffopt.parseopts);\n> +       options = add_diff_options(range_diff_options, &diffopt);\n>         argc = parse_options(argc, argv, prefix, options,\n>                              builtin_range_diff_usage, PARSE_OPT_KEEP_DASHDASH);\n>\n> diff --git a/diff-no-index.c b/diff-no-index.c\n> index 18edbdf4b5..05fafd0019 100644\n> --- a/diff-no-index.c\n> +++ b/diff-no-index.c\n> @@ -255,8 +255,7 @@ int diff_no_index(struct rev_info *revs,\n>         };\n>         struct option *options;\n>\n> -       options = parse_options_concat(no_index_options,\n> -                                      revs->diffopt.parseopts);\n> +       options = add_diff_options(no_index_options, &revs->diffopt);\n>         argc = parse_options(argc, argv, revs->prefix, options,\n>                              diff_no_index_usage, 0);\n>         if (argc != 2) {\n> diff --git a/diff.c b/diff.c\n> index 1054a4b732..e01129f0ea 100644\n> --- a/diff.c\n> +++ b/diff.c\n> @@ -5693,6 +5693,12 @@ static void prep_parse_options(struct diff_options *options)\n>         memcpy(options->parseopts, parseopts, sizeof(parseopts));\n>  }\n>\n> +struct option *add_diff_options(const struct option *parseopts,\n> +                               struct diff_options *options)\n> +{\n> +       return parse_options_concat(parseopts, options->parseopts);\n> +}\n> +\n\nThis idea is very good. I am implementing `--scope` for git diff\nrecently [1], but I\ndon’t want this option to be directly inherited by commands such as git log,\ngit format-patch, etc. It would be best if the option can be added\ndynamically...\nThis patch looks like it can be used to do just that! :)\n\n>  int diff_opt_parse(struct diff_options *options,\n>                    const char **av, int ac, const char *prefix)\n>  {\n> diff --git a/diff.h b/diff.h\n> index fd33caeb25..c20a1ad76d 100644\n> --- a/diff.h\n> +++ b/diff.h\n> @@ -539,6 +539,7 @@ int git_diff_ui_config(const char *var, const char *value, void *cb);\n>  #define diff_setup(diffopts) repo_diff_setup(the_repository, diffopts)\n>  #endif\n>  void repo_diff_setup(struct repository *, struct diff_options *);\n> +struct option *add_diff_options(const struct option *, struct diff_options *);\n>  int diff_opt_parse(struct diff_options *, const char **, int, const char *);\n>  void diff_setup_done(struct diff_options *);\n>  int git_config_rename(const char *var, const char *value);\n> --\n> 2.38.1\n\nThanks!\n\n[1]: https://lore.kernel.org/git/pull.1398.v3.git.1669723221.gitgitgadget@gmail.com/\n"},{"id":"468333","messageId":"221201.86r0xjgicc.gmgdl@evledraar.gmail.com","threadId":"58876","inReplyTo":"ea838ae7-b635-d4d2-d9df-e96b3d8980af@web.de","subject":"Re: [PATCH v2 0/3] diff: build parseopts array on demand","fromName":"Ævar Arnfjörð Bjarmason","fromEmail":"avarab@gmail.com","sentAt":"2022-12-01T16:54:31Z","receivedAt":"2022-12-01T17:11:06Z","isPatch":true,"sender":{"key":"avarab@gmail.com","avatar":"https://avatars.githubusercontent.com/u/45301?v=4"},"body":"\nOn Thu, Dec 01 2022, René Scharfe wrote:\n\n> Calling repo_init_revisions() and release_revisions() in that order\n> leaks the memory allocated for the parseopts array in the embedded\n> struct diff_options member.  Get rid of that leak by reducing the\n> lifetime of that array.\n>\n> Original patch:\n> https://lore.kernel.org/git/4fd82dc6-e0f8-0638-5b10-16bfef39a171@web.de/\n>\n> Submitted separately from that thread because it's independent enough.\n>\n> Change since v1:\n> - Actually remove the parseopts member.  Its removal got lost during\n>   refactoring in v1.  Thank you for spotting that, Junio!\n>\n>   diff: factor out add_diff_options()\n>   diff: let prep_parse_options() return parseopt array\n>   diff: remove parseopts member from struct diff_options\n>\n>  builtin/range-diff.c |  2 +-\n>  diff-no-index.c      |  3 +--\n>  diff.c               | 26 +++++++++++++++-----------\n>  diff.h               |  2 +-\n>  4 files changed, 18 insertions(+), 15 deletions(-)\n>\n> Range-Diff gegen v1:\n> 1:  630f95320f = 1:  4dc8b2632b diff: factor out add_diff_options()\n> 2:  4b56fa795c = 2:  10903d355e diff: let prep_parse_options() return parseopt array\n> 3:  7e54e4370a ! 3:  24bd18ae79 diff: remove parseopts member from struct diff_options\n>     @@ diff.c: void diff_free(struct diff_options *options)\n>       }\n>\n>       void diff_flush(struct diff_options *options)\n>     +\n>     + ## diff.h ##\n>     +@@ diff.h: struct diff_options {\n>     + \tunsigned color_moved_ws_handling;\n>     +\n>     + \tstruct repository *repo;\n>     +-\tstruct option *parseopts;\n>     + \tstruct strmap *additional_path_headers;\n>     +\n>     + \tint no_free;\n\nThis looks good to me. Would you mind running the tests with:\n\n\tGIT_TEST_PASSING_SANITIZE_LEAK=check GIT_TEST_SANITIZE_LEAK_LOG=true make SANITIZE=leak \n\nAnd then marking up the ones that now pass with\nTEST_PASSES_SANITIZE_LEAK=true. I think it's all except one of these\n(one isn't marked on \"master\", I forget which one):\n\n\tTest Summary Report\n\t-------------------\n\tt1022-read-tree-partial-clone.sh                 (Wstat: 256 Tests: 1 Failed: 0)\n\t  Non-zero exit status: 1\n\tt2012-checkout-last.sh                           (Wstat: 256 Tests: 22 Failed: 0)\n\t  Non-zero exit status: 1\n\tt3210-pack-refs.sh                               (Wstat: 256 Tests: 30 Failed: 0)\n\t  Non-zero exit status: 1\n\tt4053-diff-no-index.sh                           (Wstat: 256 Tests: 19 Failed: 0)\n\t  Non-zero exit status: 1\n\tt5554-noop-fetch-negotiator.sh                   (Wstat: 256 Tests: 1 Failed: 0)\n\t  Non-zero exit status: 1\n\tt5613-info-alternate.sh                          (Wstat: 256 Tests: 13 Failed: 0)\n\t  Non-zero exit status: 1\n\tt6021-rev-list-exclude-hidden.sh                 (Wstat: 256 Tests: 42 Failed: 0)\n\t  Non-zero exit status: 1\n\tt6415-merge-dir-to-symlink.sh                    (Wstat: 256 Tests: 24 Failed: 0)\n\t  Non-zero exit status: 1\n\tt7403-submodule-sync.sh                          (Wstat: 256 Tests: 18 Failed: 0)\n\t  Non-zero exit status: 1\n\tt7504-commit-msg-hook.sh                         (Wstat: 256 Tests: 30 Failed: 0)\n\t  Non-zero exit status: 1\n\tt9115-git-svn-dcommit-funky-renames.sh           (Wstat: 256 Tests: 12 Failed: 0)\n\t  Non-zero exit status: 1\n\tt9146-git-svn-empty-dirs.sh                      (Wstat: 256 Tests: 14 Failed: 0)\n\t  Non-zero exit status: 1\n\tt9160-git-svn-preserve-empty-dirs.sh             (Wstat: 256 Tests: 12 Failed: 0)\n\t  Non-zero exit status: 1\n\nI.e. this makes a lot more tests pass leak-free, yay!\n\nTo nitpick a bit: I didn't find that splitting this up into three\npatches helped to read it, e.g. 2/3 adds code that promptly goes away in\n3/3.\n\nI also wondered why add two API functions for this, instead of just\nteaching the \"prep options\" to concat passed-in options with the user\noptions? That also avoids a parse_options_dup(). I.e. with that squashed\nin the whole thing is:\n\n builtin/range-diff.c |  2 +-\n diff-no-index.c      |  3 +--\n diff.c               | 19 ++++++++-----------\n diff.h               |  3 ++-\n 4 files changed, 12 insertions(+), 15 deletions(-)\n\ndiff --git a/builtin/range-diff.c b/builtin/range-diff.c\nindex e2a74efb42a..8fcd6663b89 100644\n--- a/builtin/range-diff.c\n+++ b/builtin/range-diff.c\n@@ -47,7 +47,7 @@ int cmd_range_diff(int argc, const char **argv, const char *prefix)\n \n \trepo_diff_setup(the_repository, &diffopt);\n \n-\toptions = parse_options_concat(range_diff_options, diffopt.parseopts);\n+\toptions = add_diff_parseopts(range_diff_options, &diffopt);\n \targc = parse_options(argc, argv, prefix, options,\n \t\t\t     builtin_range_diff_usage, PARSE_OPT_KEEP_DASHDASH);\n \ndiff --git a/diff-no-index.c b/diff-no-index.c\nindex 18edbdf4b59..efac1d38b38 100644\n--- a/diff-no-index.c\n+++ b/diff-no-index.c\n@@ -255,8 +255,7 @@ int diff_no_index(struct rev_info *revs,\n \t};\n \tstruct option *options;\n \n-\toptions = parse_options_concat(no_index_options,\n-\t\t\t\t       revs->diffopt.parseopts);\n+\toptions = add_diff_parseopts(no_index_options, &revs->diffopt);\n \targc = parse_options(argc, argv, revs->prefix, options,\n \t\t\t     diff_no_index_usage, 0);\n \tif (argc != 2) {\ndiff --git a/diff.c b/diff.c\nindex 1054a4b7329..e186fc91802 100644\n--- a/diff.c\n+++ b/diff.c\n@@ -4615,8 +4615,6 @@ static void run_checkdiff(struct diff_filepair *p, struct diff_options *o)\n \tbuiltin_checkdiff(name, other, attr_path, p->one, p->two, o);\n }\n \n-static void prep_parse_options(struct diff_options *options);\n-\n void repo_diff_setup(struct repository *r, struct diff_options *options)\n {\n \tmemcpy(options, &default_diff_options, sizeof(*options));\n@@ -4662,8 +4660,6 @@ void repo_diff_setup(struct repository *r, struct diff_options *options)\n \n \toptions->color_moved = diff_color_moved_default;\n \toptions->color_moved_ws_handling = diff_color_moved_ws_default;\n-\n-\tprep_parse_options(options);\n }\n \n static const char diff_status_letters[] = {\n@@ -4821,8 +4817,6 @@ void diff_setup_done(struct diff_options *options)\n \t\t\toptions->filter = ~filter_bit[DIFF_STATUS_FILTER_AON];\n \t\toptions->filter &= ~options->filter_not;\n \t}\n-\n-\tFREE_AND_NULL(options->parseopts);\n }\n \n int parse_long_opt(const char *opt, const char **argv,\n@@ -5419,7 +5413,8 @@ static int diff_opt_rotate_to(const struct option *opt, const char *arg, int uns\n \treturn 0;\n }\n \n-static void prep_parse_options(struct diff_options *options)\n+struct option *add_diff_parseopts(struct option *useropts,\n+\t\t\t\t  struct diff_options *options)\n {\n \tstruct option parseopts[] = {\n \t\tOPT_GROUP(N_(\"Diff output format options\")),\n@@ -5689,22 +5684,25 @@ static void prep_parse_options(struct diff_options *options)\n \t\tOPT_END()\n \t};\n \n-\tALLOC_ARRAY(options->parseopts, ARRAY_SIZE(parseopts));\n-\tmemcpy(options->parseopts, parseopts, sizeof(parseopts));\n+\treturn parse_options_concat(useropts, parseopts);\n }\n \n int diff_opt_parse(struct diff_options *options,\n \t\t   const char **av, int ac, const char *prefix)\n {\n+\tstruct option no_options[] = { OPT_END() };\n+\tstruct option *diff_parseopts = add_diff_parseopts(no_options, options);\n+\n \tif (!prefix)\n \t\tprefix = \"\";\n \n-\tac = parse_options(ac, av, prefix, options->parseopts, NULL,\n+\tac = parse_options(ac, av, prefix, diff_parseopts, NULL,\n \t\t\t   PARSE_OPT_KEEP_DASHDASH |\n \t\t\t   PARSE_OPT_KEEP_UNKNOWN_OPT |\n \t\t\t   PARSE_OPT_NO_INTERNAL_HELP |\n \t\t\t   PARSE_OPT_ONE_SHOT |\n \t\t\t   PARSE_OPT_STOP_AT_NON_OPTION);\n+\tfree(diff_parseopts);\n \n \treturn ac;\n }\n@@ -6513,7 +6511,6 @@ void diff_free(struct diff_options *options)\n \tdiff_free_file(options);\n \tdiff_free_ignore_regex(options);\n \tclear_pathspec(&options->pathspec);\n-\tFREE_AND_NULL(options->parseopts);\n }\n \n void diff_flush(struct diff_options *options)\ndiff --git a/diff.h b/diff.h\nindex fd33caeb25d..56704d3de22 100644\n--- a/diff.h\n+++ b/diff.h\n@@ -394,7 +394,6 @@ struct diff_options {\n \tunsigned color_moved_ws_handling;\n \n \tstruct repository *repo;\n-\tstruct option *parseopts;\n \tstruct strmap *additional_path_headers;\n \n \tint no_free;\n@@ -539,6 +538,8 @@ int git_diff_ui_config(const char *var, const char *value, void *cb);\n #define diff_setup(diffopts) repo_diff_setup(the_repository, diffopts)\n #endif\n void repo_diff_setup(struct repository *, struct diff_options *);\n+struct option *add_diff_parseopts(struct option *useropts,\n+\t\t\t\t  struct diff_options *options);\n int diff_opt_parse(struct diff_options *, const char **, int, const char *);\n void diff_setup_done(struct diff_options *);\n int git_config_rename(const char *var, const char *value);\n"},{"id":"468338","messageId":"572e29a2-3f19-e946-d152-009999651084@web.de","threadId":"58876","inReplyTo":"221201.86r0xjgicc.gmgdl@evledraar.gmail.com","subject":"Re: [PATCH v2 0/3] diff: build parseopts array on demand","fromName":"René Scharfe","fromEmail":"l.s.r@web.de","sentAt":"2022-12-01T19:01:47Z","receivedAt":"2022-12-01T19:02:03Z","isPatch":true,"sender":{"key":"l.s.r@web.de","avatar":"https://avatars.githubusercontent.com/u/26122331?v=4"},"body":"Am 01.12.2022 um 17:54 schrieb Ævar Arnfjörð Bjarmason:\n>\n> On Thu, Dec 01 2022, René Scharfe wrote:\n>\n>> Calling repo_init_revisions() and release_revisions() in that order\n>> leaks the memory allocated for the parseopts array in the embedded\n>> struct diff_options member.  Get rid of that leak by reducing the\n>> lifetime of that array.\n>>\n>> Original patch:\n>> https://lore.kernel.org/git/4fd82dc6-e0f8-0638-5b10-16bfef39a171@web.de/\n>>\n>> Submitted separately from that thread because it's independent enough.\n>>\n>> Change since v1:\n>> - Actually remove the parseopts member.  Its removal got lost during\n>>   refactoring in v1.  Thank you for spotting that, Junio!\n>>\n>>   diff: factor out add_diff_options()\n>>   diff: let prep_parse_options() return parseopt array\n>>   diff: remove parseopts member from struct diff_options\n>>\n>>  builtin/range-diff.c |  2 +-\n>>  diff-no-index.c      |  3 +--\n>>  diff.c               | 26 +++++++++++++++-----------\n>>  diff.h               |  2 +-\n>>  4 files changed, 18 insertions(+), 15 deletions(-)\n>>\n>> Range-Diff gegen v1:\n>> 1:  630f95320f = 1:  4dc8b2632b diff: factor out add_diff_options()\n>> 2:  4b56fa795c = 2:  10903d355e diff: let prep_parse_options() return parseopt array\n>> 3:  7e54e4370a ! 3:  24bd18ae79 diff: remove parseopts member from struct diff_options\n>>     @@ diff.c: void diff_free(struct diff_options *options)\n>>       }\n>>\n>>       void diff_flush(struct diff_options *options)\n>>     +\n>>     + ## diff.h ##\n>>     +@@ diff.h: struct diff_options {\n>>     + \tunsigned color_moved_ws_handling;\n>>     +\n>>     + \tstruct repository *repo;\n>>     +-\tstruct option *parseopts;\n>>     + \tstruct strmap *additional_path_headers;\n>>     +\n>>     + \tint no_free;\n>\n> This looks good to me. Would you mind running the tests with:\n>\n> \tGIT_TEST_PASSING_SANITIZE_LEAK=check GIT_TEST_SANITIZE_LEAK_LOG=true make SANITIZE=leak\n>\n> And then marking up the ones that now pass with\n> TEST_PASSES_SANITIZE_LEAK=true. I think it's all except one of these\n> (one isn't marked on \"master\", I forget which one):\n>\n> \tTest Summary Report\n> \t-------------------\n> \tt1022-read-tree-partial-clone.sh                 (Wstat: 256 Tests: 1 Failed: 0)\n> \t  Non-zero exit status: 1\n> \tt2012-checkout-last.sh                           (Wstat: 256 Tests: 22 Failed: 0)\n> \t  Non-zero exit status: 1\n> \tt3210-pack-refs.sh                               (Wstat: 256 Tests: 30 Failed: 0)\n> \t  Non-zero exit status: 1\n> \tt4053-diff-no-index.sh                           (Wstat: 256 Tests: 19 Failed: 0)\n> \t  Non-zero exit status: 1\n> \tt5554-noop-fetch-negotiator.sh                   (Wstat: 256 Tests: 1 Failed: 0)\n> \t  Non-zero exit status: 1\n> \tt5613-info-alternate.sh                          (Wstat: 256 Tests: 13 Failed: 0)\n> \t  Non-zero exit status: 1\n> \tt6021-rev-list-exclude-hidden.sh                 (Wstat: 256 Tests: 42 Failed: 0)\n> \t  Non-zero exit status: 1\n> \tt6415-merge-dir-to-symlink.sh                    (Wstat: 256 Tests: 24 Failed: 0)\n> \t  Non-zero exit status: 1\n> \tt7403-submodule-sync.sh                          (Wstat: 256 Tests: 18 Failed: 0)\n> \t  Non-zero exit status: 1\n> \tt7504-commit-msg-hook.sh                         (Wstat: 256 Tests: 30 Failed: 0)\n> \t  Non-zero exit status: 1\n> \tt9115-git-svn-dcommit-funky-renames.sh           (Wstat: 256 Tests: 12 Failed: 0)\n> \t  Non-zero exit status: 1\n> \tt9146-git-svn-empty-dirs.sh                      (Wstat: 256 Tests: 14 Failed: 0)\n> \t  Non-zero exit status: 1\n> \tt9160-git-svn-preserve-empty-dirs.sh             (Wstat: 256 Tests: 12 Failed: 0)\n> \t  Non-zero exit status: 1\n>\n> I.e. this makes a lot more tests pass leak-free, yay!\n\nWith -rc1 (i.e. without this series) I get:\n\nt1022-read-tree-partial-clone.sh                 (Wstat: 256 Tests: 1 Failed: 0)\n  Non-zero exit status: 1\nt2016-checkout-patch.sh                          (Wstat: 256 Tests: 16 Failed: 0)\n  Non-zero exit status: 1\nt2012-checkout-last.sh                           (Wstat: 256 Tests: 22 Failed: 0)\n  Non-zero exit status: 1\nt4023-diff-rename-typechange.sh                  (Wstat: 256 Tests: 4 Failed: 0)\n  Non-zero exit status: 1\nt4053-diff-no-index.sh                           (Wstat: 256 Tests: 19 Failed: 0)\n  Non-zero exit status: 1\nt4058-diff-duplicates.sh                         (Wstat: 256 Tests: 16 Failed: 0)\n  Non-zero exit status: 1\nt4205-log-pretty-formats.sh                      (Wstat: 256 Tests: 21 Failed: 0)\n  Non-zero exit status: 1\n  Parse errors: No plan found in TAP output\nt5406-remote-rejects.sh                          (Wstat: 256 Tests: 3 Failed: 0)\n  Non-zero exit status: 1\nt5507-remote-environment.sh                      (Wstat: 256 Tests: 5 Failed: 0)\n  Non-zero exit status: 1\nt5554-noop-fetch-negotiator.sh                   (Wstat: 256 Tests: 1 Failed: 0)\n  Non-zero exit status: 1\nt6001-rev-list-graft.sh                          (Wstat: 256 Tests: 14 Failed: 0)\n  Non-zero exit status: 1\nt6021-rev-list-exclude-hidden.sh                 (Wstat: 256 Tests: 42 Failed: 0)\n  Non-zero exit status: 1\nt6401-merge-criss-cross.sh                       (Wstat: 256 Tests: 4 Failed: 0)\n  Non-zero exit status: 1\nt6407-merge-binary.sh                            (Wstat: 256 Tests: 3 Failed: 0)\n  Non-zero exit status: 1\nt6415-merge-dir-to-symlink.sh                    (Wstat: 256 Tests: 24 Failed: 0)\n  Non-zero exit status: 1\nt7006-pager.sh                                   (Wstat: 256 Tests: 109 Failed: 0)\n  Non-zero exit status: 1\nt7008-filter-branch-null-sha1.sh                 (Wstat: 256 Tests: 6 Failed: 0)\n  Non-zero exit status: 1\nt7504-commit-msg-hook.sh                         (Wstat: 256 Tests: 30 Failed: 0)\n  Non-zero exit status: 1\nt7517-per-repo-email.sh                          (Wstat: 256 Tests: 16 Failed: 0)\n  Non-zero exit status: 1\nt7605-merge-resolve.sh                           (Wstat: 256 Tests: 4 Failed: 0)\n  Non-zero exit status: 1\n\nThere is some overlap with your results, but also several differences.\nI wonder why so many more tests appear to be leak-free for me.  I used\nDebian clang version 11.0.1-2.\n\nIn any case it seems we need to update the marks before we can\nattribute which tests are made leak-free by any new patches.\n\nThe TAP error in t4205-log-pretty-formats.sh is fixed by the following\npatch, but I can't explain it:\n\ndiff --git a/t/t4205-log-pretty-formats.sh b/t/t4205-log-pretty-formats.sh\nindex e448ef2928..0404491d6e 100755\n--- a/t/t4205-log-pretty-formats.sh\n+++ b/t/t4205-log-pretty-formats.sh\n@@ -156,7 +156,7 @@ test_expect_success 'NUL termination with --reflog --pretty=oneline' '\n \tfor r in $revs\n \tdo\n \t\tgit show -s --pretty=oneline \"$r\" >raw &&\n-\t\tcat raw | lf_to_nul || exit 1\n+\t\tcat raw | lf_to_nul || return 1\n \tdone >expect &&\n \t# the trailing NUL is already produced so we do not need to\n \t# output another one\n\n> To nitpick a bit: I didn't find that splitting this up into three\n> patches helped to read it, e.g. 2/3 adds code that promptly goes away in\n> 3/3.\n>\n> I also wondered why add two API functions for this, instead of just\n> teaching the \"prep options\" to concat passed-in options with the user\n> options?\n\nOnly add_diff_options() is exported.\n\nget_diff_parseopts() is more flexible than a function that concatenates.\nCallers can choose the order, or not concatenate at all.\n\nAn inter-diff would have been nice.  Let's see if I can find the\nrelevant parts.\n\n> That also avoids a parse_options_dup(). I.e. with that squashed\n> in the whole thing is:\n>\n>  builtin/range-diff.c |  2 +-\n>  diff-no-index.c      |  3 +--\n>  diff.c               | 19 ++++++++-----------\n>  diff.h               |  3 ++-\n>  4 files changed, 12 insertions(+), 15 deletions(-)\n>\n> diff --git a/builtin/range-diff.c b/builtin/range-diff.c\n> index e2a74efb42a..8fcd6663b89 100644\n> --- a/builtin/range-diff.c\n> +++ b/builtin/range-diff.c\n> @@ -47,7 +47,7 @@ int cmd_range_diff(int argc, const char **argv, const char *prefix)\n>\n>  \trepo_diff_setup(the_repository, &diffopt);\n>\n> -\toptions = parse_options_concat(range_diff_options, diffopt.parseopts);\n> +\toptions = add_diff_parseopts(range_diff_options, &diffopt);\n>  \targc = parse_options(argc, argv, prefix, options,\n>  \t\t\t     builtin_range_diff_usage, PARSE_OPT_KEEP_DASHDASH);\n>\n> diff --git a/diff-no-index.c b/diff-no-index.c\n> index 18edbdf4b59..efac1d38b38 100644\n> --- a/diff-no-index.c\n> +++ b/diff-no-index.c\n> @@ -255,8 +255,7 @@ int diff_no_index(struct rev_info *revs,\n>  \t};\n>  \tstruct option *options;\n>\n> -\toptions = parse_options_concat(no_index_options,\n> -\t\t\t\t       revs->diffopt.parseopts);\n> +\toptions = add_diff_parseopts(no_index_options, &revs->diffopt);\n>  \targc = parse_options(argc, argv, revs->prefix, options,\n>  \t\t\t     diff_no_index_usage, 0);\n>  \tif (argc != 2) {\n> diff --git a/diff.c b/diff.c\n> index 1054a4b7329..e186fc91802 100644\n> --- a/diff.c\n> +++ b/diff.c\n> @@ -4615,8 +4615,6 @@ static void run_checkdiff(struct diff_filepair *p, struct diff_options *o)\n>  \tbuiltin_checkdiff(name, other, attr_path, p->one, p->two, o);\n>  }\n>\n> -static void prep_parse_options(struct diff_options *options);\n> -\n>  void repo_diff_setup(struct repository *r, struct diff_options *options)\n>  {\n>  \tmemcpy(options, &default_diff_options, sizeof(*options));\n> @@ -4662,8 +4660,6 @@ void repo_diff_setup(struct repository *r, struct diff_options *options)\n>\n>  \toptions->color_moved = diff_color_moved_default;\n>  \toptions->color_moved_ws_handling = diff_color_moved_ws_default;\n> -\n> -\tprep_parse_options(options);\n>  }\n>\n>  static const char diff_status_letters[] = {\n> @@ -4821,8 +4817,6 @@ void diff_setup_done(struct diff_options *options)\n>  \t\t\toptions->filter = ~filter_bit[DIFF_STATUS_FILTER_AON];\n>  \t\toptions->filter &= ~options->filter_not;\n>  \t}\n> -\n> -\tFREE_AND_NULL(options->parseopts);\n>  }\n>\n>  int parse_long_opt(const char *opt, const char **argv,\n> @@ -5419,7 +5413,8 @@ static int diff_opt_rotate_to(const struct option *opt, const char *arg, int uns\n>  \treturn 0;\n>  }\n>\n> -static void prep_parse_options(struct diff_options *options)\n> +struct option *add_diff_parseopts(struct option *useropts,\n> +\t\t\t\t  struct diff_options *options)\n\nYou rename add_diff_options() to add_diff_parseopts().  OK.\n\n>  {\n>  \tstruct option parseopts[] = {\n>  \t\tOPT_GROUP(N_(\"Diff output format options\")),\n> @@ -5689,22 +5684,25 @@ static void prep_parse_options(struct diff_options *options)\n>  \t\tOPT_END()\n>  \t};\n>\n> -\tALLOC_ARRAY(options->parseopts, ARRAY_SIZE(parseopts));\n> -\tmemcpy(options->parseopts, parseopts, sizeof(parseopts));\n> +\treturn parse_options_concat(useropts, parseopts);\n\nYou bring back the concatenate from my demo patch.\n\n>  }\n>\n>  int diff_opt_parse(struct diff_options *options,\n>  \t\t   const char **av, int ac, const char *prefix)\n>  {\n> +\tstruct option no_options[] = { OPT_END() };\n> +\tstruct option *diff_parseopts = add_diff_parseopts(no_options, options);\n\nThis kinda open-codes parse_options_dup().  The reduced flexibility of\nthe concatenating variant is easily worked around here and it avoids the\nneed for slightly awkward manual concatenation.\n\nWell, the get_diff_parseopts() variant feels cleaner, but baking the\nconcat in is simpler overall.\n\n> +\n>  \tif (!prefix)\n>  \t\tprefix = \"\";\n>\n> -\tac = parse_options(ac, av, prefix, options->parseopts, NULL,\n> +\tac = parse_options(ac, av, prefix, diff_parseopts, NULL,\n>  \t\t\t   PARSE_OPT_KEEP_DASHDASH |\n>  \t\t\t   PARSE_OPT_KEEP_UNKNOWN_OPT |\n>  \t\t\t   PARSE_OPT_NO_INTERNAL_HELP |\n>  \t\t\t   PARSE_OPT_ONE_SHOT |\n>  \t\t\t   PARSE_OPT_STOP_AT_NON_OPTION);\n> +\tfree(diff_parseopts);\n>\n>  \treturn ac;\n>  }\n> @@ -6513,7 +6511,6 @@ void diff_free(struct diff_options *options)\n>  \tdiff_free_file(options);\n>  \tdiff_free_ignore_regex(options);\n>  \tclear_pathspec(&options->pathspec);\n> -\tFREE_AND_NULL(options->parseopts);\n>  }\n>\n>  void diff_flush(struct diff_options *options)\n> diff --git a/diff.h b/diff.h\n> index fd33caeb25d..56704d3de22 100644\n> --- a/diff.h\n> +++ b/diff.h\n> @@ -394,7 +394,6 @@ struct diff_options {\n>  \tunsigned color_moved_ws_handling;\n>\n>  \tstruct repository *repo;\n> -\tstruct option *parseopts;\n>  \tstruct strmap *additional_path_headers;\n>\n>  \tint no_free;\n> @@ -539,6 +538,8 @@ int git_diff_ui_config(const char *var, const char *value, void *cb);\n>  #define diff_setup(diffopts) repo_diff_setup(the_repository, diffopts)\n>  #endif\n>  void repo_diff_setup(struct repository *, struct diff_options *);\n> +struct option *add_diff_parseopts(struct option *useropts,\n> +\t\t\t\t  struct diff_options *options);\n>  int diff_opt_parse(struct diff_options *, const char **, int, const char *);\n>  void diff_setup_done(struct diff_options *);\n>  int git_config_rename(const char *var, const char *value);\n\n"},{"id":"468341","messageId":"CAPig+cTdSNfWwN-c7R3-MF6ipAD6-7aVjxRK9f9XaKzg4yhhyg@mail.gmail.com","threadId":"58876","inReplyTo":"572e29a2-3f19-e946-d152-009999651084@web.de","subject":"Re: [PATCH v2 0/3] diff: build parseopts array on demand","fromName":"Eric Sunshine","fromEmail":"sunshine@sunshineco.com","sentAt":"2022-12-01T19:19:50Z","receivedAt":"2022-12-01T19:20:05Z","isPatch":true,"sender":{"key":"sunshine@sunshineco.com","avatar":"https://avatars.githubusercontent.com/u/163641?v=4"},"body":"On Thu, Dec 1, 2022 at 2:11 PM René Scharfe <l.s.r@web.de> wrote:\n> t4205-log-pretty-formats.sh                      (Wstat: 256 Tests: 21 Failed: 0)\n>   Non-zero exit status: 1\n>   Parse errors: No plan found in TAP output\n>\n> The TAP error in t4205-log-pretty-formats.sh is fixed by the following\n> patch, but I can't explain it:\n>\n> diff --git a/t/t4205-log-pretty-formats.sh b/t/t4205-log-pretty-formats.sh\n> @@ -156,7 +156,7 @@ test_expect_success 'NUL termination with --reflog --pretty=oneline' '\n>         for r in $revs\n>         do\n>                 git show -s --pretty=oneline \"$r\" >raw &&\n> -               cat raw | lf_to_nul || exit 1\n> +               cat raw | lf_to_nul || return 1\n>         done >expect &&\n\nMakes sense. The `exit 1` undesirably causes the entire script to\nabort, which means test_done() is never invoked, whereas `return 1`\nmakes only the test fail. `exit 1` would be appropriate inside a\nsubshell but there is no subshell here.\n"},{"id":"468348","messageId":"3d9ccbda-201b-fdaf-5fa3-224ba10b8c40@web.de","threadId":"58876","inReplyTo":"CAPig+cTdSNfWwN-c7R3-MF6ipAD6-7aVjxRK9f9XaKzg4yhhyg@mail.gmail.com","subject":"Re: [PATCH v2 0/3] diff: build parseopts array on demand","fromName":"René Scharfe","fromEmail":"l.s.r@web.de","sentAt":"2022-12-01T19:43:04Z","receivedAt":"2022-12-01T19:43:32Z","isPatch":true,"sender":{"key":"l.s.r@web.de","avatar":"https://avatars.githubusercontent.com/u/26122331?v=4"},"body":"Am 01.12.2022 um 20:19 schrieb Eric Sunshine:\n> On Thu, Dec 1, 2022 at 2:11 PM René Scharfe <l.s.r@web.de> wrote:\n>> t4205-log-pretty-formats.sh                      (Wstat: 256 Tests: 21 Failed: 0)\n>>   Non-zero exit status: 1\n>>   Parse errors: No plan found in TAP output\n>>\n>> The TAP error in t4205-log-pretty-formats.sh is fixed by the following\n>> patch, but I can't explain it:\n>>\n>> diff --git a/t/t4205-log-pretty-formats.sh b/t/t4205-log-pretty-formats.sh\n>> @@ -156,7 +156,7 @@ test_expect_success 'NUL termination with --reflog --pretty=oneline' '\n>>         for r in $revs\n>>         do\n>>                 git show -s --pretty=oneline \"$r\" >raw &&\n>> -               cat raw | lf_to_nul || exit 1\n>> +               cat raw | lf_to_nul || return 1\n>>         done >expect &&\n>\n> Makes sense. The `exit 1` undesirably causes the entire script to\n> abort, which means test_done() is never invoked, whereas `return 1`\n> makes only the test fail. `exit 1` would be appropriate inside a\n> subshell but there is no subshell here.\n\nRight.  Here's how I got confused:\n\nWhat's failing?  \"cat raw\" works in the trash directory.  lf_to_nul is\nnot found.  Of course, it's defined in test-lib-functions.sh.  So source\nthat.  Then \"cat raw | lf_to_nul\" can be started and fails as expected.\nAnd it reports: \"-bash: 4: Bad file descriptor\".  What?!\n\nlf_to_nul calls perl, and test-lib-functions.sh defines \"perl\" as a\nfunction that redirects to &4.  Which is not open in my shell.\n\nBut the confusion was at the beginning: Of course it's \"git show\" that's\nfailing and triggering the exit from the test, not cat or lf_to_nul.\nDidn't even get to that point because I somehow thought the redirection\nmagic was somehow broken.\n\nSo the lesson is to start searching at the beginning, I guess..\n\nRené\n"},{"id":"468357","messageId":"xmqqiliuss8k.fsf@gitster.g","threadId":"58876","inReplyTo":"20b5d6d1-ec09-e09f-4f92-5925fb9c96f8@web.de","subject":"Re: [PATCH 3/3] diff: remove parseopts member of struct diff_options","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2022-12-01T21:56:27Z","receivedAt":"2022-12-01T21:57:14Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"René Scharfe <l.s.r@web.de> writes:\n\n> Am 01.12.22 um 02:25 schrieb Junio C Hamano:\n>> René Scharfe <l.s.r@web.de> writes:\n>>\n>>> repo_diff_setup() builds the struct option array with git diff's command\n>>> line options and stores a pointer to it in the parseopts member of\n>>> struct diff_options.  The array is freed by diff_setup_done(), but not\n>>> by release_revisions().  repo_init_revisions() calls repo_diff_setup(),\n>>> thus calling repo_init_revisions() then release_revisions() leaks it.\n>>>\n>>> We could free the array in release_revisions() as well to plug that\n>>> leak, but there is a better way: Only build it when needed.  Move the\n>>> get_diff_parseopts() calls to the two places that use the array, free it\n>>> after use and get rid of the parseopts member.\n>>>\n>>> Signed-off-by: René Scharfe <l.s.r@web.de>\n>>> ---\n>>>  diff.c | 17 ++++++++---------\n>>>  1 file changed, 8 insertions(+), 9 deletions(-)\n>>\n>> I think this hunk is missing?\n>\n> Yes. O_o\n\nI did not see any other issue in the series, so if no further tweaks\nare needed, I could just squash it in.\n\nThanks for working on this clean-up.  The way parse-options parser\nwas bolted on (eh, rather, the way the original parser of diff\ncommand line options way predated the parse-options machinery) has\nalways disturbed me as being klunky.\n\n>\n>>\n>>  diff.h | 1 -\n>>  1 file changed, 1 deletion(-)\n>>\n>> diff --git c/diff.h w/diff.h\n>> index 5229f20486..6840499844 100644\n>> --- c/diff.h\n>> +++ w/diff.h\n>> @@ -394,7 +394,6 @@ struct diff_options {\n>>  \tunsigned color_moved_ws_handling;\n>>\n>>  \tstruct repository *repo;\n>> -\tstruct option *parseopts;\n>>  \tstruct strmap *additional_path_headers;\n>>\n>>  \tint no_free;\n"},{"id":"468359","messageId":"fd51f518-0d74-2077-3e6b-4c796489503b@web.de","threadId":"58876","inReplyTo":"xmqqiliuss8k.fsf@gitster.g","subject":"Re: [PATCH 3/3] diff: remove parseopts member of struct diff_options","fromName":"René Scharfe","fromEmail":"l.s.r@web.de","sentAt":"2022-12-01T22:45:28Z","receivedAt":"2022-12-01T22:45:48Z","isPatch":true,"sender":{"key":"l.s.r@web.de","avatar":"https://avatars.githubusercontent.com/u/26122331?v=4"},"body":"Am 01.12.2022 um 22:56 schrieb Junio C Hamano:\n> René Scharfe <l.s.r@web.de> writes:\n>\n>> Am 01.12.22 um 02:25 schrieb Junio C Hamano:\n>>>\n>>> I think this hunk is missing?\n>>\n>> Yes. O_o\n>\n> I did not see any other issue in the series, so if no further tweaks\n> are needed, I could just squash it in.\n\nThank you, but I'm about to send a version 3.\n\nRené\n"},{"id":"468361","messageId":"19325420-5630-87bb-0c2c-362151214b36@web.de","threadId":"58876","inReplyTo":"0620b4a6-b80a-d8a0-5a35-857b9fe0c986@web.de","subject":"[PATCH v3 0/3] diff: build parseopts array on demand","fromName":"René Scharfe","fromEmail":"l.s.r@web.de","sentAt":"2022-12-01T22:45:55Z","receivedAt":"2022-12-01T22:46:47Z","isPatch":true,"sender":{"key":"l.s.r@web.de","avatar":"https://avatars.githubusercontent.com/u/26122331?v=4"},"body":"Calling repo_init_revisions() and release_revisions() in that order\nleaks the memory allocated for the parseopts array in the embedded\nstruct diff_options member.  Get rid of that leak by reducing the\nlifetime of that array.\n\nOriginal patch:\nhttps://lore.kernel.org/git/4fd82dc6-e0f8-0638-5b10-16bfef39a171@web.de/\n\nSubmitted separately from that thread because it's independent enough.\n\nChanges since v2:\n- Inline get_diff_parseopts() to simplify the code.\n\nChange since v1:\n- Actually remove the parseopts member.  Its removal got lost during\n  refactoring in v1.  Thank you for spotting that, Junio!\n\n\n  diff: factor out add_diff_options()\n  diff: use add_diff_options() in diff_opt_parse()\n  diff: remove parseopts member from struct diff_options\n\n builtin/range-diff.c |  2 +-\n diff-no-index.c      |  3 +--\n diff.c               | 19 ++++++++-----------\n diff.h               |  2 +-\n 4 files changed, 11 insertions(+), 15 deletions(-)\n\nInterdiff against v2:\ndiff --git a/diff.c b/diff.c\nindex 6415c4dc2d..4dfe824c85 100644\n--- a/diff.c\n+++ b/diff.c\n@@ -5413,7 +5413,8 @@ static int diff_opt_rotate_to(const struct option *opt, const char *arg, int uns\n \treturn 0;\n }\n\n-static struct option *get_diff_parseopts(struct diff_options *options)\n+struct option *add_diff_options(const struct option *opts,\n+\t\t\t\tstruct diff_options *options)\n {\n \tstruct option parseopts[] = {\n \t\tOPT_GROUP(N_(\"Diff output format options\")),\n@@ -5683,33 +5684,25 @@ static struct option *get_diff_parseopts(struct diff_options *options)\n \t\tOPT_END()\n \t};\n\n-\treturn parse_options_dup(parseopts);\n-}\n-\n-struct option *add_diff_options(const struct option *parseopts,\n-\t\t\t\tstruct diff_options *options)\n-{\n-\tstruct option *diff_parseopts = get_diff_parseopts(options);\n-\tstruct option *result = parse_options_concat(parseopts, diff_parseopts);\n-\tfree(diff_parseopts);\n-\treturn result;\n+\treturn parse_options_concat(opts, parseopts);\n }\n\n int diff_opt_parse(struct diff_options *options,\n \t\t   const char **av, int ac, const char *prefix)\n {\n-\tstruct option *diff_parseopts = get_diff_parseopts(options);\n+\tstruct option no_options[] = { OPT_END() };\n+\tstruct option *parseopts = add_diff_options(no_options, options);\n\n \tif (!prefix)\n \t\tprefix = \"\";\n\n-\tac = parse_options(ac, av, prefix, diff_parseopts, NULL,\n+\tac = parse_options(ac, av, prefix, parseopts, NULL,\n \t\t\t   PARSE_OPT_KEEP_DASHDASH |\n \t\t\t   PARSE_OPT_KEEP_UNKNOWN_OPT |\n \t\t\t   PARSE_OPT_NO_INTERNAL_HELP |\n \t\t\t   PARSE_OPT_ONE_SHOT |\n \t\t\t   PARSE_OPT_STOP_AT_NON_OPTION);\n-\tfree(diff_parseopts);\n+\tfree(parseopts);\n\n \treturn ac;\n }\n--\n2.30.2\n"},{"id":"468362","messageId":"31f0e4ed-4a31-2423-65f0-2f9f1f6c7df3@web.de","threadId":"58876","inReplyTo":"19325420-5630-87bb-0c2c-362151214b36@web.de","subject":"[PATCH v3 1/3] diff: factor out add_diff_options()","fromName":"René Scharfe","fromEmail":"l.s.r@web.de","sentAt":"2022-12-01T22:49:11Z","receivedAt":"2022-12-01T22:50:33Z","isPatch":true,"sender":{"key":"l.s.r@web.de","avatar":"https://avatars.githubusercontent.com/u/26122331?v=4"},"body":"Add a function for appending the parseopts member of struct diff_options\nto a struct option array.  Use it in two sites instead of accessing the\nparseopts member directly.  Decoupling callers from diff internals like\nthat allows us to change the latter.\n\nSigned-off-by: René Scharfe <l.s.r@web.de>\n---\n builtin/range-diff.c | 2 +-\n diff-no-index.c      | 3 +--\n diff.c               | 6 ++++++\n diff.h               | 1 +\n 4 files changed, 9 insertions(+), 3 deletions(-)\n\ndiff --git a/builtin/range-diff.c b/builtin/range-diff.c\nindex e2a74efb42..aecfae12d3 100644\n--- a/builtin/range-diff.c\n+++ b/builtin/range-diff.c\n@@ -47,7 +47,7 @@ int cmd_range_diff(int argc, const char **argv, const char *prefix)\n\n \trepo_diff_setup(the_repository, &diffopt);\n\n-\toptions = parse_options_concat(range_diff_options, diffopt.parseopts);\n+\toptions = add_diff_options(range_diff_options, &diffopt);\n \targc = parse_options(argc, argv, prefix, options,\n \t\t\t     builtin_range_diff_usage, PARSE_OPT_KEEP_DASHDASH);\n\ndiff --git a/diff-no-index.c b/diff-no-index.c\nindex 18edbdf4b5..05fafd0019 100644\n--- a/diff-no-index.c\n+++ b/diff-no-index.c\n@@ -255,8 +255,7 @@ int diff_no_index(struct rev_info *revs,\n \t};\n \tstruct option *options;\n\n-\toptions = parse_options_concat(no_index_options,\n-\t\t\t\t       revs->diffopt.parseopts);\n+\toptions = add_diff_options(no_index_options, &revs->diffopt);\n \targc = parse_options(argc, argv, revs->prefix, options,\n \t\t\t     diff_no_index_usage, 0);\n \tif (argc != 2) {\ndiff --git a/diff.c b/diff.c\nindex 1054a4b732..f1cf13e8e7 100644\n--- a/diff.c\n+++ b/diff.c\n@@ -5419,6 +5419,12 @@ static int diff_opt_rotate_to(const struct option *opt, const char *arg, int uns\n \treturn 0;\n }\n\n+struct option *add_diff_options(const struct option *opts,\n+\t\t\t\tstruct diff_options *options)\n+{\n+\treturn parse_options_concat(opts, options->parseopts);\n+}\n+\n static void prep_parse_options(struct diff_options *options)\n {\n \tstruct option parseopts[] = {\ndiff --git a/diff.h b/diff.h\nindex fd33caeb25..c20a1ad76d 100644\n--- a/diff.h\n+++ b/diff.h\n@@ -539,6 +539,7 @@ int git_diff_ui_config(const char *var, const char *value, void *cb);\n #define diff_setup(diffopts) repo_diff_setup(the_repository, diffopts)\n #endif\n void repo_diff_setup(struct repository *, struct diff_options *);\n+struct option *add_diff_options(const struct option *, struct diff_options *);\n int diff_opt_parse(struct diff_options *, const char **, int, const char *);\n void diff_setup_done(struct diff_options *);\n int git_config_rename(const char *var, const char *value);\n--\n2.30.2\n"},{"id":"468363","messageId":"fb901635-8058-856d-c7cd-6fef24329f62@web.de","threadId":"58876","inReplyTo":"19325420-5630-87bb-0c2c-362151214b36@web.de","subject":"[PATCH v3 2/3] diff: use add_diff_options() in diff_opt_parse()","fromName":"René Scharfe","fromEmail":"l.s.r@web.de","sentAt":"2022-12-01T22:51:21Z","receivedAt":"2022-12-01T22:52:42Z","isPatch":true,"sender":{"key":"l.s.r@web.de","avatar":"https://avatars.githubusercontent.com/u/26122331?v=4"},"body":"Prepare the removal of the parseopts member of struct diff_options by\nusing the API function add_diff_options() instead of accessing it\ndirectly to get the command line option definitions.  Building the copy\nby concatenating with an empty option array is slightly awkward, but\nsimpler than a non-concat version of add_diff_options() would be to use\nin places that need concatenation.\n\nSuggested-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>\nSigned-off-by: René Scharfe <l.s.r@web.de>\n---\n diff.c | 6 +++++-\n 1 file changed, 5 insertions(+), 1 deletion(-)\n\ndiff --git a/diff.c b/diff.c\nindex f1cf13e8e7..f6d85122a9 100644\n--- a/diff.c\n+++ b/diff.c\n@@ -5702,15 +5702,19 @@ static void prep_parse_options(struct diff_options *options)\n int diff_opt_parse(struct diff_options *options,\n \t\t   const char **av, int ac, const char *prefix)\n {\n+\tstruct option no_options[] = { OPT_END() };\n+\tstruct option *parseopts = add_diff_options(no_options, options);\n+\n \tif (!prefix)\n \t\tprefix = \"\";\n\n-\tac = parse_options(ac, av, prefix, options->parseopts, NULL,\n+\tac = parse_options(ac, av, prefix, parseopts, NULL,\n \t\t\t   PARSE_OPT_KEEP_DASHDASH |\n \t\t\t   PARSE_OPT_KEEP_UNKNOWN_OPT |\n \t\t\t   PARSE_OPT_NO_INTERNAL_HELP |\n \t\t\t   PARSE_OPT_ONE_SHOT |\n \t\t\t   PARSE_OPT_STOP_AT_NON_OPTION);\n+\tfree(parseopts);\n\n \treturn ac;\n }\n--\n2.30.2\n"},{"id":"468364","messageId":"3816dd61-ee61-94b4-a2b2-49ee07986c5b@web.de","threadId":"58876","inReplyTo":"19325420-5630-87bb-0c2c-362151214b36@web.de","subject":"[PATCH v3 3/3] diff: remove parseopts member from struct diff_options","fromName":"René Scharfe","fromEmail":"l.s.r@web.de","sentAt":"2022-12-01T22:53:17Z","receivedAt":"2022-12-01T22:53:32Z","isPatch":true,"sender":{"key":"l.s.r@web.de","avatar":"https://avatars.githubusercontent.com/u/26122331?v=4"},"body":"repo_diff_setup() builds the struct option array with git diff's command\nline options and stores a pointer to it in the parseopts member of\nstruct diff_options.  The array is freed by diff_setup_done(), but not\nby release_revisions().  Thus calling only repo_diff_setup() and\nrelease_revisions() leaks that array.\n\nWe could free it in release_revisions() as well to plug that leak, but\nthere is a better way: Only build it when needed.  Absorb\nprep_parse_options() into the last place that uses the parseopts member\nof struct diff_options, add_diff_parseopts(), and get rid of said\nmember.\n\nSigned-off-by: René Scharfe <l.s.r@web.de>\n---\n diff.c | 15 +--------------\n diff.h |  1 -\n 2 files changed, 1 insertion(+), 15 deletions(-)\n\ndiff --git a/diff.c b/diff.c\nindex f6d85122a9..4dfe824c85 100644\n--- a/diff.c\n+++ b/diff.c\n@@ -4615,8 +4615,6 @@ static void run_checkdiff(struct diff_filepair *p, struct diff_options *o)\n \tbuiltin_checkdiff(name, other, attr_path, p->one, p->two, o);\n }\n\n-static void prep_parse_options(struct diff_options *options);\n-\n void repo_diff_setup(struct repository *r, struct diff_options *options)\n {\n \tmemcpy(options, &default_diff_options, sizeof(*options));\n@@ -4662,8 +4660,6 @@ void repo_diff_setup(struct repository *r, struct diff_options *options)\n\n \toptions->color_moved = diff_color_moved_default;\n \toptions->color_moved_ws_handling = diff_color_moved_ws_default;\n-\n-\tprep_parse_options(options);\n }\n\n static const char diff_status_letters[] = {\n@@ -4821,8 +4817,6 @@ void diff_setup_done(struct diff_options *options)\n \t\t\toptions->filter = ~filter_bit[DIFF_STATUS_FILTER_AON];\n \t\toptions->filter &= ~options->filter_not;\n \t}\n-\n-\tFREE_AND_NULL(options->parseopts);\n }\n\n int parse_long_opt(const char *opt, const char **argv,\n@@ -5421,11 +5415,6 @@ static int diff_opt_rotate_to(const struct option *opt, const char *arg, int uns\n\n struct option *add_diff_options(const struct option *opts,\n \t\t\t\tstruct diff_options *options)\n-{\n-\treturn parse_options_concat(opts, options->parseopts);\n-}\n-\n-static void prep_parse_options(struct diff_options *options)\n {\n \tstruct option parseopts[] = {\n \t\tOPT_GROUP(N_(\"Diff output format options\")),\n@@ -5695,8 +5684,7 @@ static void prep_parse_options(struct diff_options *options)\n \t\tOPT_END()\n \t};\n\n-\tALLOC_ARRAY(options->parseopts, ARRAY_SIZE(parseopts));\n-\tmemcpy(options->parseopts, parseopts, sizeof(parseopts));\n+\treturn parse_options_concat(opts, parseopts);\n }\n\n int diff_opt_parse(struct diff_options *options,\n@@ -6523,7 +6511,6 @@ void diff_free(struct diff_options *options)\n \tdiff_free_file(options);\n \tdiff_free_ignore_regex(options);\n \tclear_pathspec(&options->pathspec);\n-\tFREE_AND_NULL(options->parseopts);\n }\n\n void diff_flush(struct diff_options *options)\ndiff --git a/diff.h b/diff.h\nindex c20a1ad76d..41eb2c3d42 100644\n--- a/diff.h\n+++ b/diff.h\n@@ -394,7 +394,6 @@ struct diff_options {\n \tunsigned color_moved_ws_handling;\n\n \tstruct repository *repo;\n-\tstruct option *parseopts;\n \tstruct strmap *additional_path_headers;\n\n \tint no_free;\n--\n2.30.2\n"},{"id":"468367","messageId":"221202.86iliuhgk4.gmgdl@evledraar.gmail.com","threadId":"58876","inReplyTo":"572e29a2-3f19-e946-d152-009999651084@web.de","subject":"Re: [PATCH v2 0/3] diff: build parseopts array on demand","fromName":"Ævar Arnfjörð Bjarmason","fromEmail":"avarab@gmail.com","sentAt":"2022-12-01T23:00:17Z","receivedAt":"2022-12-01T23:04:23Z","isPatch":true,"sender":{"key":"avarab@gmail.com","avatar":"https://avatars.githubusercontent.com/u/45301?v=4"},"body":"\nOn Thu, Dec 01 2022, René Scharfe wrote:\n\n> Am 01.12.2022 um 17:54 schrieb Ævar Arnfjörð Bjarmason:\n>>\n>> On Thu, Dec 01 2022, René Scharfe wrote:\n>>\n>>> Calling repo_init_revisions() and release_revisions() in that order\n>>> leaks the memory allocated for the parseopts array in the embedded\n>>> struct diff_options member.  Get rid of that leak by reducing the\n>>> lifetime of that array.\n>>>\n>>> Original patch:\n>>> https://lore.kernel.org/git/4fd82dc6-e0f8-0638-5b10-16bfef39a171@web.de/\n>>>\n>>> Submitted separately from that thread because it's independent enough.\n>>>\n>>> Change since v1:\n>>> - Actually remove the parseopts member.  Its removal got lost during\n>>>   refactoring in v1.  Thank you for spotting that, Junio!\n>>>\n>>>   diff: factor out add_diff_options()\n>>>   diff: let prep_parse_options() return parseopt array\n>>>   diff: remove parseopts member from struct diff_options\n>>>\n>>>  builtin/range-diff.c |  2 +-\n>>>  diff-no-index.c      |  3 +--\n>>>  diff.c               | 26 +++++++++++++++-----------\n>>>  diff.h               |  2 +-\n>>>  4 files changed, 18 insertions(+), 15 deletions(-)\n>>>\n>>> Range-Diff gegen v1:\n>>> 1:  630f95320f = 1:  4dc8b2632b diff: factor out add_diff_options()\n>>> 2:  4b56fa795c = 2:  10903d355e diff: let prep_parse_options() return parseopt array\n>>> 3:  7e54e4370a ! 3:  24bd18ae79 diff: remove parseopts member from struct diff_options\n>>>     @@ diff.c: void diff_free(struct diff_options *options)\n>>>       }\n>>>\n>>>       void diff_flush(struct diff_options *options)\n>>>     +\n>>>     + ## diff.h ##\n>>>     +@@ diff.h: struct diff_options {\n>>>     + \tunsigned color_moved_ws_handling;\n>>>     +\n>>>     + \tstruct repository *repo;\n>>>     +-\tstruct option *parseopts;\n>>>     + \tstruct strmap *additional_path_headers;\n>>>     +\n>>>     + \tint no_free;\n>>\n>> This looks good to me. Would you mind running the tests with:\n>>\n>> \tGIT_TEST_PASSING_SANITIZE_LEAK=check GIT_TEST_SANITIZE_LEAK_LOG=true make SANITIZE=leak\n>>\n>> And then marking up the ones that now pass with\n>> TEST_PASSES_SANITIZE_LEAK=true. I think it's all except one of these\n>> (one isn't marked on \"master\", I forget which one):\n>>\n>> \tTest Summary Report\n>> \t-------------------\n>> \tt1022-read-tree-partial-clone.sh                 (Wstat: 256 Tests: 1 Failed: 0)\n>> \t  Non-zero exit status: 1\n>> \tt2012-checkout-last.sh                           (Wstat: 256 Tests: 22 Failed: 0)\n>> \t  Non-zero exit status: 1\n>> \tt3210-pack-refs.sh                               (Wstat: 256 Tests: 30 Failed: 0)\n>> \t  Non-zero exit status: 1\n>> \tt4053-diff-no-index.sh                           (Wstat: 256 Tests: 19 Failed: 0)\n>> \t  Non-zero exit status: 1\n>> \tt5554-noop-fetch-negotiator.sh                   (Wstat: 256 Tests: 1 Failed: 0)\n>> \t  Non-zero exit status: 1\n>> \tt5613-info-alternate.sh                          (Wstat: 256 Tests: 13 Failed: 0)\n>> \t  Non-zero exit status: 1\n>> \tt6021-rev-list-exclude-hidden.sh                 (Wstat: 256 Tests: 42 Failed: 0)\n>> \t  Non-zero exit status: 1\n>> \tt6415-merge-dir-to-symlink.sh                    (Wstat: 256 Tests: 24 Failed: 0)\n>> \t  Non-zero exit status: 1\n>> \tt7403-submodule-sync.sh                          (Wstat: 256 Tests: 18 Failed: 0)\n>> \t  Non-zero exit status: 1\n>> \tt7504-commit-msg-hook.sh                         (Wstat: 256 Tests: 30 Failed: 0)\n>> \t  Non-zero exit status: 1\n>> \tt9115-git-svn-dcommit-funky-renames.sh           (Wstat: 256 Tests: 12 Failed: 0)\n>> \t  Non-zero exit status: 1\n>> \tt9146-git-svn-empty-dirs.sh                      (Wstat: 256 Tests: 14 Failed: 0)\n>> \t  Non-zero exit status: 1\n>> \tt9160-git-svn-preserve-empty-dirs.sh             (Wstat: 256 Tests: 12 Failed: 0)\n>> \t  Non-zero exit status: 1\n>>\n>> I.e. this makes a lot more tests pass leak-free, yay!\n>\n> With -rc1 (i.e. without this series) I get:\n>\n> t1022-read-tree-partial-clone.sh                 (Wstat: 256 Tests: 1 Failed: 0)\n>   Non-zero exit status: 1\n> t2016-checkout-patch.sh                          (Wstat: 256 Tests: 16 Failed: 0)\n>   Non-zero exit status: 1\n> t2012-checkout-last.sh                           (Wstat: 256 Tests: 22 Failed: 0)\n>   Non-zero exit status: 1\n> t4023-diff-rename-typechange.sh                  (Wstat: 256 Tests: 4 Failed: 0)\n>   Non-zero exit status: 1\n> t4053-diff-no-index.sh                           (Wstat: 256 Tests: 19 Failed: 0)\n>   Non-zero exit status: 1\n> t4058-diff-duplicates.sh                         (Wstat: 256 Tests: 16 Failed: 0)\n>   Non-zero exit status: 1\n> t4205-log-pretty-formats.sh                      (Wstat: 256 Tests: 21 Failed: 0)\n>   Non-zero exit status: 1\n>   Parse errors: No plan found in TAP output\n> t5406-remote-rejects.sh                          (Wstat: 256 Tests: 3 Failed: 0)\n>   Non-zero exit status: 1\n> t5507-remote-environment.sh                      (Wstat: 256 Tests: 5 Failed: 0)\n>   Non-zero exit status: 1\n> t5554-noop-fetch-negotiator.sh                   (Wstat: 256 Tests: 1 Failed: 0)\n>   Non-zero exit status: 1\n> t6001-rev-list-graft.sh                          (Wstat: 256 Tests: 14 Failed: 0)\n>   Non-zero exit status: 1\n> t6021-rev-list-exclude-hidden.sh                 (Wstat: 256 Tests: 42 Failed: 0)\n>   Non-zero exit status: 1\n> t6401-merge-criss-cross.sh                       (Wstat: 256 Tests: 4 Failed: 0)\n>   Non-zero exit status: 1\n> t6407-merge-binary.sh                            (Wstat: 256 Tests: 3 Failed: 0)\n>   Non-zero exit status: 1\n> t6415-merge-dir-to-symlink.sh                    (Wstat: 256 Tests: 24 Failed: 0)\n>   Non-zero exit status: 1\n> t7006-pager.sh                                   (Wstat: 256 Tests: 109 Failed: 0)\n>   Non-zero exit status: 1\n> t7008-filter-branch-null-sha1.sh                 (Wstat: 256 Tests: 6 Failed: 0)\n>   Non-zero exit status: 1\n> t7504-commit-msg-hook.sh                         (Wstat: 256 Tests: 30 Failed: 0)\n>   Non-zero exit status: 1\n> t7517-per-repo-email.sh                          (Wstat: 256 Tests: 16 Failed: 0)\n>   Non-zero exit status: 1\n> t7605-merge-resolve.sh                           (Wstat: 256 Tests: 4 Failed: 0)\n>   Non-zero exit status: 1\n>\n> There is some overlap with your results, but also several differences.\n> I wonder why so many more tests appear to be leak-free for me.  I used\n> Debian clang version 11.0.1-2.\n\nSorry to have sent you down this rabbit hole, I misrecalled that it was\nrelatively clean on \"master\", but it's \"next\" that's in that state (due\nto ab/various-leak-fixes).\n\n> In any case it seems we need to update the marks before we can\n> attribute which tests are made leak-free by any new patches.\n\nYes, let's leave it for now in this series. I think it would make sense\nif \"master\" was almost clean, but I'll just submit another change like\ne5e37517dd9 (tests: mark tests as passing with SANITIZE=leak,\n2022-11-08) after this lands.\n"}]}