Debian Codegraph @

unittest/gunit/strtoll-t.cc

raw symbols SCIP
1 /* Copyright (c) 2013, 2026, Oracle and/or its affiliates.
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License, version 2.0,
5 as published by the Free Software Foundation.
6
7 This program is designed to work with certain software (including
8 but not limited to OpenSSL) that is licensed under separate terms,
9 as designated in a particular file or component or in included license
10 documentation. The authors of MySQL hereby grant you an additional
11 permission to link the program and your derivative works with the
12 separately licensed software that they have either included with
13 the program or referenced in the documentation.
14
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
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24 #include <gtest/gtest.h>
25 #include <limits.h>
26
27 /*
28
29 ==== Purpose ====
30
31 Test if my_strtoll10 overflows values above unsigned long long
32 limit correctly.
33
34 ==== Related Bugs and Worklogs ====
35
36 BUG#16997513: MY_STRTOLL10 ACCEPTING OVERFLOWED UNSIGNED LONG LONG
37 VALUES AS NORMAL ONES
38
39 ==== Implementation ====
40
41 Check if my_strtoll10 returns the larger unsigned long long and raise
42 the overflow error when receiving a number like 18446744073709551915
43
44 */
45 #include "m_ctype.h"
46 #include "m_string.h"
47 #include "my_sys.h"
48
49 TEST(StringToULLTest, OverflowedNumber) {
50 unsigned long long number;
51 int error;
52 const char *str = "18446744073709551915";
53 number = my_strtoll10(str, nullptr, &error);
54 EXPECT_EQ(number, ULLONG_MAX);
55 EXPECT_EQ(error, MY_ERRNO_ERANGE);
56 }
57
58 TEST(StringToULLTest, MiscStrntoull10rndBugs) {
59 int error = 0;
60 const char *str;
61 const char *endptr;
62 unsigned long long number;
63
64 str = "-18446744073709551615";
65 number =
66 my_strntoull10rnd_8bit(nullptr, str, strlen(str), true, &endptr, &error);
67 EXPECT_EQ(0, number);
68 EXPECT_EQ(MY_ERRNO_ERANGE, error);
69 number =
70 my_strntoull10rnd_8bit(nullptr, str, strlen(str), false, &endptr, &error);
71 EXPECT_EQ(LLONG_MIN, number);
72 EXPECT_EQ(MY_ERRNO_ERANGE, error);
73
74 // At ret_too_big: check for (unsigned_flag && negative)
75 str = "-18446744073709551616";
76 number =
77 my_strntoull10rnd_8bit(nullptr, str, strlen(str), true, &endptr, &error);
78 EXPECT_EQ(0, number);
79 EXPECT_EQ(MY_ERRNO_ERANGE, error);
80 number =
81 my_strntoull10rnd_8bit(nullptr, str, strlen(str), false, &endptr, &error);
82 EXPECT_EQ(LLONG_MIN, number);
83 EXPECT_EQ(MY_ERRNO_ERANGE, error);
84
85 str = "-1e19";
86 number =
87 my_strntoull10rnd_8bit(nullptr, str, strlen(str), true, &endptr, &error);
88 EXPECT_EQ(0, number);
89 EXPECT_EQ(MY_ERRNO_ERANGE, error);
90 number =
91 my_strntoull10rnd_8bit(nullptr, str, strlen(str), false, &endptr, &error);
92 EXPECT_EQ(LLONG_MIN, number);
93 EXPECT_EQ(MY_ERRNO_ERANGE, error);
94
95 // At ret_too_big: check for (unsigned_flag && negative)
96 str = "-2e19";
97 number =
98 my_strntoull10rnd_8bit(nullptr, str, strlen(str), true, &endptr, &error);
99 EXPECT_EQ(0, number);
100 EXPECT_EQ(MY_ERRNO_ERANGE, error);
101 number =
102 my_strntoull10rnd_8bit(nullptr, str, strlen(str), false, &endptr, &error);
103 EXPECT_EQ(LLONG_MIN, number);
104 EXPECT_EQ(MY_ERRNO_ERANGE, error);
105
106 str = "0.9223372036854775807";
107 number =
108 my_strntoull10rnd_8bit(nullptr, str, strlen(str), true, &endptr, &error);
109 EXPECT_EQ(1, number);
110 EXPECT_EQ(0, error);
111 number =
112 my_strntoull10rnd_8bit(nullptr, str, strlen(str), false, &endptr, &error);
113 EXPECT_EQ(1, number);
114 EXPECT_EQ(0, error);
115
116 // (ull % d) * 2; overflowed to zero.
117 str = "0.9223372036854775808";
118 number =
119 my_strntoull10rnd_8bit(nullptr, str, strlen(str), true, &endptr, &error);
120 EXPECT_EQ(1, number);
121 EXPECT_EQ(0, error);
122 number =
123 my_strntoull10rnd_8bit(nullptr, str, strlen(str), false, &endptr, &error);
124 EXPECT_EQ(1, number);
125 EXPECT_EQ(0, error);
126
127 str = "1.2";
128 number =
129 my_strntoull10rnd_8bit(nullptr, str, strlen(str), true, &endptr, &error);
130 EXPECT_EQ(1, number);
131 EXPECT_EQ(0, error);
132 number =
133 my_strntoull10rnd_8bit(nullptr, str, strlen(str), false, &endptr, &error);
134 EXPECT_EQ(1, number);
135 EXPECT_EQ(0, error);
136
137 // On seeing the second dot, we need to calculate 'shift' and divide by 10.
138 str = "1.2.";
139 number =
140 my_strntoull10rnd_8bit(nullptr, str, strlen(str), true, &endptr, &error);
141 EXPECT_EQ(1, number);
142 EXPECT_EQ(0, error);
143 number =
144 my_strntoull10rnd_8bit(nullptr, str, strlen(str), false, &endptr, &error);
145 EXPECT_EQ(1, number);
146 EXPECT_EQ(0, error);
147
148 str = "92233720368547758000";
149 number =
150 my_strntoull10rnd_8bit(nullptr, str, strlen(str), true, &endptr, &error);
151 EXPECT_EQ(ULLONG_MAX, number);
152 EXPECT_EQ(MY_ERRNO_ERANGE, error);
153 number =
154 my_strntoull10rnd_8bit(nullptr, str, strlen(str), false, &endptr, &error);
155 EXPECT_EQ(LLONG_MAX, number);
156 EXPECT_EQ(MY_ERRNO_ERANGE, error);
157
158 // On seeing end-of-input, we still have to check for overflow.
159 str = "92233720368547758000e+";
160 number =
161 my_strntoull10rnd_8bit(nullptr, str, strlen(str), true, &endptr, &error);
162 EXPECT_EQ(ULLONG_MAX, number);
163 EXPECT_EQ(MY_ERRNO_ERANGE, error);
164 number =
165 my_strntoull10rnd_8bit(nullptr, str, strlen(str), false, &endptr, &error);
166 EXPECT_EQ(LLONG_MAX, number);
167 EXPECT_EQ(MY_ERRNO_ERANGE, error);
168 }