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

Side by Side Diff: src/conversions-inl.h

Issue 2548243004: Return false in TryNumberToSize if there is a cast error (Closed)
Patch Set: Return false in TryNumberToSize if the number is 1 << 64. Created 4 years 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
« no previous file with comments | « AUTHORS ('k') | test/cctest/test-conversions.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2011 the V8 project authors. All rights reserved. 1 // Copyright 2011 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef V8_CONVERSIONS_INL_H_ 5 #ifndef V8_CONVERSIONS_INL_H_
6 #define V8_CONVERSIONS_INL_H_ 6 #define V8_CONVERSIONS_INL_H_
7 7
8 #include <float.h> // Required for DBL_MAX and on Win32 for finite() 8 #include <float.h> // Required for DBL_MAX and on Win32 for finite()
9 #include <limits.h> // Required for INT_MAX etc. 9 #include <limits.h> // Required for INT_MAX etc.
10 #include <stdarg.h> 10 #include <stdarg.h>
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after
147 DCHECK(static_cast<unsigned>(Smi::kMaxValue) <= 147 DCHECK(static_cast<unsigned>(Smi::kMaxValue) <=
148 std::numeric_limits<size_t>::max()); 148 std::numeric_limits<size_t>::max());
149 if (value >= 0) { 149 if (value >= 0) {
150 *result = static_cast<size_t>(value); 150 *result = static_cast<size_t>(value);
151 return true; 151 return true;
152 } 152 }
153 return false; 153 return false;
154 } else { 154 } else {
155 DCHECK(number->IsHeapNumber()); 155 DCHECK(number->IsHeapNumber());
156 double value = HeapNumber::cast(number)->value(); 156 double value = HeapNumber::cast(number)->value();
157 if (value >= 0 && value <= std::numeric_limits<size_t>::max()) { 157 // If value is compared directly to the limit, the limit will be
158 // casted to a double and could end up as limit + 1,
159 // because a double might not have enough mantissa bits for it.
160 // So we might as well cast the limit first, and use < instead of <=.
161 double maxSize = static_cast<double>(std::numeric_limits<size_t>::max());
162 if (value >= 0 && value < maxSize) {
158 *result = static_cast<size_t>(value); 163 *result = static_cast<size_t>(value);
159 return true; 164 return true;
160 } else { 165 } else {
161 return false; 166 return false;
162 } 167 }
163 } 168 }
164 } 169 }
165 170
166 size_t NumberToSize(Object* number) { 171 size_t NumberToSize(Object* number) {
167 size_t result = 0; 172 size_t result = 0;
(...skipping 597 matching lines...) Expand 10 before | Expand all | Expand 10 after
765 buffer[buffer_pos] = '\0'; 770 buffer[buffer_pos] = '\0';
766 771
767 double converted = Strtod(Vector<const char>(buffer, buffer_pos), exponent); 772 double converted = Strtod(Vector<const char>(buffer, buffer_pos), exponent);
768 return (sign == NEGATIVE) ? -converted : converted; 773 return (sign == NEGATIVE) ? -converted : converted;
769 } 774 }
770 775
771 } // namespace internal 776 } // namespace internal
772 } // namespace v8 777 } // namespace v8
773 778
774 #endif // V8_CONVERSIONS_INL_H_ 779 #endif // V8_CONVERSIONS_INL_H_
OLDNEW
« no previous file with comments | « AUTHORS ('k') | test/cctest/test-conversions.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698