2023-11-21 09:24:45

by Jiri Slaby

[permalink] [raw]
Subject: [PATCH 17/17] tty: srmcons: make 'str_cr' const and non-array

'str_cr' contains a single character: \n. There is no need to declare it
as array. Declare it as a variable, make it const and pass a pointer to
it to callback_puts().

Signed-off-by: Jiri Slaby (SUSE) <[email protected]>
Cc: Richard Henderson <[email protected]>
Cc: Ivan Kokshaysky <[email protected]>
Cc: Matt Turner <[email protected]>
Cc: [email protected]
---
arch/alpha/kernel/srmcons.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/arch/alpha/kernel/srmcons.c b/arch/alpha/kernel/srmcons.c
index c6b821afbfd3..a6cff61706b5 100644
--- a/arch/alpha/kernel/srmcons.c
+++ b/arch/alpha/kernel/srmcons.c
@@ -91,7 +91,7 @@ srmcons_receive_chars(struct timer_list *t)
static void
srmcons_do_write(struct tty_port *port, const char *buf, int count)
{
- static char str_cr[1] = "\r";
+ static const char str_cr = '\r';
size_t c;
srmcons_result result;

@@ -119,7 +119,7 @@ srmcons_do_write(struct tty_port *port, const char *buf, int count)
}

while (need_cr) {
- result.as_long = callback_puts(0, str_cr, 1);
+ result.as_long = callback_puts(0, &str_cr, 1);
if (result.bits.c > 0)
need_cr = false;
}
--
2.42.1


2023-11-21 15:29:18

by Ilpo Järvinen

[permalink] [raw]
Subject: Re: [PATCH 17/17] tty: srmcons: make 'str_cr' const and non-array

On Tue, 21 Nov 2023, Jiri Slaby (SUSE) wrote:

> 'str_cr' contains a single character: \n. There is no need to declare it

Aren't \r and \n different characters?

> - static char str_cr[1] = "\r";
> + static const char str_cr = '\r';

Thanks for making these cleanups.

I've reviewed all the patches in this series, so if I didn't comment a
patch or when you address my remarks, feel free to add:

Reviewed-by: Ilpo J?rvinen <[email protected]>

--
i.

2023-11-21 17:52:32

by Richard Henderson

[permalink] [raw]
Subject: Re: [PATCH 17/17] tty: srmcons: make 'str_cr' const and non-array

On 11/21/23 03:22, Jiri Slaby (SUSE) wrote:
> 'str_cr' contains a single character: \n. There is no need to declare it

\r

> as array. Declare it as a variable, make it const and pass a pointer to
> it to callback_puts().
>
> Signed-off-by: Jiri Slaby (SUSE) <[email protected]>
> Cc: Richard Henderson <[email protected]>
> Cc: Ivan Kokshaysky <[email protected]>
> Cc: Matt Turner <[email protected]>
> Cc: [email protected]
> ---
> arch/alpha/kernel/srmcons.c | 4 ++--
> 1 file changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/arch/alpha/kernel/srmcons.c b/arch/alpha/kernel/srmcons.c
> index c6b821afbfd3..a6cff61706b5 100644
> --- a/arch/alpha/kernel/srmcons.c
> +++ b/arch/alpha/kernel/srmcons.c
> @@ -91,7 +91,7 @@ srmcons_receive_chars(struct timer_list *t)
> static void
> srmcons_do_write(struct tty_port *port, const char *buf, int count)
> {
> - static char str_cr[1] = "\r";
> + static const char str_cr = '\r';

An array of one element is fine -- what's wrong with that?
Adding const is an improvement though.


r~

2023-11-21 17:59:21

by Richard Henderson

[permalink] [raw]
Subject: Re: [PATCH 17/17] tty: srmcons: make 'str_cr' const and non-array

On 11/21/23 03:22, Jiri Slaby (SUSE) wrote:
> 'str_cr' contains a single character: \n. There is no need to declare it
> as array. Declare it as a variable, make it const and pass a pointer to
> it to callback_puts().
>
> Signed-off-by: Jiri Slaby (SUSE) <[email protected]>
> Cc: Richard Henderson <[email protected]>
> Cc: Ivan Kokshaysky <[email protected]>
> Cc: Matt Turner <[email protected]>
> Cc: [email protected]
> ---
> arch/alpha/kernel/srmcons.c | 4 ++--
> 1 file changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/arch/alpha/kernel/srmcons.c b/arch/alpha/kernel/srmcons.c
> index c6b821afbfd3..a6cff61706b5 100644
> --- a/arch/alpha/kernel/srmcons.c
> +++ b/arch/alpha/kernel/srmcons.c
> @@ -91,7 +91,7 @@ srmcons_receive_chars(struct timer_list *t)
> static void
> srmcons_do_write(struct tty_port *port, const char *buf, int count)
> {
> - static char str_cr[1] = "\r";
> + static const char str_cr = '\r';

Best to remove this entirely...

> size_t c;
> srmcons_result result;
>
> @@ -119,7 +119,7 @@ srmcons_do_write(struct tty_port *port, const char *buf, int count)
> }
>
> while (need_cr) {
> - result.as_long = callback_puts(0, str_cr, 1);
> + result.as_long = callback_puts(0, &str_cr, 1);

... and simply use "\r" here.

Logically it adds one '\0' of const data, but it is virtually certain that even more bytes
of padding are present anyway. As a string literal it will be placed into .rodata.str1.1
and packed with other strings.


r~

2023-11-22 06:33:35

by Jiri Slaby

[permalink] [raw]
Subject: Re: [PATCH 17/17] tty: srmcons: make 'str_cr' const and non-array

On 21. 11. 23, 16:28, Ilpo Järvinen wrote:
> On Tue, 21 Nov 2023, Jiri Slaby (SUSE) wrote:
>
>> 'str_cr' contains a single character: \n. There is no need to declare it
>
> Aren't \r and \n different characters?

Definitely, this is a thinko -- I didn't remember properly what it
contains when writing the log. Fixed.

>
>> - static char str_cr[1] = "\r";
>> + static const char str_cr = '\r';
>
> Thanks for making these cleanups.
>
> I've reviewed all the patches in this series, so if I didn't comment a
> patch or when you address my remarks, feel free to add:

thanks,
--
js
suse labs