diff mbox series

[07/19] http: do not assign string constant to non-const field

Message ID 03b13c449b52ecfc845e8ffb0dd69fe67d50651c.1716983704.git.ps@pks.im (mailing list archive)
State Superseded
Headers show
Series Compile with `-Wwrite-strings` | expand

Commit Message

Patrick Steinhardt May 29, 2024, 12:44 p.m. UTC
In `write_accept_language()`, we put all acceptable languages into an
array. While all entries in that array are allocated strings, the final
entry in that array is a string constant. This is fine because we
explicitly skip over the last entry when freeing the array, but will
cause warnings once we enable `-Wwrite-strings`.

Adapt the code to also allocate the final entry.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
---
 http.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)

Comments

Junio C Hamano May 29, 2024, 7:39 p.m. UTC | #1
Patrick Steinhardt <ps@pks.im> writes:

> In `write_accept_language()`, we put all acceptable languages into an
> array. While all entries in that array are allocated strings, the final
> entry in that array is a string constant. This is fine because we
> explicitly skip over the last entry when freeing the array, but will
> cause warnings once we enable `-Wwrite-strings`.
>
> Adapt the code to also allocate the final entry.
>
> Signed-off-by: Patrick Steinhardt <ps@pks.im>
> ---
>  http.c | 5 ++---
>  1 file changed, 2 insertions(+), 3 deletions(-)
>
> diff --git a/http.c b/http.c
> index 67cc47d28f..2dea2d03da 100644
> --- a/http.c
> +++ b/http.c
> @@ -1974,7 +1974,7 @@ static void write_accept_language(struct strbuf *buf)
>  
>  		/* add '*' */
>  		REALLOC_ARRAY(language_tags, num_langs + 1);
> -		language_tags[num_langs++] = "*"; /* it's OK; this won't be freed */
> +		language_tags[num_langs++] = xstrdup("*");
>  
>  		/* compute decimal_places */
>  		for (max_q = 1, decimal_places = 0;
> @@ -2004,8 +2004,7 @@ static void write_accept_language(struct strbuf *buf)
>  		}
>  	}
>  
> -	/* free language tags -- last one is a static '*' */
> -	for (i = 0; i < num_langs - 1; i++)
> +	for (i = 0; i < num_langs; i++)
>  		free(language_tags[i]);
>  	free(language_tags);
>  }

Makes sense, especially that this is done only once per process.
diff mbox series

Patch

diff --git a/http.c b/http.c
index 67cc47d28f..2dea2d03da 100644
--- a/http.c
+++ b/http.c
@@ -1974,7 +1974,7 @@  static void write_accept_language(struct strbuf *buf)
 
 		/* add '*' */
 		REALLOC_ARRAY(language_tags, num_langs + 1);
-		language_tags[num_langs++] = "*"; /* it's OK; this won't be freed */
+		language_tags[num_langs++] = xstrdup("*");
 
 		/* compute decimal_places */
 		for (max_q = 1, decimal_places = 0;
@@ -2004,8 +2004,7 @@  static void write_accept_language(struct strbuf *buf)
 		}
 	}
 
-	/* free language tags -- last one is a static '*' */
-	for (i = 0; i < num_langs - 1; i++)
+	for (i = 0; i < num_langs; i++)
 		free(language_tags[i]);
 	free(language_tags);
 }