| 1 | /* SPDX-License-Identifier: (GPL-2.0 or LGPL-2.1) |
| 2 | * |
| 3 | * wrapper/kstrtox.h |
| 4 | * |
| 5 | * Copyright (C) 2015 Michael Jeanson <mjeanson@efficios.com> |
| 6 | */ |
| 7 | |
| 8 | #ifndef _LTTNG_WRAPPER_KSTRTOX_H |
| 9 | #define _LTTNG_WRAPPER_KSTRTOX_H |
| 10 | |
| 11 | #include <linux/version.h> |
| 12 | |
| 13 | #if (LINUX_VERSION_CODE < KERNEL_VERSION(3,0,0)) |
| 14 | |
| 15 | #include <linux/uaccess.h> |
| 16 | |
| 17 | /* Excludes final \0. */ |
| 18 | #define LTTNG_MAX_UINT_CHAR 10 |
| 19 | |
| 20 | static inline |
| 21 | int __must_check lttng_kstrtouint_from_user(const char __user *ubuf, |
| 22 | size_t count, unsigned int base, unsigned int *res) |
| 23 | { |
| 24 | unsigned int _res; |
| 25 | char kbuf[LTTNG_MAX_UINT_CHAR + 1], *endptr; |
| 26 | |
| 27 | memset(kbuf, 0, sizeof(kbuf)); |
| 28 | if (copy_from_user(kbuf, ubuf, min_t(size_t, LTTNG_MAX_UINT_CHAR, count))) |
| 29 | return -EFAULT; |
| 30 | |
| 31 | _res = simple_strtoul(kbuf, &endptr, base); |
| 32 | if (!endptr) |
| 33 | return -EINVAL; |
| 34 | |
| 35 | *res = _res; |
| 36 | return 0; |
| 37 | } |
| 38 | #else |
| 39 | static inline |
| 40 | int __must_check lttng_kstrtouint_from_user(const char __user *ubuf, |
| 41 | size_t count, unsigned int base, unsigned int *res) |
| 42 | { |
| 43 | return kstrtouint_from_user(ubuf, count, base, res); |
| 44 | } |
| 45 | #endif |
| 46 | |
| 47 | #endif /* _LTTNG_WRAPPER_KSTRTOX_H */ |