1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Convert integer string representation to an integer. 4 * If an integer doesn't fit into specified type, -E is returned. 5 * 6 * Integer starts with optional sign. 7 * kstrtou*() functions do not accept sign "-". 8 * 9 * Radix 0 means autodetection: leading "0x" implies radix 16, 10 * leading "0" implies radix 8, otherwise radix is 10. 11 * Autodetection hints work after optional sign, but not before. 12 * 13 * If -E is returned, result is not touched. 14 */ 15 #include <linux/ctype.h> 16 #include <linux/errno.h> 17 #include <linux/export.h> 18 #include <linux/kstrtox.h> 19 #include <linux/math64.h> 20 #include <linux/overflow.h> 21 #include <linux/types.h> 22 #include <linux/uaccess.h> 23 24 #include "kstrtox.h" 25 26 noinline 27 const char *_parse_integer_fixup_radix(const char *s, unsigned int *base) 28 { 29 if (*base == 0) { 30 if (s[0] == '0') { 31 if (_tolower(s[1]) == 'x' && isxdigit(s[2])) 32 *base = 16; 33 else 34 *base = 8; 35 } else 36 *base = 10; 37 } 38 if (*base == 16 && s[0] == '0' && _tolower(s[1]) == 'x') 39 s += 2; 40 return s; 41 } 42 43 /** 44 * _parse_integer_limit - Convert integer string representation to an integer 45 * @s: Integer string representation 46 * @base: Radix 47 * @p: Where to store result 48 * @max_chars: Maximum amount of characters to convert 49 * @init: Initial value of the multiply-accumulate result 50 * 51 * Convert non-negative integer string representation in explicitly given 52 * radix to an integer. If overflow occurs, value at @p is set to ULLONG_MAX. 53 * 54 * This function is the workhorse of other string conversion functions and it 55 * is discouraged to use it explicitly. Consider kstrto*() family instead. 56 * 57 * Return: Number of characters consumed, maybe ORed with overflow bit 58 */ 59 noinline 60 unsigned int _parse_integer_limit(const char *s, unsigned int base, unsigned long long *p, 61 size_t max_chars, unsigned long long init) 62 { 63 unsigned int rv, overflow = 0; 64 unsigned long long res; 65 66 res = init; 67 for (rv = 0; rv < max_chars; rv++, s++) { 68 unsigned int c = *s; 69 unsigned int lc = _tolower(c); 70 unsigned int val; 71 72 if ('0' <= c && c <= '9') 73 val = c - '0'; 74 else if ('a' <= lc && lc <= 'f') 75 val = lc - 'a' + 10; 76 else 77 break; 78 79 if (val >= base) 80 break; 81 /* 82 * Check for overflow only if we are within range of 83 * it in the max base we support (16) 84 */ 85 if (unlikely(res & (~0ull << 60))) { 86 if (check_mul_overflow(res, base, &res) || 87 check_add_overflow(res, val, &res)) { 88 res = ULLONG_MAX; 89 overflow = KSTRTOX_OVERFLOW; 90 } 91 } else { 92 res = res * base + val; 93 } 94 } 95 *p = res; 96 return rv | overflow; 97 } 98 99 static int _kstrtoull(const char *s, unsigned int base, unsigned long long *res) 100 { 101 unsigned long long _res; 102 unsigned int rv; 103 104 s = _parse_integer_fixup_radix(s, &base); 105 rv = _parse_integer(s, base, &_res); 106 if (rv & KSTRTOX_OVERFLOW) 107 return -ERANGE; 108 if (rv == 0) 109 return -EINVAL; 110 s += rv; 111 if (*s == '\n') 112 s++; 113 if (*s) 114 return -EINVAL; 115 *res = _res; 116 return 0; 117 } 118 119 /** 120 * kstrtoull - convert a string to an unsigned long long 121 * @s: The start of the string. The string must be null-terminated, and may also 122 * include a single newline before its terminating null. The first character 123 * may also be a plus sign, but not a minus sign. 124 * @base: The number base to use. The maximum supported base is 16. If base is 125 * given as 0, then the base of the string is automatically detected with the 126 * conventional semantics - If it begins with 0x the number will be parsed as a 127 * hexadecimal (case insensitive), if it otherwise begins with 0, it will be 128 * parsed as an octal number. Otherwise it will be parsed as a decimal. 129 * @res: Where to write the result of the conversion on success. 130 * 131 * Returns 0 on success, -ERANGE on overflow and -EINVAL on parsing error. 132 * Preferred over simple_strtoull(). Return code must be checked. 133 */ 134 noinline 135 int kstrtoull(const char *s, unsigned int base, unsigned long long *res) 136 { 137 if (s[0] == '+') 138 s++; 139 return _kstrtoull(s, base, res); 140 } 141 EXPORT_SYMBOL(kstrtoull); 142 143 /** 144 * kstrtoll - convert a string to a long long 145 * @s: The start of the string. The string must be null-terminated, and may also 146 * include a single newline before its terminating null. The first character 147 * may also be a plus sign or a minus sign. 148 * @base: The number base to use. The maximum supported base is 16. If base is 149 * given as 0, then the base of the string is automatically detected with the 150 * conventional semantics - If it begins with 0x the number will be parsed as a 151 * hexadecimal (case insensitive), if it otherwise begins with 0, it will be 152 * parsed as an octal number. Otherwise it will be parsed as a decimal. 153 * @res: Where to write the result of the conversion on success. 154 * 155 * Returns 0 on success, -ERANGE on overflow and -EINVAL on parsing error. 156 * Preferred over simple_strtoll(). Return code must be checked. 157 */ 158 noinline 159 int kstrtoll(const char *s, unsigned int base, long long *res) 160 { 161 unsigned long long tmp; 162 int rv; 163 164 if (s[0] == '-') { 165 rv = _kstrtoull(s + 1, base, &tmp); 166 if (rv < 0) 167 return rv; 168 if ((long long)-tmp > 0) 169 return -ERANGE; 170 *res = -tmp; 171 } else { 172 rv = kstrtoull(s, base, &tmp); 173 if (rv < 0) 174 return rv; 175 if ((long long)tmp < 0) 176 return -ERANGE; 177 *res = tmp; 178 } 179 return 0; 180 } 181 EXPORT_SYMBOL(kstrtoll); 182 183 /* Internal, do not use. */ 184 int _kstrtoul(const char *s, unsigned int base, unsigned long *res) 185 { 186 unsigned long long tmp; 187 int rv; 188 189 rv = kstrtoull(s, base, &tmp); 190 if (rv < 0) 191 return rv; 192 if (tmp != (unsigned long)tmp) 193 return -ERANGE; 194 *res = tmp; 195 return 0; 196 } 197 EXPORT_SYMBOL(_kstrtoul); 198 199 /* Internal, do not use. */ 200 int _kstrtol(const char *s, unsigned int base, long *res) 201 { 202 long long tmp; 203 int rv; 204 205 rv = kstrtoll(s, base, &tmp); 206 if (rv < 0) 207 return rv; 208 if (tmp != (long)tmp) 209 return -ERANGE; 210 *res = tmp; 211 return 0; 212 } 213 EXPORT_SYMBOL(_kstrtol); 214 215 /** 216 * kstrtouint - convert a string to an unsigned int 217 * @s: The start of the string. The string must be null-terminated, and may also 218 * include a single newline before its terminating null. The first character 219 * may also be a plus sign, but not a minus sign. 220 * @base: The number base to use. The maximum supported base is 16. If base is 221 * given as 0, then the base of the string is automatically detected with the 222 * conventional semantics - If it begins with 0x the number will be parsed as a 223 * hexadecimal (case insensitive), if it otherwise begins with 0, it will be 224 * parsed as an octal number. Otherwise it will be parsed as a decimal. 225 * @res: Where to write the result of the conversion on success. 226 * 227 * Returns 0 on success, -ERANGE on overflow and -EINVAL on parsing error. 228 * Preferred over simple_strtoul(). Return code must be checked. 229 */ 230 noinline 231 int kstrtouint(const char *s, unsigned int base, unsigned int *res) 232 { 233 unsigned long long tmp; 234 int rv; 235 236 rv = kstrtoull(s, base, &tmp); 237 if (rv < 0) 238 return rv; 239 if (tmp != (unsigned int)tmp) 240 return -ERANGE; 241 *res = tmp; 242 return 0; 243 } 244 EXPORT_SYMBOL(kstrtouint); 245 246 /** 247 * kstrtoint - convert a string to an int 248 * @s: The start of the string. The string must be null-terminated, and may also 249 * include a single newline before its terminating null. The first character 250 * may also be a plus sign or a minus sign. 251 * @base: The number base to use. The maximum supported base is 16. If base is 252 * given as 0, then the base of the string is automatically detected with the 253 * conventional semantics - If it begins with 0x the number will be parsed as a 254 * hexadecimal (case insensitive), if it otherwise begins with 0, it will be 255 * parsed as an octal number. Otherwise it will be parsed as a decimal. 256 * @res: Where to write the result of the conversion on success. 257 * 258 * Returns 0 on success, -ERANGE on overflow and -EINVAL on parsing error. 259 * Preferred over simple_strtol(). Return code must be checked. 260 */ 261 noinline 262 int kstrtoint(const char *s, unsigned int base, int *res) 263 { 264 long long tmp; 265 int rv; 266 267 rv = kstrtoll(s, base, &tmp); 268 if (rv < 0) 269 return rv; 270 if (tmp != (int)tmp) 271 return -ERANGE; 272 *res = tmp; 273 return 0; 274 } 275 EXPORT_SYMBOL(kstrtoint); 276 277 noinline 278 int kstrtou16(const char *s, unsigned int base, u16 *res) 279 { 280 unsigned long long tmp; 281 int rv; 282 283 rv = kstrtoull(s, base, &tmp); 284 if (rv < 0) 285 return rv; 286 if (tmp != (u16)tmp) 287 return -ERANGE; 288 *res = tmp; 289 return 0; 290 } 291 EXPORT_SYMBOL(kstrtou16); 292 293 noinline 294 int kstrtos16(const char *s, unsigned int base, s16 *res) 295 { 296 long long tmp; 297 int rv; 298 299 rv = kstrtoll(s, base, &tmp); 300 if (rv < 0) 301 return rv; 302 if (tmp != (s16)tmp) 303 return -ERANGE; 304 *res = tmp; 305 return 0; 306 } 307 EXPORT_SYMBOL(kstrtos16); 308 309 noinline 310 int kstrtou8(const char *s, unsigned int base, u8 *res) 311 { 312 unsigned long long tmp; 313 int rv; 314 315 rv = kstrtoull(s, base, &tmp); 316 if (rv < 0) 317 return rv; 318 if (tmp != (u8)tmp) 319 return -ERANGE; 320 *res = tmp; 321 return 0; 322 } 323 EXPORT_SYMBOL(kstrtou8); 324 325 noinline 326 int kstrtos8(const char *s, unsigned int base, s8 *res) 327 { 328 long long tmp; 329 int rv; 330 331 rv = kstrtoll(s, base, &tmp); 332 if (rv < 0) 333 return rv; 334 if (tmp != (s8)tmp) 335 return -ERANGE; 336 *res = tmp; 337 return 0; 338 } 339 EXPORT_SYMBOL(kstrtos8); 340 341 /** 342 * kstrtobool - convert common user inputs into boolean values 343 * @s: input string 344 * @res: result 345 * 346 * This routine returns 0 iff the first character is one of 'EeYyTt1DdNnFf0', 347 * or [oO][NnFf] for "on" and "off". Otherwise it will return -EINVAL. Value 348 * pointed to by res is updated upon finding a match. 349 */ 350 noinline 351 int kstrtobool(const char *s, bool *res) 352 { 353 if (!s) 354 return -EINVAL; 355 356 switch (s[0]) { 357 case 'e': 358 case 'E': 359 case 'y': 360 case 'Y': 361 case 't': 362 case 'T': 363 case '1': 364 *res = true; 365 return 0; 366 case 'd': 367 case 'D': 368 case 'n': 369 case 'N': 370 case 'f': 371 case 'F': 372 case '0': 373 *res = false; 374 return 0; 375 case 'o': 376 case 'O': 377 switch (s[1]) { 378 case 'n': 379 case 'N': 380 *res = true; 381 return 0; 382 case 'f': 383 case 'F': 384 *res = false; 385 return 0; 386 default: 387 break; 388 } 389 break; 390 default: 391 break; 392 } 393 394 return -EINVAL; 395 } 396 EXPORT_SYMBOL(kstrtobool); 397 398 static int _kstrtoudec64(const char *s, unsigned int scale, u64 *res) 399 { 400 unsigned int rv_int, rv_frac; 401 u64 _res = 0; 402 403 rv_int = _parse_integer(s, 10, &_res); 404 if (rv_int & KSTRTOX_OVERFLOW) 405 return -ERANGE; 406 s += rv_int; 407 408 if (*s == '.') 409 s++; /* skip decimal point */ 410 411 rv_frac = _parse_integer(s, 10, &_res, scale, _res); 412 if (rv_frac & KSTRTOX_OVERFLOW) 413 return -ERANGE; 414 s += rv_frac; 415 416 /* 417 * Check input beyond rv_int and rv_frac to cover cases like ".5" with 418 * scale 0, which is considered a valid input, being parsed as 0. 419 */ 420 if (!rv_int && !rv_frac && !isdigit(*s)) 421 return -EINVAL; 422 423 while (isdigit(*s)) /* truncate digits */ 424 s++; 425 426 if (*s == '\n') 427 s++; 428 if (*s) 429 return -EINVAL; 430 431 if (_res && ((scale - rv_frac) > 19 /* log10(2^64) = 19.26 */ || 432 check_mul_overflow(_res, int_pow(10, scale - rv_frac), &_res))) 433 return -ERANGE; 434 435 *res = _res; 436 return 0; 437 } 438 439 /** 440 * kstrtoudec64() - Convert a string to an unsigned 64-bit scaled decimal value. 441 * @s: The start of the string. The string must be null-terminated, and may also 442 * include a single newline before its terminating null. The first character 443 * may also be a plus sign, but not a minus sign. 444 * @scale: The number of digits to the right of the decimal point. 445 * @res: Where to write the result of the conversion on success. 446 * 447 * For example, a scale of 3 with input "123.45" results in 123450. Note that 448 * trailing zeros in the fractional part input to match the scale are not 449 * required. Also, digits beyond the specified scale are ignored. 450 * 451 * Return: 0 on success, -ERANGE on overflow and -EINVAL on parsing error. 452 */ 453 noinline 454 int kstrtoudec64(const char *s, unsigned int scale, u64 *res) 455 { 456 if (s[0] == '+') 457 s++; 458 return _kstrtoudec64(s, scale, res); 459 } 460 EXPORT_SYMBOL(kstrtoudec64); 461 462 /** 463 * kstrtodec64() - Convert a string to a signed 64-bit scaled decimal value. 464 * @s: The start of the string. The string must be null-terminated, and may also 465 * include a single newline before its terminating null. The first character 466 * may also be a plus sign or a minus sign. 467 * @scale: The number of digits to the right of the decimal point. 468 * @res: Where to write the result of the conversion on success. 469 * 470 * For example, a scale of 4 with input "-3.141592" results in -31415. Note 471 * that digits beyond the specified scale are ignored. Also, trailing zeros in 472 * the fractional part input to match the scale are not required. 473 * 474 * Return: 0 on success, -ERANGE on overflow and -EINVAL on parsing error. 475 */ 476 noinline 477 int kstrtodec64(const char *s, unsigned int scale, s64 *res) 478 { 479 u64 tmp; 480 int rv; 481 482 if (s[0] == '-') { 483 rv = _kstrtoudec64(s + 1, scale, &tmp); 484 if (rv < 0) 485 return rv; 486 if ((s64)-tmp > 0) 487 return -ERANGE; 488 *res = -tmp; 489 } else { 490 rv = kstrtoudec64(s, scale, &tmp); 491 if (rv < 0) 492 return rv; 493 if ((s64)tmp < 0) 494 return -ERANGE; 495 *res = tmp; 496 } 497 return 0; 498 } 499 EXPORT_SYMBOL(kstrtodec64); 500 501 /* 502 * Since "base" would be a nonsense argument, this open-codes the 503 * _from_user helper instead of using the helper macro below. 504 */ 505 int kstrtobool_from_user(const char __user *s, size_t count, bool *res) 506 { 507 /* Longest string needed to differentiate, newline, terminator */ 508 char buf[4]; 509 510 count = min(count, sizeof(buf) - 1); 511 if (copy_from_user(buf, s, count)) 512 return -EFAULT; 513 buf[count] = '\0'; 514 return kstrtobool(buf, res); 515 } 516 EXPORT_SYMBOL(kstrtobool_from_user); 517 518 #define kstrto_from_user(f, g, type) \ 519 int f(const char __user *s, size_t count, unsigned int base, type *res) \ 520 { \ 521 /* sign, base 2 representation, newline, terminator */ \ 522 char buf[1 + sizeof(type) * 8 + 1 + 1]; \ 523 \ 524 count = min(count, sizeof(buf) - 1); \ 525 if (copy_from_user(buf, s, count)) \ 526 return -EFAULT; \ 527 buf[count] = '\0'; \ 528 return g(buf, base, res); \ 529 } \ 530 EXPORT_SYMBOL(f) 531 532 kstrto_from_user(kstrtoull_from_user, kstrtoull, unsigned long long); 533 kstrto_from_user(kstrtoll_from_user, kstrtoll, long long); 534 kstrto_from_user(kstrtoul_from_user, kstrtoul, unsigned long); 535 kstrto_from_user(kstrtol_from_user, kstrtol, long); 536 kstrto_from_user(kstrtouint_from_user, kstrtouint, unsigned int); 537 kstrto_from_user(kstrtoint_from_user, kstrtoint, int); 538 kstrto_from_user(kstrtou16_from_user, kstrtou16, u16); 539 kstrto_from_user(kstrtos16_from_user, kstrtos16, s16); 540 kstrto_from_user(kstrtou8_from_user, kstrtou8, u8); 541 kstrto_from_user(kstrtos8_from_user, kstrtos8, s8); 542