Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(299)

Side by Side Diff: runtime/lib/integers_patch.dart

Issue 2987003002: [corelib] Add explicit overflow checks to int.parse (Closed)
Patch Set: Created 3 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 // Dart core library. 4 // Dart core library.
5 5
6 // VM implementation of int. 6 // VM implementation of int.
7 7
8 @patch 8 @patch
9 class int { 9 class int {
10 @patch 10 @patch
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after
125 while (smallBlockSize >= blockSize) smallBlockSize -= blockSize; 125 while (smallBlockSize >= blockSize) smallBlockSize -= blockSize;
126 int result = 0; 126 int result = 0;
127 if (smallBlockSize > 0) { 127 if (smallBlockSize > 0) {
128 int blockEnd = start + smallBlockSize; 128 int blockEnd = start + smallBlockSize;
129 _Smi smi = _parseBlock(source, radix, start, blockEnd); 129 _Smi smi = _parseBlock(source, radix, start, blockEnd);
130 if (smi == null) return null; 130 if (smi == null) return null;
131 result = sign * smi; 131 result = sign * smi;
132 start = blockEnd; 132 start = blockEnd;
133 } 133 }
134 int multiplier = _PARSE_LIMITS[tableIndex + 1]; 134 int multiplier = _PARSE_LIMITS[tableIndex + 1];
135 int positiveOverflowLimit = _int64OverflowLimits[tableIndex];
136 int negativeOverflowLimit = _int64OverflowLimits[tableIndex + 1];
Lasse Reichstein Nielsen 2017/07/27 23:40:18 Only read these if they are needed (which goes wel
alexmarkov 2017/07/28 17:29:02 Done (see the newer CL).
135 int blockEnd = start + blockSize; 137 int blockEnd = start + blockSize;
136 do { 138 do {
137 _Smi smi = _parseBlock(source, radix, start, blockEnd); 139 _Smi smi = _parseBlock(source, radix, start, blockEnd);
138 if (smi == null) return null; 140 if (smi == null) return null;
141 if (_limitIntsTo64Bits) {
142 if (result >= positiveOverflowLimit) {
143 if ((result > positiveOverflowLimit) ||
144 (smi > (_maxInt64 - positiveOverflowLimit * multiplier))) {
145 return null;
146 }
147 } else if (result <= negativeOverflowLimit) {
148 if ((result < negativeOverflowLimit) ||
149 (smi > (negativeOverflowLimit * multiplier - _minInt64))) {
150 return null;
151 }
152 }
153 }
Lasse Reichstein Nielsen 2017/07/27 23:40:18 This increases the inner loop size significantly i
alexmarkov 2017/07/28 17:29:02 Optimizer should be able to remove "if (_limitInts
alexmarkov 2017/07/28 18:25:56 As I expected, both JIT and AOT optimizers are abl
139 result = (result * multiplier) + (sign * smi); 154 result = (result * multiplier) + (sign * smi);
140 start = blockEnd; 155 start = blockEnd;
141 blockEnd = start + blockSize; 156 blockEnd = start + blockSize;
142 } while (blockEnd <= end); 157 } while (blockEnd <= end);
143 return result; 158 return result;
144 } 159 }
145 160
146 // Parse block of digits into a Smi. 161 // Parse block of digits into a Smi.
147 static _Smi _parseBlock(String source, int radix, int start, int end) { 162 static _Smi _parseBlock(String source, int radix, int start, int end) {
148 _Smi result = 0; 163 _Smi result = 0;
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
199 6, 481890304, 12, 232218265089212416, 214 6, 481890304, 12, 232218265089212416,
200 6, 594823321, 12, 353814783205469041, 215 6, 594823321, 12, 353814783205469041,
201 6, 729000000, 12, 531441000000000000, // radix: 30 216 6, 729000000, 12, 531441000000000000, // radix: 30
202 6, 887503681, 12, 787662783788549761, 217 6, 887503681, 12, 787662783788549761,
203 6, 1073741824, 12, 1152921504606846976, 218 6, 1073741824, 12, 1152921504606846976,
204 5, 39135393, 12, 1667889514952984961, 219 5, 39135393, 12, 1667889514952984961,
205 5, 45435424, 12, 2386420683693101056, 220 5, 45435424, 12, 2386420683693101056,
206 5, 52521875, 12, 3379220508056640625, // radix: 35 221 5, 52521875, 12, 3379220508056640625, // radix: 35
207 5, 60466176, 11, 131621703842267136, 222 5, 60466176, 11, 131621703842267136,
208 ]; 223 ];
224
225 // Flag indicating if integers are limited by 64 bits
226 // (--limit-ints-to-64-bits mode is enabled).
Lasse Reichstein Nielsen 2017/07/27 23:40:18 Use doc-comments ("///"). It might make some edito
alexmarkov 2017/07/28 17:29:02 Done.
227 static const _limitIntsTo64Bits = ((1 << 64) == 0);
228
229 static const _maxInt64 = 0x7fffffffffffffff;
230 static const _minInt64 = -_maxInt64 - 1;
Lasse Reichstein Nielsen 2017/07/27 23:40:17 In the "limit integers to 64-bit mode", you should
alexmarkov 2017/07/28 17:29:02 It's a known problem and will be addressed separat
231
232 // In the --limit-ints-to-64-bits mode calculation of the expression
233 // result = (result * multiplier) + (sign * smi);
234 // in _parseRadix() may overflow 64-bit integers. In such case, int.parse()
235 // should stop with an error. It is safe to do this calculation until partial
236 // result reaches or exceeds limits in this table.
237 // For each multiplier from _PARSE_LIMITS[tableIndex + 1] the
238 // positive limit is at tableIndex, the negative limit is at tableIndex + 1.
239 static const _int64OverflowLimits = const [
zra 2017/07/27 21:22:38 Is there any way to populate this table lazily?
alexmarkov 2017/07/27 23:37:32 Done.
Lasse Reichstein Nielsen 2017/07/27 23:40:17 Don't make it const, then it's evaluated the first
alexmarkov 2017/07/28 17:29:02 Done.
240 _maxInt64 ~/ 1073741824,
241 _minInt64 ~/ 1073741824,
242 _maxInt64 ~/ 4611686018427387904,
243 _minInt64 ~/ 4611686018427387904,
244 _maxInt64 ~/ 387420489,
245 _minInt64 ~/ 387420489,
246 _maxInt64 ~/ 4052555153018976267,
247 _minInt64 ~/ 4052555153018976267,
248 _maxInt64 ~/ 1073741824,
249 _minInt64 ~/ 1073741824,
250 _maxInt64 ~/ 1152921504606846976,
251 _minInt64 ~/ 1152921504606846976,
252 _maxInt64 ~/ 244140625,
253 _minInt64 ~/ 244140625,
254 _maxInt64 ~/ 1490116119384765625,
255 _minInt64 ~/ 1490116119384765625,
256 _maxInt64 ~/ 362797056,
257 _minInt64 ~/ 362797056,
258 _maxInt64 ~/ 789730223053602816,
259 _minInt64 ~/ 789730223053602816,
260 _maxInt64 ~/ 282475249,
261 _minInt64 ~/ 282475249,
262 _maxInt64 ~/ 3909821048582988049,
263 _minInt64 ~/ 3909821048582988049,
264 _maxInt64 ~/ 1073741824,
265 _minInt64 ~/ 1073741824,
266 _maxInt64 ~/ 1152921504606846976,
267 _minInt64 ~/ 1152921504606846976,
268 _maxInt64 ~/ 387420489,
269 _minInt64 ~/ 387420489,
270 _maxInt64 ~/ 1350851717672992089,
271 _minInt64 ~/ 1350851717672992089,
272 _maxInt64 ~/ 1000000000,
273 _minInt64 ~/ 1000000000,
274 _maxInt64 ~/ 1000000000000000000,
275 _minInt64 ~/ 1000000000000000000,
276 _maxInt64 ~/ 214358881,
277 _minInt64 ~/ 214358881,
278 _maxInt64 ~/ 505447028499293771,
279 _minInt64 ~/ 505447028499293771,
280 _maxInt64 ~/ 429981696,
281 _minInt64 ~/ 429981696,
282 _maxInt64 ~/ 2218611106740436992,
283 _minInt64 ~/ 2218611106740436992,
284 _maxInt64 ~/ 815730721,
285 _minInt64 ~/ 815730721,
286 _maxInt64 ~/ 665416609183179841,
287 _minInt64 ~/ 665416609183179841,
288 _maxInt64 ~/ 105413504,
289 _minInt64 ~/ 105413504,
290 _maxInt64 ~/ 2177953337809371136,
291 _minInt64 ~/ 2177953337809371136,
292 _maxInt64 ~/ 170859375,
293 _minInt64 ~/ 170859375,
294 _maxInt64 ~/ 437893890380859375,
295 _minInt64 ~/ 437893890380859375,
296 _maxInt64 ~/ 268435456,
297 _minInt64 ~/ 268435456,
298 _maxInt64 ~/ 1152921504606846976,
299 _minInt64 ~/ 1152921504606846976,
300 _maxInt64 ~/ 410338673,
301 _minInt64 ~/ 410338673,
302 _maxInt64 ~/ 2862423051509815793,
303 _minInt64 ~/ 2862423051509815793,
304 _maxInt64 ~/ 612220032,
305 _minInt64 ~/ 612220032,
306 _maxInt64 ~/ 374813367582081024,
307 _minInt64 ~/ 374813367582081024,
308 _maxInt64 ~/ 893871739,
309 _minInt64 ~/ 893871739,
310 _maxInt64 ~/ 799006685782884121,
311 _minInt64 ~/ 799006685782884121,
312 _maxInt64 ~/ 64000000,
313 _minInt64 ~/ 64000000,
314 _maxInt64 ~/ 1638400000000000000,
315 _minInt64 ~/ 1638400000000000000,
316 _maxInt64 ~/ 85766121,
317 _minInt64 ~/ 85766121,
318 _maxInt64 ~/ 3243919932521508681,
319 _minInt64 ~/ 3243919932521508681,
320 _maxInt64 ~/ 113379904,
321 _minInt64 ~/ 113379904,
322 _maxInt64 ~/ 282810057883082752,
323 _minInt64 ~/ 282810057883082752,
324 _maxInt64 ~/ 148035889,
325 _minInt64 ~/ 148035889,
326 _maxInt64 ~/ 504036361936467383,
327 _minInt64 ~/ 504036361936467383,
328 _maxInt64 ~/ 191102976,
329 _minInt64 ~/ 191102976,
330 _maxInt64 ~/ 876488338465357824,
331 _minInt64 ~/ 876488338465357824,
332 _maxInt64 ~/ 244140625,
333 _minInt64 ~/ 244140625,
334 _maxInt64 ~/ 1490116119384765625,
335 _minInt64 ~/ 1490116119384765625,
336 _maxInt64 ~/ 308915776,
337 _minInt64 ~/ 308915776,
338 _maxInt64 ~/ 2481152873203736576,
339 _minInt64 ~/ 2481152873203736576,
340 _maxInt64 ~/ 387420489,
341 _minInt64 ~/ 387420489,
342 _maxInt64 ~/ 4052555153018976267,
343 _minInt64 ~/ 4052555153018976267,
344 _maxInt64 ~/ 481890304,
345 _minInt64 ~/ 481890304,
346 _maxInt64 ~/ 232218265089212416,
347 _minInt64 ~/ 232218265089212416,
348 _maxInt64 ~/ 594823321,
349 _minInt64 ~/ 594823321,
350 _maxInt64 ~/ 353814783205469041,
351 _minInt64 ~/ 353814783205469041,
352 _maxInt64 ~/ 729000000,
353 _minInt64 ~/ 729000000,
354 _maxInt64 ~/ 531441000000000000,
355 _minInt64 ~/ 531441000000000000,
356 _maxInt64 ~/ 887503681,
357 _minInt64 ~/ 887503681,
358 _maxInt64 ~/ 787662783788549761,
359 _minInt64 ~/ 787662783788549761,
360 _maxInt64 ~/ 1073741824,
361 _minInt64 ~/ 1073741824,
362 _maxInt64 ~/ 1152921504606846976,
363 _minInt64 ~/ 1152921504606846976,
364 _maxInt64 ~/ 39135393,
365 _minInt64 ~/ 39135393,
366 _maxInt64 ~/ 1667889514952984961,
367 _minInt64 ~/ 1667889514952984961,
368 _maxInt64 ~/ 45435424,
369 _minInt64 ~/ 45435424,
370 _maxInt64 ~/ 2386420683693101056,
371 _minInt64 ~/ 2386420683693101056,
372 _maxInt64 ~/ 52521875,
373 _minInt64 ~/ 52521875,
374 _maxInt64 ~/ 3379220508056640625,
375 _minInt64 ~/ 3379220508056640625,
376 _maxInt64 ~/ 60466176,
377 _minInt64 ~/ 60466176,
378 _maxInt64 ~/ 131621703842267136,
379 _minInt64 ~/ 131621703842267136,
380 ];
209 } 381 }
OLDNEW
« no previous file with comments | « no previous file | tests/corelib_2/corelib_2.status » ('j') | tests/corelib_2/int_parse_with_limited_ints_test.dart » ('J')

Powered by Google App Engine
This is Rietveld 408576698