{"thread":{"id":"65173","subject":"[PATCH] dir: avoid -Wdiscarded-qualifiers in remove_path()","startedAt":"2026-03-09T03:23:37Z","lastAt":"2026-03-10T00:57:25Z","messageCount":4,"participants":["Collin Funk","Junio C Hamano"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"538250","messageId":"3ad40c3d0762c2e8c14792dfb68cba9f63a883a3.1773026586.git.collin.funk1@gmail.com","threadId":"65173","inReplyTo":null,"subject":"[PATCH] dir: avoid -Wdiscarded-qualifiers in remove_path()","fromName":"Collin Funk","fromEmail":"collin.funk1@gmail.com","sentAt":"2026-03-09T03:23:06Z","receivedAt":"2026-03-09T03:23:37Z","isPatch":true,"sender":{"key":"collin.funk1@gmail.com","avatar":"https://avatars.githubusercontent.com/u/65689063?v=4"},"body":"When building with glibc-2.43 there is the following warning:\n\n    dir.c:3526:15: warning: assignment discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]\n     3526 |         slash = strrchr(name, '/');\n          |               ^\n\nIn this case we use a non-const pointer to get the last slash of the\nunwritable file name, and then use it again to write in the strdup'd\nfile name.\n\nWe can avoid this warning and make the code a bit more clear by using a\nseparate variable to access the original argument and it's strdup'd\ncopy.\n\nSigned-off-by: Collin Funk <collin.funk1@gmail.com>\n---\n dir.c | 8 ++++----\n 1 file changed, 4 insertions(+), 4 deletions(-)\n\ndiff --git a/dir.c b/dir.c\nindex 026d8516a9..fcb8f6dd2a 100644\n--- a/dir.c\n+++ b/dir.c\n@@ -3518,15 +3518,15 @@ int get_sparse_checkout_patterns(struct pattern_list *pl)\n \n int remove_path(const char *name)\n {\n-\tchar *slash;\n+\tconst char *last;\n \n \tif (unlink(name) && !is_missing_file_error(errno))\n \t\treturn -1;\n \n-\tslash = strrchr(name, '/');\n-\tif (slash) {\n+\tlast = strrchr(name, '/');\n+\tif (last) {\n \t\tchar *dirs = xstrdup(name);\n-\t\tslash = dirs + (slash - name);\n+\t\tchar *slash = dirs + (last - name);\n \t\tdo {\n \t\t\t*slash = '\\0';\n \t\t\tif (startup_info->original_cwd &&\n-- \n2.53.0\n\n"},{"id":"538281","messageId":"xmqqbjgx6obs.fsf@gitster.g","threadId":"65173","inReplyTo":"3ad40c3d0762c2e8c14792dfb68cba9f63a883a3.1773026586.git.collin.funk1@gmail.com","subject":"Re: [PATCH] dir: avoid -Wdiscarded-qualifiers in remove_path()","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-03-09T14:59:35Z","receivedAt":"2026-03-09T14:59:38Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Collin Funk <collin.funk1@gmail.com> writes:\n\n> When building with glibc-2.43 there is the following warning:\n>\n>     dir.c:3526:15: warning: assignment discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]\n>      3526 |         slash = strrchr(name, '/');\n>           |               ^\n>\n> In this case we use a non-const pointer to get the last slash of the\n> unwritable file name, and then use it again to write in the strdup'd\n> file name.\n>\n> We can avoid this warning and make the code a bit more clear by using a\n> separate variable to access the original argument and it's strdup'd\n> copy.\n\n\"it's\" -> \"its\", if I am reading the above correctly?\n\n>\n> Signed-off-by: Collin Funk <collin.funk1@gmail.com>\n> ---\n>  dir.c | 8 ++++----\n>  1 file changed, 4 insertions(+), 4 deletions(-)\n\nThe changes make sense.  Thanks.\n\n> diff --git a/dir.c b/dir.c\n> index 026d8516a9..fcb8f6dd2a 100644\n> --- a/dir.c\n> +++ b/dir.c\n> @@ -3518,15 +3518,15 @@ int get_sparse_checkout_patterns(struct pattern_list *pl)\n>  \n>  int remove_path(const char *name)\n>  {\n> -\tchar *slash;\n> +\tconst char *last;\n>  \n>  \tif (unlink(name) && !is_missing_file_error(errno))\n>  \t\treturn -1;\n>  \n> -\tslash = strrchr(name, '/');\n> -\tif (slash) {\n> +\tlast = strrchr(name, '/');\n> +\tif (last) {\n>  \t\tchar *dirs = xstrdup(name);\n> -\t\tslash = dirs + (slash - name);\n> +\t\tchar *slash = dirs + (last - name);\n>  \t\tdo {\n>  \t\t\t*slash = '\\0';\n>  \t\t\tif (startup_info->original_cwd &&\n"},{"id":"538342","messageId":"87a4wgwn18.fsf@gmail.com","threadId":"65173","inReplyTo":"xmqqbjgx6obs.fsf@gitster.g","subject":"Re: [PATCH] dir: avoid -Wdiscarded-qualifiers in remove_path()","fromName":"Collin Funk","fromEmail":"collin.funk1@gmail.com","sentAt":"2026-03-10T00:22:59Z","receivedAt":"2026-03-10T00:23:02Z","isPatch":true,"sender":{"key":"collin.funk1@gmail.com","avatar":"https://avatars.githubusercontent.com/u/65689063?v=4"},"body":"Junio C Hamano <gitster@pobox.com> writes:\n\n> Collin Funk <collin.funk1@gmail.com> writes:\n>\n>> When building with glibc-2.43 there is the following warning:\n>>\n>>     dir.c:3526:15: warning: assignment discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]\n>>      3526 |         slash = strrchr(name, '/');\n>>           |               ^\n>>\n>> In this case we use a non-const pointer to get the last slash of the\n>> unwritable file name, and then use it again to write in the strdup'd\n>> file name.\n>>\n>> We can avoid this warning and make the code a bit more clear by using a\n>> separate variable to access the original argument and it's strdup'd\n>> copy.\n>\n> \"it's\" -> \"its\", if I am reading the above correctly?\n\nYep, my mistake. Assuming you can fix that locally?\n\nThanks,\nCollin\n"},{"id":"538344","messageId":"CAPc5daXDPLYnmtgk3M+B4qOPVU7vWwP3A8jqNe6ex3WaVZMufA@mail.gmail.com","threadId":"65173","inReplyTo":"87a4wgwn18.fsf@gmail.com","subject":"Re: [PATCH] dir: avoid -Wdiscarded-qualifiers in remove_path()","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-03-10T00:57:08Z","receivedAt":"2026-03-10T00:57:25Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"> > \"it's\" -> \"its\", if I am reading the above correctly?\n>\n> Yep, my mistake. Assuming you can fix that locally?\n\nSure, will do.\n"}]}