Skip to contentSkip to conversation

batman-adv: add compat code for kstrtou32

PATCH Applied Antonio Quartulli 3 participants 8 messages Nov 19, 2013 – Nov 21, 2013

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 batman-adv: add compat code for kstrtou32

kstrtou32() does not exist before 2.6.39. Convert it to strict_strtoul() in that case.

Used-by: e29249b4483b91d7a590a16e9b5ffe9412ce7fde ("batman-adv: add isolation_mark sysfs attribute")

---
 compat.h | 2 ++
 1 file changed, 2 insertions(+)
compat.h +2 -0
index 01984b8..334dc5c 100644
@@ -152,6 +152,8 @@ static inline int batadv_param_set_copystring(const char *val,
 
 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 39)
 
+/* cast last argument of strict_strtoul() because we have a uint32_t */
+#define kstrtou32(cp, base, v) strict_strtoul(cp, base, (unsigned long *)v)
 #define kstrtoul strict_strtoul
 #define kstrtol  strict_strtol
 
Signature · 1 line
1.8.4.3
#1 lore raw
Quoted 10 lines · On Tuesday 19 November 2013 08:26:07 Antonio Quartulli wrote:

kstrtou32() does not exist before 2.6.39. Convert it to strict_strtoul() in that case.

Used-by: e29249b4483b91d7a590a16e9b5ffe9412ce7fde ("batman-adv: add isolation_mark sysfs attribute")

---
 compat.h | 2 ++
 1 file changed, 2 insertions(+)

Applied in revision 28855a6.

Thanks,
Marek

#2 lore raw
In reply to Antonio Quartulli
Quoted 17 lines · Antonio Quartulli

kstrtou32() does not exist before 2.6.39. Convert it to strict_strtoul() in that case.

Used-by: e29249b4483b91d7a590a16e9b5ffe9412ce7fde ("batman-adv: add isolation_mark sysfs attribute")

---
 compat.h | 2 ++
 1 file changed, 2 insertions(+)
compat.h +0 -0
index 01984b8..334dc5c 100644

