2023-08-16 10:29:18

by Jiri Slaby

[permalink] [raw]
Subject: [PATCH] tty: gdm724x: use min_t() for size_t varable and a constant

My thinking was that ulong is the same as size_t everywhere. No, size_t
is uint on 32bit. So the below commit introduced a build warning on
32bit:
.../gdm724x/gdm_tty.c:165:24: warning: comparison of distinct pointer types ('typeof (2048UL) *' (aka 'unsigned long *') and 'typeof (remain) *' (aka 'unsigned int *'))

To fix this, partially revert the commit (remove constants' suffixes)
and switch to min_t() in this case instead.

/me would hope for Z (or alike) suffix for constants.

Signed-off-by: Jiri Slaby (SUSE) <[email protected]>
Fixes: c3e5c706aefc (tty: gdm724x: convert counts to size_t)
Reported-by: Nathan Chancellor <[email protected]>
Reported-by: kernel test robot <[email protected]>
Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
---
drivers/staging/gdm724x/gdm_tty.c | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/drivers/staging/gdm724x/gdm_tty.c b/drivers/staging/gdm724x/gdm_tty.c
index 67d9bf41e836..32b2e817ff04 100644
--- a/drivers/staging/gdm724x/gdm_tty.c
+++ b/drivers/staging/gdm724x/gdm_tty.c
@@ -17,9 +17,9 @@
#define GDM_TTY_MAJOR 0
#define GDM_TTY_MINOR 32

-#define WRITE_SIZE 2048UL
+#define WRITE_SIZE 2048

-#define MUX_TX_MAX_SIZE 2048UL
+#define MUX_TX_MAX_SIZE 2048

static inline bool gdm_tty_ready(struct gdm *gdm)
{
@@ -159,7 +159,7 @@ static ssize_t gdm_tty_write(struct tty_struct *tty, const u8 *buf, size_t len)
return -ENODEV;

while (remain) {
- size_t sending_len = min(MUX_TX_MAX_SIZE, remain);
+ size_t sending_len = min_t(size_t, MUX_TX_MAX_SIZE, remain);
gdm->tty_dev->send_func(gdm->tty_dev->priv_dev,
(void *)(buf + sent_len),
sending_len,
--
2.41.0



2023-08-17 07:37:21

by Nathan Chancellor

[permalink] [raw]
Subject: Re: [PATCH] tty: gdm724x: use min_t() for size_t varable and a constant

On Wed, Aug 16, 2023 at 10:53:22AM +0200, Jiri Slaby (SUSE) wrote:
> My thinking was that ulong is the same as size_t everywhere. No, size_t
> is uint on 32bit. So the below commit introduced a build warning on
> 32bit:
> .../gdm724x/gdm_tty.c:165:24: warning: comparison of distinct pointer types ('typeof (2048UL) *' (aka 'unsigned long *') and 'typeof (remain) *' (aka 'unsigned int *'))
>
> To fix this, partially revert the commit (remove constants' suffixes)
> and switch to min_t() in this case instead.
>
> /me would hope for Z (or alike) suffix for constants.
>
> Signed-off-by: Jiri Slaby (SUSE) <[email protected]>
> Fixes: c3e5c706aefc (tty: gdm724x: convert counts to size_t)
> Reported-by: Nathan Chancellor <[email protected]>
> Reported-by: kernel test robot <[email protected]>
> Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

Tested-by: Nathan Chancellor <[email protected]> # build

> ---
> drivers/staging/gdm724x/gdm_tty.c | 6 +++---
> 1 file changed, 3 insertions(+), 3 deletions(-)
>
> diff --git a/drivers/staging/gdm724x/gdm_tty.c b/drivers/staging/gdm724x/gdm_tty.c
> index 67d9bf41e836..32b2e817ff04 100644
> --- a/drivers/staging/gdm724x/gdm_tty.c
> +++ b/drivers/staging/gdm724x/gdm_tty.c
> @@ -17,9 +17,9 @@
> #define GDM_TTY_MAJOR 0
> #define GDM_TTY_MINOR 32
>
> -#define WRITE_SIZE 2048UL
> +#define WRITE_SIZE 2048
>
> -#define MUX_TX_MAX_SIZE 2048UL
> +#define MUX_TX_MAX_SIZE 2048
>
> static inline bool gdm_tty_ready(struct gdm *gdm)
> {
> @@ -159,7 +159,7 @@ static ssize_t gdm_tty_write(struct tty_struct *tty, const u8 *buf, size_t len)
> return -ENODEV;
>
> while (remain) {
> - size_t sending_len = min(MUX_TX_MAX_SIZE, remain);
> + size_t sending_len = min_t(size_t, MUX_TX_MAX_SIZE, remain);
> gdm->tty_dev->send_func(gdm->tty_dev->priv_dev,
> (void *)(buf + sent_len),
> sending_len,
> --
> 2.41.0
>

2023-08-18 09:40:32

by David Laight

[permalink] [raw]
Subject: RE: [PATCH] tty: gdm724x: use min_t() for size_t varable and a constant

From: Jiri Slaby (SUSE)
> Sent: Wednesday, August 16, 2023 9:53 AM
>
> My thinking was that ulong is the same as size_t everywhere. No, size_t
> is uint on 32bit. So the below commit introduced a build warning on
> 32bit:
> .../gdm724x/gdm_tty.c:165:24: warning: comparison of distinct pointer types ('typeof (2048UL) *' (aka
> 'unsigned long *') and 'typeof (remain) *' (aka 'unsigned int *'))
>
> To fix this, partially revert the commit (remove constants' suffixes)
> and switch to min_t() in this case instead.
>
....
> - size_t sending_len = min(MUX_TX_MAX_SIZE, remain);
> + size_t sending_len = min_t(size_t, MUX_TX_MAX_SIZE, remain);

It would be slightly safer to use:
min(remain, (size_t)MAX_TX_MAX_SIZE);
since that maintains the type check.
(It is also nicer to put the constant second.)

David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)