Skip to contentSkip to conversation

module: fix sprintf format specifier in param_get_byte()

PATCH In review Christoph Jaeger 5 participants 5 messages Aug 6, 2013 – Aug 7, 2013

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 module: fix sprintf format specifier in param_get_byte()

In param_get_byte(), to which the macro STANDARD_PARAM_DEF(byte, ...) expands, "%c" is used to print an unsigned char. So it gets printed as a character what is not intended here. Use "%hhu" instead.

---
 kernel/params.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/params.c +1 -1
index 440e65d..59f7ac7 100644
@@ -252,7 +252,7 @@ int parse_args(const char *doing,
 	EXPORT_SYMBOL(param_ops_##name)
 
 
-STANDARD_PARAM_DEF(byte, unsigned char, "%c", unsigned long, strict_strtoul);
+STANDARD_PARAM_DEF(byte, unsigned char, "%hhu", unsigned long, strict_strtoul);
 STANDARD_PARAM_DEF(short, short, "%hi", long, strict_strtol);
 STANDARD_PARAM_DEF(ushort, unsigned short, "%hu", unsigned long, strict_strtoul);
 STANDARD_PARAM_DEF(int, int, "%i", long, strict_strtol);
Signature · 1 line
1.8.3.1
#1 lore raw
Quoted 14 lines · Christoph Jaeger

In param_get_byte(), to which the macro STANDARD_PARAM_DEF(byte, ...) expands, "%c" is used to print an unsigned char. So it gets printed as a character what is not intended here. Use "%hhu" instead.

---
 kernel/params.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/params.c +0 -0
index 440e65d..59f7ac7 100644

kernel/params.c int parse_args(const char *doing, · line 257

Quoted 7 lines
kernel/params.c +1 -1
@@ -252,7 +252,7 @@ int parse_args(const char *doing,
 	EXPORT_SYMBOL(param_ops_##name)
 
 
-STANDARD_PARAM_DEF(byte, unsigned char, "%c", unsigned long, strict_strtoul);
+STANDARD_PARAM_DEF(byte, unsigned char, "%hhu", unsigned long, strict_strtoul);
 STANDARD_PARAM_DEF(short, short, "%hi", long, strict_strtol);
 STANDARD_PARAM_DEF(ushort, unsigned short, "%hu", unsigned long, strict_strtoul);

I don't see the point of using "%hh[ui]" and "%h[ui]". These are promoted to int/unsigned int for the sprintf anyway. I'd just use %d and %u instead.

#2 lore raw
In reply to Christoph Jaeger
Quoted 5 lines · Christoph Jaeger

In param_get_byte(), to which the macro STANDARD_PARAM_DEF(byte, ...) expands, "%c" is used to print an unsigned char. So it gets printed as a character what is not intended here. Use "%hhu" instead.

Nice patch. Unfortunately, there are several users of this already:

drivers/net/wireless/cw1200/main.c:50:module_param_array_named(macaddr, cw1200_mac_template, byte, NULL, S_IRUGO); drivers/ntb/ntb_transport.c:68:module_param(max_num_clients, byte, 0644); drivers/scsi/lpfc/lpfc_attr.c:4207:module_param(lpfc_prot_guard, byte, S_IRUGO); drivers/usb/atm/speedtch.c:117:module_param(ModemMode, byte, S_IRUGO | S_IWUSR); drivers/usb/atm/speedtch.c:121:module_param_array(ModemOption, byte, &num_ModemOption, S_IRUGO);

drivers/usb/gadget/g_ffs.c:95:module_param_named(bDeviceClass,    gfs_dev_desc.bDeviceClass,    byte,   0644);
drivers/usb/gadget/g_ffs.c:97:module_param_named(bDeviceSubClass, gfs_dev_desc.bDeviceSubClass, byte,   0644);
drivers/usb/gadget/g_ffs.c:99:module_param_named(bDeviceProtocol, gfs_dev_desc.bDeviceProtocol, byte,   0644);

I have CC'd all the authors, to see if changing the results of reading /sys/module/<modname>/parameters/<xxx> from a literal char to a number will harm them.

Thanks,
Rusty.

Quoted 19 lines
---
 kernel/params.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/params.c +1 -1
index 440e65d..59f7ac7 100644
@@ -252,7 +252,7 @@ int parse_args(const char *doing,
 	EXPORT_SYMBOL(param_ops_##name)
 
 
-STANDARD_PARAM_DEF(byte, unsigned char, "%c", unsigned long, strict_strtoul);
+STANDARD_PARAM_DEF(byte, unsigned char, "%hhu", unsigned long, strict_strtoul);
 STANDARD_PARAM_DEF(short, short, "%hi", long, strict_strtol);
 STANDARD_PARAM_DEF(ushort, unsigned short, "%hu", unsigned long, strict_strtoul);
 STANDARD_PARAM_DEF(int, int, "%i", long, strict_strtol);
Signature · 1 line
1.8.3.1
#3 lore raw
Quoted 6 lines · Rusty Russell
Quoted 5 lines · Christoph Jaeger

In param_get_byte(), to which the macro STANDARD_PARAM_DEF(byte, ...) expands, "%c" is used to print an unsigned char. So it gets printed as a character what is not intended here. Use "%hhu" instead.

for g_ffs.c:

Quoted 3 lines
drivers/usb/gadget/g_ffs.c:95:module_param_named(bDeviceClass,    gfs_dev_desc.bDeviceClass,    byte,   0644);
drivers/usb/gadget/g_ffs.c:97:module_param_named(bDeviceSubClass, gfs_dev_desc.bDeviceSubClass, byte,   0644);
drivers/usb/gadget/g_ffs.c:99:module_param_named(bDeviceProtocol, gfs_dev_desc.bDeviceProtocol, byte,   0644);

I don't think it breaks anything for g_ffs since those properties are most likely write-only and I don't expect many people reading them.

Quoted 12 lines
Quoted 12 lines
---
 kernel/params.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/params.c +0 -0
index 440e65d..59f7ac7 100644
@@ -252,7 +252,7 @@ int parse_args(const char *doing,
 	EXPORT_SYMBOL(param_ops_##name)
 
 
Quoted 4 lines

kernel/params.c int parse_args(const char *doing, · line 257

Quoted 4 lines
kernel/params.c +1 -1
-STANDARD_PARAM_DEF(byte, unsigned char, "%c", unsigned long, strict_strtoul);
+STANDARD_PARAM_DEF(byte, unsigned char, "%hhu", unsigned long, strict_strtoul);
 STANDARD_PARAM_DEF(short, short, "%hi", long, strict_strtol);
 STANDARD_PARAM_DEF(ushort, unsigned short, "%hu", unsigned long, strict_strtoul);

Actually, are those “h” specifiers even necessary? I'm fairly certain that “%u” for byte, “%i” for short, and “%u” for ushort would work just fine since the argument gets promoted to (unsigned) int anyway and indeed vsnprintf reads an int for all those types.

Quoted 1 line
Quoted 1 line
kernel/params.c +0 -0
 STANDARD_PARAM_DEF(int, int, "%i", long, strict_strtol);
Signature · 4 lines
Best regards,                                         _     _
.o. | Liege of Serenely Enlightened Majesty of      o' \,=./ `o
..o | Computer Science,  Michał “mina86” Nazarewicz    (o o)
ooo +----<email/xmpp: [email protected]>--------------ooO--(_)--Ooo--
Acked-by
#4 lore raw
In reply to Rusty Russell
Quoted 11 lines · Rusty Russell
Quoted 5 lines · Christoph Jaeger

In param_get_byte(), to which the macro STANDARD_PARAM_DEF(byte, ...) expands, "%c" is used to print an unsigned char. So it gets printed as a character what is not intended here. Use "%hhu" instead.

Nice patch. Unfortunately, there are several users of this already:

drivers/net/wireless/cw1200/main.c:50:module_param_array_named(macaddr, cw1200_mac_template, byte, NULL, S_IRUGO); drivers/ntb/ntb_transport.c:68:module_param(max_num_clients, byte, 0644);

This shouldn't affect NTB negatively.

Quoted 33 lines

drivers/scsi/lpfc/lpfc_attr.c:4207:module_param(lpfc_prot_guard, byte, S_IRUGO); drivers/usb/atm/speedtch.c:117:module_param(ModemMode, byte, S_IRUGO | S_IWUSR); drivers/usb/atm/speedtch.c:121:module_param_array(ModemOption, byte, &num_ModemOption, S_IRUGO);

drivers/usb/gadget/g_ffs.c:95:module_param_named(bDeviceClass,    gfs_dev_desc.bDeviceClass,    byte,   0644);
drivers/usb/gadget/g_ffs.c:97:module_param_named(bDeviceSubClass, gfs_dev_desc.bDeviceSubClass, byte,   0644);
drivers/usb/gadget/g_ffs.c:99:module_param_named(bDeviceProtocol, gfs_dev_desc.bDeviceProtocol, byte,   0644);

I have CC'd all the authors, to see if changing the results of reading /sys/module/<modname>/parameters/<xxx> from a literal char to a number will harm them.

Thanks,
Rusty.

Quoted 19 lines
---
 kernel/params.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/params.c +1 -1
index 440e65d..59f7ac7 100644
@@ -252,7 +252,7 @@ int parse_args(const char *doing,
 	EXPORT_SYMBOL(param_ops_##name)
 
 
-STANDARD_PARAM_DEF(byte, unsigned char, "%c", unsigned long, strict_strtoul);
+STANDARD_PARAM_DEF(byte, unsigned char, "%hhu", unsigned long, strict_strtoul);
 STANDARD_PARAM_DEF(short, short, "%hi", long, strict_strtol);
 STANDARD_PARAM_DEF(ushort, unsigned short, "%hu", unsigned long, strict_strtoul);
 STANDARD_PARAM_DEF(int, int, "%i", long, strict_strtol);
Signature · 1 line
1.8.3.1