diff options
author | Alexey Dobriyan <adobriyan@gmail.com> | 2011-03-22 19:34:40 -0400 |
---|---|---|
committer | Linus Torvalds <torvalds@linux-foundation.org> | 2011-03-22 20:44:14 -0400 |
commit | 33ee3b2e2eb9b4b6c64dcf9ed66e2ac3124e748c (patch) | |
tree | 25d70c021189efa0bcbdf4e84b3ca97a6c147246 /include/linux/kernel.h | |
parent | 8a5700cd6754a3c88d2ea2f1d7a56f671987fc25 (diff) |
kstrto*: converting strings to integers done (hopefully) right
1. simple_strto*() do not contain overflow checks and crufty,
libc way to indicate failure.
2. strict_strto*() also do not have overflow checks but the name and
comments pretend they do.
3. Both families have only "long long" and "long" variants,
but users want strtou8()
4. Both "simple" and "strict" prefixes are wrong:
Simple doesn't exactly say what's so simple, strict should not exist
because conversion should be strict by default.
The solution is to use "k" prefix and add convertors for more types.
Enter
kstrtoull()
kstrtoll()
kstrtoul()
kstrtol()
kstrtouint()
kstrtoint()
kstrtou64()
kstrtos64()
kstrtou32()
kstrtos32()
kstrtou16()
kstrtos16()
kstrtou8()
kstrtos8()
Include runtime testsuite (somewhat incomplete) as well.
strict_strto*() become deprecated, stubbed to kstrto*() and
eventually will be removed altogether.
Use kstrto*() in code today!
Note: on some archs _kstrtoul() and _kstrtol() are left in tree, even if
they'll be unused at runtime. This is temporarily solution,
because I don't want to hardcode list of archs where these
functions aren't needed. Current solution with sizeof() and
__alignof__ at least always works.
Signed-off-by: Alexey Dobriyan <adobriyan@gmail.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'include/linux/kernel.h')
-rw-r--r-- | include/linux/kernel.h | 70 |
1 files changed, 66 insertions, 4 deletions
diff --git a/include/linux/kernel.h b/include/linux/kernel.h index 2fe6e84894a4..00cec4dc0ae2 100644 --- a/include/linux/kernel.h +++ b/include/linux/kernel.h | |||
@@ -187,14 +187,76 @@ NORET_TYPE void do_exit(long error_code) | |||
187 | ATTRIB_NORET; | 187 | ATTRIB_NORET; |
188 | NORET_TYPE void complete_and_exit(struct completion *, long) | 188 | NORET_TYPE void complete_and_exit(struct completion *, long) |
189 | ATTRIB_NORET; | 189 | ATTRIB_NORET; |
190 | |||
191 | /* Internal, do not use. */ | ||
192 | int __must_check _kstrtoul(const char *s, unsigned int base, unsigned long *res); | ||
193 | int __must_check _kstrtol(const char *s, unsigned int base, long *res); | ||
194 | |||
195 | int __must_check kstrtoull(const char *s, unsigned int base, unsigned long long *res); | ||
196 | int __must_check kstrtoll(const char *s, unsigned int base, long long *res); | ||
197 | static inline int __must_check kstrtoul(const char *s, unsigned int base, unsigned long *res) | ||
198 | { | ||
199 | /* | ||
200 | * We want to shortcut function call, but | ||
201 | * __builtin_types_compatible_p(unsigned long, unsigned long long) = 0. | ||
202 | */ | ||
203 | if (sizeof(unsigned long) == sizeof(unsigned long long) && | ||
204 | __alignof__(unsigned long) == __alignof__(unsigned long long)) | ||
205 | return kstrtoull(s, base, (unsigned long long *)res); | ||
206 | else | ||
207 | return _kstrtoul(s, base, res); | ||
208 | } | ||
209 | |||
210 | static inline int __must_check kstrtol(const char *s, unsigned int base, long *res) | ||
211 | { | ||
212 | /* | ||
213 | * We want to shortcut function call, but | ||
214 | * __builtin_types_compatible_p(long, long long) = 0. | ||
215 | */ | ||
216 | if (sizeof(long) == sizeof(long long) && | ||
217 | __alignof__(long) == __alignof__(long long)) | ||
218 | return kstrtoll(s, base, (long long *)res); | ||
219 | else | ||
220 | return _kstrtol(s, base, res); | ||
221 | } | ||
222 | |||
223 | int __must_check kstrtouint(const char *s, unsigned int base, unsigned int *res); | ||
224 | int __must_check kstrtoint(const char *s, unsigned int base, int *res); | ||
225 | |||
226 | static inline int __must_check kstrtou64(const char *s, unsigned int base, u64 *res) | ||
227 | { | ||
228 | return kstrtoull(s, base, res); | ||
229 | } | ||
230 | |||
231 | static inline int __must_check kstrtos64(const char *s, unsigned int base, s64 *res) | ||
232 | { | ||
233 | return kstrtoll(s, base, res); | ||
234 | } | ||
235 | |||
236 | static inline int __must_check kstrtou32(const char *s, unsigned int base, u32 *res) | ||
237 | { | ||
238 | return kstrtouint(s, base, res); | ||
239 | } | ||
240 | |||
241 | static inline int __must_check kstrtos32(const char *s, unsigned int base, s32 *res) | ||
242 | { | ||
243 | return kstrtoint(s, base, res); | ||
244 | } | ||
245 | |||
246 | int __must_check kstrtou16(const char *s, unsigned int base, u16 *res); | ||
247 | int __must_check kstrtos16(const char *s, unsigned int base, s16 *res); | ||
248 | int __must_check kstrtou8(const char *s, unsigned int base, u8 *res); | ||
249 | int __must_check kstrtos8(const char *s, unsigned int base, s8 *res); | ||
250 | |||
190 | extern unsigned long simple_strtoul(const char *,char **,unsigned int); | 251 | extern unsigned long simple_strtoul(const char *,char **,unsigned int); |
191 | extern long simple_strtol(const char *,char **,unsigned int); | 252 | extern long simple_strtol(const char *,char **,unsigned int); |
192 | extern unsigned long long simple_strtoull(const char *,char **,unsigned int); | 253 | extern unsigned long long simple_strtoull(const char *,char **,unsigned int); |
193 | extern long long simple_strtoll(const char *,char **,unsigned int); | 254 | extern long long simple_strtoll(const char *,char **,unsigned int); |
194 | extern int __must_check strict_strtoul(const char *, unsigned int, unsigned long *); | 255 | #define strict_strtoul kstrtoul |
195 | extern int __must_check strict_strtol(const char *, unsigned int, long *); | 256 | #define strict_strtol kstrtol |
196 | extern int __must_check strict_strtoull(const char *, unsigned int, unsigned long long *); | 257 | #define strict_strtoull kstrtoull |
197 | extern int __must_check strict_strtoll(const char *, unsigned int, long long *); | 258 | #define strict_strtoll kstrtoll |
259 | |||
198 | extern int sprintf(char * buf, const char * fmt, ...) | 260 | extern int sprintf(char * buf, const char * fmt, ...) |
199 | __attribute__ ((format (printf, 2, 3))); | 261 | __attribute__ ((format (printf, 2, 3))); |
200 | extern int vsprintf(char *buf, const char *, va_list) | 262 | extern int vsprintf(char *buf, const char *, va_list) |