{"thread":{"id":"58295","subject":"[PATCH 0/1] pathspec: fix memleak","startedAt":"2022-08-12T08:17:51Z","lastAt":"2022-08-12T17:41:20Z","messageCount":3,"participants":["Anthony Delannoy","Junio C Hamano"],"isPatch":true,"patchVersion":1,"patchTotal":1},"messages":[{"id":"461111","messageId":"20220812081744.456280-1-anthony.2lannoy@gmail.com","threadId":"58295","inReplyTo":null,"subject":"[PATCH 0/1] pathspec: fix memleak","fromName":"Anthony Delannoy","fromEmail":"anthony.2lannoy@gmail.com","sentAt":"2022-08-12T08:17:43Z","receivedAt":"2022-08-12T08:17:51Z","isPatch":true,"sender":{"key":"anthony.2lannoy@gmail.com","avatar":"https://avatars.githubusercontent.com/u/32701669?v=4"},"body":"Sending this patch to fix a memleak, created with copy_pathspec, found\nthanks to the address sanitizer.\n\nThanks\n\nAnthony Delannoy (1):\n  pathspec: fix memleak\n\n preload-index.c | 3 +++\n 1 file changed, 3 insertions(+)\n\n\nbase-commit: 5502f77b6944eda8e26813d8f542cffe7d110aea\n--\n2.35.1\n"},{"id":"461112","messageId":"20220812081744.456280-2-anthony.2lannoy@gmail.com","threadId":"58295","inReplyTo":"20220812081744.456280-1-anthony.2lannoy@gmail.com","subject":"[PATCH 1/1] pathspec: fix memleak","fromName":"Anthony Delannoy","fromEmail":"anthony.2lannoy@gmail.com","sentAt":"2022-08-12T08:17:44Z","receivedAt":"2022-08-12T08:17:59Z","isPatch":true,"sender":{"key":"anthony.2lannoy@gmail.com","avatar":"https://avatars.githubusercontent.com/u/32701669?v=4"},"body":"Fix a memory leak occuring in case of pathspec copy in preload_index.\n\nDirect leak of 8 byte(s) in 8 object(s) allocated from:\n    #0 0x7f0a353ead47 in __interceptor_malloc (/usr/lib/gcc/x86_64-pc-linux-gnu/11.3.0/libasan.so.6+0xb5d47)\n    #1 0x55750995e840 in do_xmalloc /home/anthony/src/c/git/wrapper.c:51\n    #2 0x55750995e840 in xmalloc /home/anthony/src/c/git/wrapper.c:72\n    #3 0x55750970f824 in copy_pathspec /home/anthony/src/c/git/pathspec.c:684\n    #4 0x557509717278 in preload_index /home/anthony/src/c/git/preload-index.c:135\n    #5 0x55750975f21e in refresh_index /home/anthony/src/c/git/read-cache.c:1633\n    #6 0x55750915b926 in cmd_status builtin/commit.c:1547\n    #7 0x5575090e1680 in run_builtin /home/anthony/src/c/git/git.c:466\n    #8 0x5575090e1680 in handle_builtin /home/anthony/src/c/git/git.c:720\n    #9 0x5575090e284a in run_argv /home/anthony/src/c/git/git.c:787\n    #10 0x5575090e284a in cmd_main /home/anthony/src/c/git/git.c:920\n    #11 0x5575090dbf82 in main /home/anthony/src/c/git/common-main.c:56\n    #12 0x7f0a348230ab  (/lib64/libc.so.6+0x290ab)\n\nSigned-off-by: Anthony Delannoy <anthony.2lannoy@gmail.com>\n---\n preload-index.c | 3 +++\n 1 file changed, 3 insertions(+)\n\ndiff --git a/preload-index.c b/preload-index.c\nindex e5529a5863..a05f4d1390 100644\n--- a/preload-index.c\n+++ b/preload-index.c\n@@ -148,6 +148,9 @@ void preload_index(struct index_state *index,\n \t\tif (pthread_join(p->pthread, NULL))\n \t\t\tdie(\"unable to join threaded lstat\");\n \t\tt2_sum_lstat += p->t2_nr_lstat;\n+\n+\t\tif (pathspec)\n+\t\t\tfree(p->pathspec.items);\n \t}\n \tstop_progress(&pd.progress);\n \n-- \n2.35.1\n\n"},{"id":"461128","messageId":"xmqqsfm1bbrf.fsf@gitster.g","threadId":"58295","inReplyTo":"20220812081744.456280-2-anthony.2lannoy@gmail.com","subject":"Re: [PATCH 1/1] pathspec: fix memleak","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2022-08-12T17:41:08Z","receivedAt":"2022-08-12T17:41:20Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Anthony Delannoy <anthony.2lannoy@gmail.com> writes:\n\n> diff --git a/preload-index.c b/preload-index.c\n> index e5529a5863..a05f4d1390 100644\n> --- a/preload-index.c\n> +++ b/preload-index.c\n> @@ -148,6 +148,9 @@ void preload_index(struct index_state *index,\n>  \t\tif (pthread_join(p->pthread, NULL))\n>  \t\t\tdie(\"unable to join threaded lstat\");\n>  \t\tt2_sum_lstat += p->t2_nr_lstat;\n> +\n> +\t\tif (pathspec)\n> +\t\t\tfree(p->pathspec.items);\n>  \t}\n>  \tstop_progress(&pd.progress);\n\nGiven the way how copy_pathspec() makes a deep copy of a pathspec, I\nsuspect that this is still leaking all the resources held by the\narray that is freed here.  Let's take a look:\n\n        void copy_pathspec(struct pathspec *dst, const struct pathspec *src)\n        {\n                int i, j;\n\n                *dst = *src;\n                ALLOC_ARRAY(dst->items, dst->nr);\n                COPY_ARRAY(dst->items, src->items, dst->nr);\n\nHere, we copy the array of \"struct pathspec_item\".  But that is not\nenough because ...\n\n                for (i = 0; i < dst->nr; i++) {\n                        struct pathspec_item *d = &dst->items[i];\n                        struct pathspec_item *s = &src->items[i];\n\n                        d->match = xstrdup(s->match);\n                        d->original = xstrdup(s->original);\n\n... each \"struct pathspec_item\" instance has pointer members like\nthese, and the copying of the array made these strings shared\nbetween the src and dst arrays.  Here we make a copy of the string\nowned by the element in the src array and give the copy to the\nelement in the dst array.\n\n                        ALLOC_ARRAY(d->attr_match, d->attr_match_nr);\n                        COPY_ARRAY(d->attr_match, s->attr_match, d->attr_match_nr);\n\nLikewise for a separate array pointed by a member in \"struct\npathspec_item\" ...\n\n                        for (j = 0; j < d->attr_match_nr; j++) {\n                                const char *value = s->attr_match[j].value;\n                                d->attr_match[j].value = xstrdup_or_null(value);\n\n... which has a pointer member here ...\n\n                        }\n\n                        d->attr_check = attr_check_dup(s->attr_check);\n\n... and here.  Both are deep-copied.\n\n                }\n        }\n\nThere is pathspec.c::clear_pathspec() API function, which looks as\nif it was made for this exact use case.\n\nI wonder if this is a good place to use it, perhaps like the\nattached patch.\n\n preload-index.c | 6 ++++++\n 1 file changed, 6 insertions(+)\n\ndiff --git c/preload-index.c w/preload-index.c\nindex e5529a5863..100f7a374d 100644\n--- c/preload-index.c\n+++ w/preload-index.c\n@@ -151,6 +151,12 @@ void preload_index(struct index_state *index,\n \t}\n \tstop_progress(&pd.progress);\n \n+\tif (pathspec) {\n+\t\t/* earlier we made deep copies for each thread to work with */\n+\t\tfor (i = 0; i < threads; i++)\n+\t\t\tclear_pathspec(&data[i].pathspec);\n+\t}\n+\n \ttrace_performance_leave(\"preload index\");\n \n \ttrace2_data_intmax(\"index\", NULL, \"preload/sum_lstat\", t2_sum_lstat);\n"}]}