compat.h static inline int batadv_param_set_copystring(const char *val, · line 159

Quoted 8 lines
compat.h +2 -0
@@ -152,6 +152,8 @@ static inline int batadv_param_set_copystring(const char *val,
 
 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 39)
 
+/* cast last argument of strict_strtoul() because we have a uint32_t */
+#define kstrtou32(cp, base, v) strict_strtoul(cp, base, (unsigned long *)v)
 #define kstrtoul strict_strtoul
 #define kstrtol  strict_strtol
 

You are casting a u32 * to unsigned long *? Won't this break horribly when sizeof(u32) != sizeof(unsigned long)?

#3 lore raw
Quoted 3 lines · Matthias Schiffer

You are casting a u32 * to unsigned long *? Won't this break horribly when sizeof(u32) != sizeof(unsigned long)?

It will break when sizeof(unsigned long) > sizeof(uint32). For example on x86_64. Hopefully nobody would use such ancient kernel on his machine. But you are right.

What do you think of the attached patch?

Signature · 1 line
Antonio Quartulli
compat.h +8 -1
index a050bb8..fe64820 100644
@@ -153,7 +153,14 @@ static inline int batadv_param_set_copystring(const char *val,
 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 39)
 
 /* cast last argument of strict_strtoul() because we have a uint32_t */
-#define kstrtou32(cp, base, v) strict_strtoul(cp, base, (unsigned long *)v)
+#define kstrtou32(cp, base, v) \
+({\
+	unsigned long _v;\
+	int _r;\
+	_r = strict_strtoul(cp, base, _v);\
+	v = (uint32_t)_v;\
+	_r;\
+})
 #define kstrtoul strict_strtoul
 #define kstrtol  strict_strtol
 
#4 lore raw
Quoted 11 lines · Antonio Quartulli
Quoted 3 lines · Matthias Schiffer

You are casting a u32 * to unsigned long *? Won't this break horribly when sizeof(u32) != sizeof(unsigned long)?

It will break when sizeof(unsigned long) > sizeof(uint32). For example on x86_64. Hopefully nobody would use such ancient kernel on his machine. But you are right.

What do you think of the attached patch?

That's better, but if you care about overflow, you should add something like

if (_v != (unsigned long)v)

	_r = -ERANGE;

Also, the comment in the line above doesn't make sense anymore...

#5 lore raw
Quoted 10 lines · Matthias Schiffer
Quoted 9 lines · Antonio Quartulli
Quoted 3 lines · Matthias Schiffer

You are casting a u32 * to unsigned long *? Won't this break horribly when sizeof(u32) != sizeof(unsigned long)?

It will break when sizeof(unsigned long) > sizeof(uint32). For example on x86_64. Hopefully nobody would use such ancient kernel on his machine. But you are right.

Quoted 7 lines
Quoted 2 lines

What do you think of the attached patch?

That's better, but if you care about overflow, you should add something like

if (_v != (unsigned long)v)

	_r = -ERANGE;

yeah, thanks! I'll add this!

Quoted 2 lines

Also, the comment in the line above doesn't make sense anymore...

yeah, I removed that already.

Signature · 1 line
Antonio Quartulli
#6 lore raw
Quoted 19 lines · Antonio Quartulli
Quoted 10 lines · Matthias Schiffer
Quoted 9 lines · Antonio Quartulli
Quoted 3 lines · Matthias Schiffer

You are casting a u32 * to unsigned long *? Won't this break horribly when sizeof(u32) != sizeof(unsigned long)?

It will break when sizeof(unsigned long) > sizeof(uint32). For example on x86_64. Hopefully nobody would use such ancient kernel on his machine. But you are right.

Quoted 7 lines
Quoted 2 lines

What do you think of the attached patch?

That's better, but if you care about overflow, you should add something like

if (_v != (unsigned long)v)

	_r = -ERANGE;
Quoted 8 lines

yeah, thanks! I'll add this!

Quoted 2 lines

Also, the comment in the line above doesn't make sense anymore...

yeah, I removed that already.

Ah, and I overlooked that you have to dereference v and take the address of _v; adding parantheses around the parameters might also be a good idea as it is a macro.

So I'd recommend something like this (not even compile tested...):

({\
	unsigned long _v;\
	int _r;\
	_r = strict_strtoul((cp), (base), &_v);\
	if (_v == (unsigned long)(u32)_v)\
		*(v) = (u32)_v;\
	else\
		_r = -ERANGE;\
	_r;\
})
#7 lore raw
Quoted 23 lines · Matthias Schiffer
Quoted 22 lines · Antonio Quartulli
Quoted 10 lines · Matthias Schiffer
Quoted 9 lines · Antonio Quartulli
Quoted 3 lines · Matthias Schiffer

You are casting a u32 * to unsigned long *? Won't this break horribly when sizeof(u32) != sizeof(unsigned long)?

It will break when sizeof(unsigned long) > sizeof(uint32). For example on x86_64. Hopefully nobody would use such ancient kernel on his machine. But you are right.

Quoted 7 lines
Quoted 2 lines

What do you think of the attached patch?

That's better, but if you care about overflow, you should add something like

if (_v != (unsigned long)v)

	_r = -ERANGE;

yeah, thanks! I'll add this!

Quoted 1 line
Quoted 8 lines
Quoted 5 lines
Quoted 1 line

Also, the comment in the line above doesn't make sense anymore...

yeah, I removed that already.

Ah, and I overlooked that you have to dereference v and take the address of _v;

yeah yeah, this snippet has not been compiled at all.

Quoted 3 lines

adding parantheses around the parameters might also be a good idea as it is a macro.

parenthesis in strict_strtoul() invocation are not useful, but yeah, they are good around the rest.

Signature · 1 line
Antonio Quartulli