No issue - Refactor FindErrorInstanceOrPrototype

The logic here wasn't very transparent or easy to follow.
Doing a positive check to set the result instead of defaulting to it
also potentially prevents issues.
This commit is contained in:
Moonchild 2023-02-21 20:29:35 +01:00 committed by roytam1
commit db89c5a05d

View file

@ -557,34 +557,30 @@ FindErrorInstanceOrPrototype(JSContext* cx, HandleObject obj, MutableHandleObjec
// (new NYI).stack
// to continue returning stacks that are useless, but at least don't throw.
RootedObject target(cx, CheckedUnwrap(obj));
if (!target) {
JS_ReportErrorASCII(cx, "Permission denied to access object");
return false;
}
RootedObject proto(cx);
while (!IsErrorProtoKey(StandardProtoKeyOrNull(target))) {
if (!GetPrototype(cx, target, &proto))
return false;
if (!proto) {
// We walked the whole prototype chain and did not find an Error
// object.
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, JSMSG_INCOMPATIBLE_PROTO,
js_Error_str, "(get stack)", obj->getClass()->name);
return false;
}
target = CheckedUnwrap(proto);
RootedObject curr(cx, obj);
RootedObject target(cx);
do {
target = CheckedUnwrap(curr);
if (!target) {
JS_ReportErrorASCII(cx, "Permission denied to access object");
return false;
}
}
if (IsErrorProtoKey(StandardProtoKeyOrNull(target))) {
result.set(target);
return true;
}
result.set(target);
return true;
if (!GetPrototype(cx, target, &curr)) {
return false;
}
} while (curr);
// We walked the whole prototype chain and did not find an Error
// object.
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, JSMSG_INCOMPATIBLE_PROTO,
js_Error_str, "(get stack)", obj->getClass()->name);
return false;
}