Commit cbaae3b6 authored by Toon Verwaest's avatar Toon Verwaest Committed by Commit Bot

[runtime] Use IsDecimalDigit in StringHasher rather than manual check

Bug: 
Change-Id: Ia347ed26ae93730a6bc58bcd6f5edb19b8ded5a9
Reviewed-on: https://chromium-review.googlesource.com/533413
Commit-Queue: Toon Verwaest <verwaest@chromium.org>
Reviewed-by: 's avatarCamillo Bruni <cbruni@chromium.org>
Cr-Commit-Position: refs/heads/master@{#45916}
parent 7eaf3a01
...@@ -32,7 +32,7 @@ ...@@ -32,7 +32,7 @@
#include "src/objects-inl.h" #include "src/objects-inl.h"
#include "src/objects.h" #include "src/objects.h"
#include "src/string-hasher.h" #include "src/string-hasher.h"
#include "src/utils.h" #include "src/utils-inl.h"
namespace v8 { namespace v8 {
namespace internal { namespace internal {
......
...@@ -70,7 +70,7 @@ ...@@ -70,7 +70,7 @@
#include "src/string-builder.h" #include "src/string-builder.h"
#include "src/string-search.h" #include "src/string-search.h"
#include "src/string-stream.h" #include "src/string-stream.h"
#include "src/utils.h" #include "src/utils-inl.h"
#include "src/wasm/wasm-module.h" #include "src/wasm/wasm-module.h"
#include "src/wasm/wasm-objects.h" #include "src/wasm/wasm-objects.h"
#include "src/zone/zone.h" #include "src/zone/zone.h"
......
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
#ifndef V8_STRING_HASHER_INL_H_ #ifndef V8_STRING_HASHER_INL_H_
#define V8_STRING_HASHER_INL_H_ #define V8_STRING_HASHER_INL_H_
#include "src/char-predicates-inl.h"
#include "src/objects.h" #include "src/objects.h"
#include "src/string-hasher.h" #include "src/string-hasher.h"
...@@ -71,14 +72,14 @@ void StringHasher::AddCharacter(uint16_t c) { ...@@ -71,14 +72,14 @@ void StringHasher::AddCharacter(uint16_t c) {
bool StringHasher::UpdateIndex(uint16_t c) { bool StringHasher::UpdateIndex(uint16_t c) {
DCHECK(is_array_index_); DCHECK(is_array_index_);
if (c < '0' || c > '9') { if (!IsDecimalDigit(c)) {
is_array_index_ = false; is_array_index_ = false;
return false; return false;
} }
int d = c - '0'; int d = c - '0';
if (is_first_char_) { if (is_first_char_) {
is_first_char_ = false; is_first_char_ = false;
if (c == '0' && length_ > 1) { if (d == 0 && length_ > 1) {
is_array_index_ = false; is_array_index_ = false;
return false; return false;
} }
......
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
#include "include/v8-platform.h" #include "include/v8-platform.h"
#include "src/base/platform/time.h" #include "src/base/platform/time.h"
#include "src/char-predicates-inl.h"
#include "src/v8.h" #include "src/v8.h"
namespace v8 { namespace v8 {
...@@ -31,6 +32,34 @@ class TimedScope { ...@@ -31,6 +32,34 @@ class TimedScope {
double* result_; double* result_;
}; };
template <typename Stream>
bool StringToArrayIndex(Stream* stream, uint32_t* index) {
uint16_t ch = stream->GetNext();
// If the string begins with a '0' character, it must only consist
// of it to be a legal array index.
if (ch == '0') {
*index = 0;
return !stream->HasMore();
}
// Convert string to uint32 array index; character by character.
if (!IsDecimalDigit(ch)) return false;
int d = ch - '0';
uint32_t result = d;
while (stream->HasMore()) {
ch = stream->GetNext();
if (!IsDecimalDigit(ch)) return false;
d = ch - '0';
// Check that the new result is below the 32 bit limit.
if (result > 429496729U - ((d + 3) >> 3)) return false;
result = (result * 10) + d;
}
*index = result;
return true;
}
} // namespace internal } // namespace internal
} // namespace v8 } // namespace v8
......
...@@ -1511,32 +1511,7 @@ class StringBuilder : public SimpleStringBuilder { ...@@ -1511,32 +1511,7 @@ class StringBuilder : public SimpleStringBuilder {
bool DoubleToBoolean(double d); bool DoubleToBoolean(double d);
template <typename Stream> template <typename Stream>
bool StringToArrayIndex(Stream* stream, uint32_t* index) { bool StringToArrayIndex(Stream* stream, uint32_t* index);
uint16_t ch = stream->GetNext();
// If the string begins with a '0' character, it must only consist
// of it to be a legal array index.
if (ch == '0') {
*index = 0;
return !stream->HasMore();
}
// Convert string to uint32 array index; character by character.
int d = ch - '0';
if (d < 0 || d > 9) return false;
uint32_t result = d;
while (stream->HasMore()) {
d = stream->GetNext() - '0';
if (d < 0 || d > 9) return false;
// Check that the new result is below the 32 bit limit.
if (result > 429496729U - ((d + 3) >> 3)) return false;
result = (result * 10) + d;
}
*index = result;
return true;
}
// Returns current value of top of the stack. Works correctly with ASAN. // Returns current value of top of the stack. Works correctly with ASAN.
DISABLE_ASAN DISABLE_ASAN
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment