public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] Make n_spaces return a const char *
@ 2021-12-11 22:11 Tom Tromey
  2021-12-17 15:48 ` Bruno Larsen
  2021-12-20 16:04 ` Tom Tromey
  0 siblings, 2 replies; 3+ messages in thread
From: Tom Tromey @ 2021-12-11 22:11 UTC (permalink / raw)
  To: gdb-patches; +Cc: Tom Tromey

n_spaces keeps the spaces in a static buffer.  If a caller overwrites
these, it may give an incorrect result to a subsequent caller.  So,
make the return type const to help avoid this outcome.
---
 gdb/utils.c | 2 +-
 gdb/utils.h | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/gdb/utils.c b/gdb/utils.c
index e27a8818b94..ea0f65c10e4 100644
--- a/gdb/utils.c
+++ b/gdb/utils.c
@@ -2303,7 +2303,7 @@ puts_unfiltered (const char *string)
 
 /* Return a pointer to N spaces and a null.  The pointer is good
    until the next call to here.  */
-char *
+const char *
 n_spaces (int n)
 {
   char *t;
diff --git a/gdb/utils.h b/gdb/utils.h
index 6f3a70213a4..f6fd32faf90 100644
--- a/gdb/utils.h
+++ b/gdb/utils.h
@@ -473,7 +473,7 @@ extern void print_spaces (int, struct ui_file *);
 
 extern void print_spaces_filtered (int, struct ui_file *);
 
-extern char *n_spaces (int);
+extern const char *n_spaces (int);
 
 extern void fputstr_filtered (const char *str, int quotr,
 			      struct ui_file * stream);
-- 
2.31.1


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] Make n_spaces return a const char *
  2021-12-11 22:11 [PATCH] Make n_spaces return a const char * Tom Tromey
@ 2021-12-17 15:48 ` Bruno Larsen
  2021-12-20 16:04 ` Tom Tromey
  1 sibling, 0 replies; 3+ messages in thread
From: Bruno Larsen @ 2021-12-17 15:48 UTC (permalink / raw)
  To: gdb-patches

On 12/11/21 19:11, Tom Tromey wrote:
> n_spaces keeps the spaces in a static buffer.  If a caller overwrites
> these, it may give an incorrect result to a subsequent caller.  So,
> make the return type const to help avoid this outcome.
> ---
>   gdb/utils.c | 2 +-
>   gdb/utils.h | 2 +-
>   2 files changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/gdb/utils.c b/gdb/utils.c
> index e27a8818b94..ea0f65c10e4 100644
> --- a/gdb/utils.c
> +++ b/gdb/utils.c
> @@ -2303,7 +2303,7 @@ puts_unfiltered (const char *string)
>   
>   /* Return a pointer to N spaces and a null.  The pointer is good
>      until the next call to here.  */
> -char *
> +const char *
>   n_spaces (int n)
>   {
>     char *t;
> diff --git a/gdb/utils.h b/gdb/utils.h
> index 6f3a70213a4..f6fd32faf90 100644
> --- a/gdb/utils.h
> +++ b/gdb/utils.h
> @@ -473,7 +473,7 @@ extern void print_spaces (int, struct ui_file *);
>   
>   extern void print_spaces_filtered (int, struct ui_file *);
>   
> -extern char *n_spaces (int);
> +extern const char *n_spaces (int);
>   
>   extern void fputstr_filtered (const char *str, int quotr,
>   			      struct ui_file * stream);

LGTM. Looks like a good change and doesn't introduce any regressions

-- 
Cheers!
Bruno Larsen


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] Make n_spaces return a const char *
  2021-12-11 22:11 [PATCH] Make n_spaces return a const char * Tom Tromey
  2021-12-17 15:48 ` Bruno Larsen
@ 2021-12-20 16:04 ` Tom Tromey
  1 sibling, 0 replies; 3+ messages in thread
From: Tom Tromey @ 2021-12-20 16:04 UTC (permalink / raw)
  To: Tom Tromey; +Cc: gdb-patches

>>>>> "Tom" == Tom Tromey <tom@tromey.com> writes:

Tom> n_spaces keeps the spaces in a static buffer.  If a caller overwrites
Tom> these, it may give an incorrect result to a subsequent caller.  So,
Tom> make the return type const to help avoid this outcome.

I'm checking this in now.

Sometimes I wonder if we want to split these utility things into
separate files and put them in gdbsupport.

Tom

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2021-12-20 16:04 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-12-11 22:11 [PATCH] Make n_spaces return a const char * Tom Tromey
2021-12-17 15:48 ` Bruno Larsen
2021-12-20 16:04 ` Tom Tromey

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).