Skip to content

Commit

Permalink
Merge pull request #70 from openkraken/fix/insert_before_crash
Browse files Browse the repository at this point in the history
fix: fix insertBefore crash when passing none node object.
  • Loading branch information
yuanyan authored Mar 19, 2021
2 parents a3ffee7 + 6e6d79c commit 09ccc57
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 8 deletions.
10 changes: 5 additions & 5 deletions bridge/bindings/jsc/DOM/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ JSValueRef JSNode::appendChild(JSContextRef ctx, JSObjectRef function, JSObjectR
JSObjectRef nodeObjectRef = JSValueToObject(ctx, nodeValueRef, exception);
auto nodeInstance = static_cast<NodeInstance *>(JSObjectGetPrivate(nodeObjectRef));

if (nodeInstance == nullptr || nodeInstance->_identify != NODE_IDENTIFY) {
if (nodeInstance == nullptr || nodeInstance->document != selfInstance->document) {
throwJSError(ctx, "Failed to execute 'appendChild' on 'Node': first arguments should be an Node type.", exception);
return nullptr;
}
Expand Down Expand Up @@ -288,7 +288,7 @@ JSValueRef JSNode::insertBefore(JSContextRef ctx, JSObjectRef function, JSObject
auto selfInstance = static_cast<NodeInstance *>(JSObjectGetPrivate(thisObject));
auto nodeInstance = static_cast<NodeInstance *>(JSObjectGetPrivate(nodeObjectRef));

if (nodeInstance == nullptr || nodeInstance->_identify != NODE_IDENTIFY) {
if (nodeInstance == nullptr || nodeInstance->document != selfInstance->document) {
throwJSError(ctx, "Failed to execute 'insertBefore' on 'Node': parameter 1 is not of type 'Node'", exception);
return nullptr;
}
Expand Down Expand Up @@ -331,14 +331,14 @@ JSValueRef JSNode::replaceChild(JSContextRef ctx, JSObjectRef function, JSObject
auto oldChildInstance = static_cast<NodeInstance *>(JSObjectGetPrivate(oldChildObjectRef));

if (oldChildInstance == nullptr || oldChildInstance->parentNode != selfInstance ||
oldChildInstance->_identify != NODE_IDENTIFY) {
oldChildInstance->document != selfInstance->document) {
throwJSError(ctx,
"Failed to execute 'replaceChild' on 'Node': The node to be replaced is not a child of this node.",
exception);
return nullptr;
}

if (newChildInstance == nullptr || newChildInstance->_identify != NODE_IDENTIFY) {
if (newChildInstance == nullptr || newChildInstance->document != selfInstance->document) {
throwJSError(ctx, "Failed to execute 'replaceChild' on 'Node': The new node is not a type of node.", exception);
return nullptr;
}
Expand Down Expand Up @@ -424,7 +424,7 @@ JSValueRef JSNode::removeChild(JSContextRef ctx, JSObjectRef function, JSObjectR
auto selfInstance = static_cast<NodeInstance *>(JSObjectGetPrivate(thisObject));
auto nodeInstance = static_cast<NodeInstance *>(JSObjectGetPrivate(nodeObjectRef));

if (nodeInstance == nullptr || nodeInstance->_identify != NODE_IDENTIFY) {
if (nodeInstance == nullptr || nodeInstance->document != selfInstance->document) {
throwJSError(ctx, "Failed to execute 'removeChild' on 'Node': 1st arguments is not a Node object.", exception);
return nullptr;
}
Expand Down
3 changes: 0 additions & 3 deletions bridge/include/kraken_bridge_jsc.h
Original file line number Diff line number Diff line change
Expand Up @@ -487,8 +487,6 @@ enum NodeType {
DOCUMENT_FRAGMENT_NODE = 11
};

#define NODE_IDENTIFY 1

class JSNode : public JSEventTarget {
public:
static std::unordered_map<JSContext *, JSNode *> instanceMap;
Expand Down Expand Up @@ -575,7 +573,6 @@ class NodeInstance : public EventTargetInstance {
void unrefer();

int32_t _referenceCount{0};
int32_t _identify{NODE_IDENTIFY};

DocumentInstance *document{nullptr};
virtual void _notifyNodeRemoved(NodeInstance *node);
Expand Down

0 comments on commit 09ccc57

Please sign in to comment.