# [PATCH] dir: avoid -Wdiscarded-qualifiers in remove_path()

4 messages from 2026-03-09 to 2026-03-10. Participants: Collin Funk, Junio C Hamano.
Thread: https://gitlist.dev/t/65173

## Collin Funk, 2026-03-09 03:23

Subject: [PATCH] dir: avoid -Wdiscarded-qualifiers in remove_path()
Message-ID: <3ad40c3d0762c2e8c14792dfb68cba9f63a883a3.1773026586.git.collin.funk1@gmail.com>
URL: https://gitlist.dev/e/3ad40c3d0762c2e8c14792dfb68cba9f63a883a3.1773026586.git.collin.funk1%40gmail.com

```
When building with glibc-2.43 there is the following warning:

    dir.c:3526:15: warning: assignment discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]
     3526 |         slash = strrchr(name, '/');
          |               ^

In this case we use a non-const pointer to get the last slash of the
unwritable file name, and then use it again to write in the strdup'd
file name.

We can avoid this warning and make the code a bit more clear by using a
separate variable to access the original argument and it's strdup'd
copy.

Signed-off-by: Collin Funk <collin.funk1@gmail.com>
---
 dir.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/dir.c b/dir.c
index 026d8516a9..fcb8f6dd2a 100644
--- a/dir.c
+++ b/dir.c
@@ -3518,15 +3518,15 @@ int get_sparse_checkout_patterns(struct pattern_list *pl)
 
 int remove_path(const char *name)
 {
-	char *slash;
+	const char *last;
 
 	if (unlink(name) && !is_missing_file_error(errno))
 		return -1;
 
-	slash = strrchr(name, '/');
-	if (slash) {
+	last = strrchr(name, '/');
+	if (last) {
 		char *dirs = xstrdup(name);
-		slash = dirs + (slash - name);
+		char *slash = dirs + (last - name);
 		do {
 			*slash = '\0';
 			if (startup_info->original_cwd &&
-- 
2.53.0


```

## Junio C Hamano, 2026-03-09 14:59

Subject: Re: [PATCH] dir: avoid -Wdiscarded-qualifiers in remove_path()
Message-ID: <xmqqbjgx6obs.fsf@gitster.g>
URL: https://gitlist.dev/e/xmqqbjgx6obs.fsf%40gitster.g
In-Reply-To: <3ad40c3d0762c2e8c14792dfb68cba9f63a883a3.1773026586.git.collin.funk1@gmail.com>

```
Collin Funk <collin.funk1@gmail.com> writes:

> When building with glibc-2.43 there is the following warning:
>
>     dir.c:3526:15: warning: assignment discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]
>      3526 |         slash = strrchr(name, '/');
>           |               ^
>
> In this case we use a non-const pointer to get the last slash of the
> unwritable file name, and then use it again to write in the strdup'd
> file name.
>
> We can avoid this warning and make the code a bit more clear by using a
> separate variable to access the original argument and it's strdup'd
> copy.

"it's" -> "its", if I am reading the above correctly?

>
> Signed-off-by: Collin Funk <collin.funk1@gmail.com>
> ---
>  dir.c | 8 ++++----
>  1 file changed, 4 insertions(+), 4 deletions(-)

The changes make sense.  Thanks.

> diff --git a/dir.c b/dir.c
> index 026d8516a9..fcb8f6dd2a 100644
> --- a/dir.c
> +++ b/dir.c
> @@ -3518,15 +3518,15 @@ int get_sparse_checkout_patterns(struct pattern_list *pl)
>  
>  int remove_path(const char *name)
>  {
> -	char *slash;
> +	const char *last;
>  
>  	if (unlink(name) && !is_missing_file_error(errno))
>  		return -1;
>  
> -	slash = strrchr(name, '/');
> -	if (slash) {
> +	last = strrchr(name, '/');
> +	if (last) {
>  		char *dirs = xstrdup(name);
> -		slash = dirs + (slash - name);
> +		char *slash = dirs + (last - name);
>  		do {
>  			*slash = '\0';
>  			if (startup_info->original_cwd &&

```

## Collin Funk, 2026-03-10 00:22

Subject: Re: [PATCH] dir: avoid -Wdiscarded-qualifiers in remove_path()
Message-ID: <87a4wgwn18.fsf@gmail.com>
URL: https://gitlist.dev/e/87a4wgwn18.fsf%40gmail.com
In-Reply-To: <xmqqbjgx6obs.fsf@gitster.g>

```
Junio C Hamano <gitster@pobox.com> writes:

> Collin Funk <collin.funk1@gmail.com> writes:
>
>> When building with glibc-2.43 there is the following warning:
>>
>>     dir.c:3526:15: warning: assignment discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]
>>      3526 |         slash = strrchr(name, '/');
>>           |               ^
>>
>> In this case we use a non-const pointer to get the last slash of the
>> unwritable file name, and then use it again to write in the strdup'd
>> file name.
>>
>> We can avoid this warning and make the code a bit more clear by using a
>> separate variable to access the original argument and it's strdup'd
>> copy.
>
> "it's" -> "its", if I am reading the above correctly?

Yep, my mistake. Assuming you can fix that locally?

Thanks,
Collin

```

## Junio C Hamano, 2026-03-10 00:57

Subject: Re: [PATCH] dir: avoid -Wdiscarded-qualifiers in remove_path()
Message-ID: <CAPc5daXDPLYnmtgk3M+B4qOPVU7vWwP3A8jqNe6ex3WaVZMufA@mail.gmail.com>
URL: https://gitlist.dev/e/CAPc5daXDPLYnmtgk3M%2BB4qOPVU7vWwP3A8jqNe6ex3WaVZMufA%40mail.gmail.com
In-Reply-To: <87a4wgwn18.fsf@gmail.com>

```
> > "it's" -> "its", if I am reading the above correctly?
>
> Yep, my mistake. Assuming you can fix that locally?

Sure, will do.

```
