str2int.cc 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /* Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License, version 2.0,
  4. as published by the Free Software Foundation.
  5. This program is also distributed with certain software (including
  6. but not limited to OpenSSL) that is licensed under separate terms,
  7. as designated in a particular file or component or in included license
  8. documentation. The authors of MySQL hereby grant you an additional
  9. permission to link the program and your derivative works with the
  10. separately licensed software that they have included with MySQL.
  11. Without limiting anything contained in the foregoing, this file,
  12. which is part of C Driver for MySQL (Connector/C), is also subject to the
  13. Universal FOSS Exception, version 1.0, a copy of which can be found at
  14. http://oss.oracle.com/licenses/universal-foss-exception.
  15. This program is distributed in the hope that it will be useful,
  16. but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. GNU General Public License, version 2.0, for more details.
  19. You should have received a copy of the GNU General Public License
  20. along with this program; if not, write to the Free Software
  21. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
  22. /*
  23. str2int(src, radix, lower, upper, &val)
  24. converts the string pointed to by src to an integer and stores it in
  25. val. It skips leading spaces and tabs (but not newlines, formfeeds,
  26. backspaces), then it accepts an optional sign and a sequence of digits
  27. in the specified radix. The result should satisfy lower <= *val <= upper.
  28. The result is a pointer to the first character after the number;
  29. trailing spaces will NOT be skipped.
  30. If an error is detected, the result will be NullS, the value put
  31. in val will be 0, and errno will be set to
  32. EDOM if there are no digits
  33. ERANGE if the result would overflow or otherwise fail to lie
  34. within the specified bounds.
  35. Check that the bounds are right for your machine.
  36. This looks amazingly complicated for what you probably thought was an
  37. easy task. Coping with integer overflow and the asymmetric range of
  38. twos complement machines is anything but easy.
  39. So that users of atoi and atol can check whether an error occurred,
  40. I have taken a wholly unprecedented step: errno is CLEARED if this
  41. call has no problems.
  42. */
  43. #include <errno.h>
  44. #include <limits.h>
  45. #include "m_ctype.h"
  46. #include "m_string.h" // IWYU pragma: keep
  47. #define char_val(X) \
  48. (X >= '0' && X <= '9' \
  49. ? X - '0' \
  50. : X >= 'A' && X <= 'Z' ? X - 'A' + 10 \
  51. : X >= 'a' && X <= 'z' ? X - 'a' + 10 : '\177')
  52. const char *str2int(const char *src, int radix, long int lower, long int upper,
  53. long int *val) {
  54. int sign; /* is number negative (+1) or positive (-1) */
  55. int n; /* number of digits yet to be converted */
  56. long limit; /* "largest" possible valid input */
  57. long scale; /* the amount to multiply next digit by */
  58. long sofar; /* the running value */
  59. int d; /* (negative of) next digit */
  60. const char *start;
  61. int digits[32]; /* Room for numbers */
  62. /* Make sure *val is sensible in case of error */
  63. *val = 0;
  64. /* Check that the radix is in the range 2..36 */
  65. #ifndef DBUG_OFF
  66. if (radix < 2 || radix > 36) {
  67. errno = EDOM;
  68. return nullptr;
  69. }
  70. #endif
  71. /* The basic problem is: how do we handle the conversion of
  72. a number without resorting to machine-specific code to
  73. check for overflow? Obviously, we have to ensure that
  74. no calculation can overflow. We are guaranteed that the
  75. "lower" and "upper" arguments are valid machine integers.
  76. On sign-and-magnitude, twos-complement, and ones-complement
  77. machines all, if +|n| is representable, so is -|n|, but on
  78. twos complement machines the converse is not true. So the
  79. "maximum" representable number has a negative representative.
  80. Limit is set to min(-|lower|,-|upper|); this is the "largest"
  81. number we are concerned with. */
  82. /* Calculate Limit using Scale as a scratch variable */
  83. if ((limit = lower) > 0) limit = -limit;
  84. if ((scale = upper) > 0) scale = -scale;
  85. if (scale < limit) limit = scale;
  86. /* Skip leading spaces and check for a sign.
  87. Note: because on a 2s complement machine MinLong is a valid
  88. integer but |MinLong| is not, we have to keep the current
  89. converted value (and the scale!) as *negative* numbers,
  90. so the sign is the opposite of what you might expect.
  91. */
  92. while (my_isspace(&my_charset_latin1, *src)) src++;
  93. sign = -1;
  94. if (*src == '+')
  95. src++;
  96. else if (*src == '-')
  97. src++, sign = 1;
  98. /* Skip leading zeros so that we never compute a power of radix
  99. in scale that we won't have a need for. Otherwise sticking
  100. enough 0s in front of a number could cause the multiplication
  101. to overflow when it neededn't.
  102. */
  103. start = src;
  104. while (*src == '0') src++;
  105. /* Move over the remaining digits. We have to convert from left
  106. to left in order to avoid overflow. Answer is after last digit.
  107. */
  108. for (n = 0; (digits[n] = char_val(*src)) < radix && n < 20; n++, src++)
  109. ;
  110. /* Check that there is at least one digit */
  111. if (start == src) {
  112. errno = EDOM;
  113. return nullptr;
  114. }
  115. /* The invariant we want to maintain is that src is just
  116. to the right of n digits, we've converted k digits to
  117. sofar, scale = -radix**k, and scale < sofar < 0. Now
  118. if the final number is to be within the original
  119. Limit, we must have (to the left)*scale+sofar >= Limit,
  120. or (to the left)*scale >= Limit-sofar, i.e. the digits
  121. to the left of src must form an integer <= (Limit-sofar)/(scale).
  122. In particular, this is true of the next digit. In our
  123. incremental calculation of Limit,
  124. IT IS VITAL that (-|N|)/(-|D|) = |N|/|D|
  125. */
  126. for (sofar = 0, scale = -1; --n >= 1;) {
  127. if ((long)-(d = digits[n]) < limit) {
  128. errno = ERANGE;
  129. return nullptr;
  130. }
  131. limit = (limit + d) / radix, sofar += d * scale;
  132. scale *= radix;
  133. }
  134. if (n == 0) {
  135. if ((long)-(d = digits[n]) < limit) /* get last digit */
  136. {
  137. errno = ERANGE;
  138. return nullptr;
  139. }
  140. sofar += d * scale;
  141. }
  142. /* Now it might still happen that sofar = -32768 or its equivalent,
  143. so we can't just multiply by the sign and check that the result
  144. is in the range lower..upper. All of this caution is a right
  145. pain in the neck. If only there were a standard routine which
  146. says generate thus and such a signal on integer overflow...
  147. But not enough machines can do it *SIGH*.
  148. */
  149. if (sign < 0) {
  150. if (sofar < -LONG_MAX || (sofar = -sofar) > upper) {
  151. errno = ERANGE;
  152. return nullptr;
  153. }
  154. } else if (sofar < lower) {
  155. errno = ERANGE;
  156. return nullptr;
  157. }
  158. *val = sofar;
  159. errno = 0; /* indicate that all went well */
  160. return src;
  161. }