ChakraCore icon indicating copy to clipboard operation
ChakraCore copied to clipboard

Wrong begin lookup segment in JavascriptArray::DirectSetItem_Full

Open guhe120 opened this issue 8 years ago • 2 comments

In the function JavascriptArray::DirectSetItem_Full (JavaScriptArray.inl):

//Find the segment where itemIndex is present or is at the boundary SparseArraySegment* current = (SparseArraySegment*)this->GetBeginLookupSegment(itemIndex, false); // If it doesn't fit in current chunk (watch for overflow), start from beginning as we'll // need the prev if (current->left + current->size > current->left || itemIndex >= current->left + current->size) { current = SparseArraySegment::From(head); }

Here it tries to use GetBeginLookupSegment to speed up the search. However, in the following statement:

if (current->left + current->size > current->left || itemIndex >= current->left + current->size) { current = SparseArraySegment::From(head); }

Here current->left + current->size > current->left is always true for a normal segment, so it will always execute: current = SparseArraySegment::From(head); Which makes the GetBeginLookupSegment useless.

I think this is a typo here, it should be:

if (current->left + current->size < current->left || itemIndex >= current->left + current->size)

guhe120 avatar Feb 08 '18 10:02 guhe120

Great catch, thanks for reporting! I see two instances of that same logic in DirectSetItem_Full.

sethbrenith avatar Feb 08 '18 17:02 sethbrenith

@guhe120 nice find. Although when I look more this current->left + current->size > current->left should be current->left > itemIndex , no? This current->left + current->size < current->left would always returns false. As we must have established that left+size will not overlfow when we have created segment.

akroshg avatar Feb 08 '18 18:02 akroshg