commit 3055196eb5f1 (HEAD) Author: Luke Wagner Date: Sat Dec 2 12:33:29 2017 -0500 Bug 1415291 - Baldr: factor out ToTableIndex(). r=bbouvier, a=gchang --- js/src/wasm/WasmJS.cpp | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git js/src/wasm/WasmJS.cpp js/src/wasm/WasmJS.cpp index 1d07b0d065fe..482de9788221 100644 --- js/src/wasm/WasmJS.cpp +++ js/src/wasm/WasmJS.cpp @@ -1694,6 +1694,20 @@ const JSPropertySpec WasmTableObject::properties[] = JS_PS_END }; +static bool +ToTableIndex(JSContext* cx, HandleValue v, const Table& table, const char* noun, uint32_t* index) +{ + if (!ToNonWrappingUint32(cx, v, UINT32_MAX, "Table", noun, index)) + return false; + + if (*index >= table.length()) { + JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, JSMSG_WASM_BAD_UINT32, "Table", noun); + return false; + } + + return true; +} + /* static */ bool WasmTableObject::getImpl(JSContext* cx, const CallArgs& args) { @@ -1701,7 +1715,7 @@ WasmTableObject::getImpl(JSContext* cx, const CallArgs& args) const Table& table = tableObj->table(); uint32_t index; - if (!ToNonWrappingUint32(cx, args.get(0), table.length() - 1, "Table", "get index", &index)) + if (!ToTableIndex(cx, args.get(0), table, "get index", &index)) return false; ExternalTableElem& elem = table.externalArray()[index]; @@ -1740,7 +1754,7 @@ WasmTableObject::setImpl(JSContext* cx, const CallArgs& args) return false; uint32_t index; - if (!ToNonWrappingUint32(cx, args.get(0), table.length() - 1, "Table", "set index", &index)) + if (!ToTableIndex(cx, args.get(0), table, "set index", &index)) return false; RootedFunction value(cx);