Skip to contentSkip to conversation

tty: gdm724x: use min_t() for size_t varable and a constant

PATCH In review Jiri Slaby (SUSE) 4 participants 4 messages Aug 16, 2023 – Aug 17, 2023

Subsystems (2)
lore
Share & tools
lore

Jiri Slaby (SUSE)

author maintainer
#0 lore raw

PATCH 1/1 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.

---
 drivers/staging/gdm724x/gdm_tty.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/staging/gdm724x/gdm_tty.c +3 -3
index 67d9bf41e836..32b2e817ff04 100644
@@ -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,
Signature · 1 line
2.41.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 8 lines · Jiri Slaby (SUSE)

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.

Quoted 7 lines

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

Thanks, this fixes the m68k/allmodconfig build.

drivers/staging/gdm724x/gdm_tty.c line 22

Quoted 11 lines
drivers/staging/gdm724x/gdm_tty.c +2 -2
@@ -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

You probably want to keep the "U" suffix, so at least both parts of min() are unsigned.

See also "[PATCH next v3 0/5] minmax: Relax type checks in min() and max().".
https://lore.kernel.org/all/[email protected],

Quoted 16 lines
drivers/staging/gdm724x/gdm_tty.c +1 -1
 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,
Signature · 1 line
2.41.0

Gr{oetje,eeting}s,

Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that.
-- Linus Torvalds

Tested-by on v1 1/1
#2 lore raw
In reply to Jiri Slaby (SUSE)
Quoted 8 lines · Jiri Slaby (SUSE)

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.

Quoted 7 lines

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

Quoted 32 lines
---
 drivers/staging/gdm724x/gdm_tty.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/staging/gdm724x/gdm_tty.c +3 -3
index 67d9bf41e836..32b2e817ff04 100644
@@ -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,
Signature · 1 line
2.41.0
#3 lore raw
In reply to Jiri Slaby (SUSE)

From: Jiri Slaby (SUSE)

Quoted 11 lines

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.

....

drivers/staging/gdm724x/gdm_tty.c static ssize_t gdm_tty_write(struct tty_struct *tty, const u8 *buf, size_t len) · line 162

Quoted 2 lines
drivers/staging/gdm724x/gdm_tty.c +1 -1
-		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)