OLD | NEW |
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 Loading... |
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 Loading... |
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_ |
OLD | NEW |