Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid reading outside of collection bounds #407

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions dist/sizzle.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Released under the MIT license
* https://js.foundation/
*
* Date: 2017-09-01
* Date: 2017-09-10
*/
(function( window ) {

Expand Down Expand Up @@ -656,7 +656,7 @@ setDocument = Sizzle.setDocument = function( node ) {
// getElementById is not reliable as a find shortcut
Expr.find["ID"] = function( id, context ) {
if ( typeof context.getElementById !== "undefined" && documentIsHTML ) {
var node, i, elems,
var node, i, elems, length,
elem = context.getElementById( id );

if ( elem ) {
Expand All @@ -669,8 +669,9 @@ setDocument = Sizzle.setDocument = function( node ) {

// Fall back on getElementsByName
elems = context.getElementsByName( id );
length = elems.length;
i = 0;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could moved to the body of a for right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, for both i and length. However, i was already outside of the for, so I decided to follow the existing style.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, that because of the https://contribute.jquery.org/style-guide/js/#good-examples, i.e. it should be fine to put i to the body, as long its initialised on top of the scope, like we doing here – https://github.com/jquery/jquery/blob/692f9d4db30c9c6c4f6bc76005cf153586202fa6/src/effects.js#L614 :)

while ( (elem = elems[i++]) ) {
for ( ; i !== length && (elem = elems[ i ]) != null; i++ ) {
node = elem.getAttributeNode("id");
if ( node && node.value === id ) {
return [ elem ];
Expand Down Expand Up @@ -700,11 +701,12 @@ setDocument = Sizzle.setDocument = function( node ) {
tmp = [],
i = 0,
// By happy coincidence, a (broken) gEBTN appears on DocumentFragment nodes too
results = context.getElementsByTagName( tag );
results = context.getElementsByTagName( tag ),
length = results.length;

// Filter out possible comments
if ( tag === "*" ) {
while ( (elem = results[i++]) ) {
for ( ; i !== length && (elem = results[ i ]) != null; i++ ) {
if ( elem.nodeType === 1 ) {
tmp.push( elem );
}
Expand Down Expand Up @@ -1049,17 +1051,19 @@ Sizzle.uniqueSort = function( results ) {
var elem,
duplicates = [],
j = 0,
i = 0;
i = 0,
length = results.length;

// Unless we *know* we can detect duplicates, assume their presence
hasDuplicate = !support.detectDuplicates;
sortInput = !support.sortStable && results.slice( 0 );
results.sort( sortOrder );

if ( hasDuplicate ) {
while ( (elem = results[i++]) ) {
if ( elem === results[ i ] ) {
j = duplicates.push( i );
for ( ; i < length; i++ ) {
elem = results[ i ];
if ( i + 1 < length && elem === results[ i + 1 ] ) {
j = duplicates.push( i + 1 );
}
}
while ( j-- ) {
Expand Down
2 changes: 1 addition & 1 deletion dist/sizzle.min.map

Large diffs are not rendered by default.

28 changes: 14 additions & 14 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 12 additions & 8 deletions src/sizzle.js
Original file line number Diff line number Diff line change
Expand Up @@ -656,7 +656,7 @@ setDocument = Sizzle.setDocument = function( node ) {
// getElementById is not reliable as a find shortcut
Expr.find["ID"] = function( id, context ) {
if ( typeof context.getElementById !== "undefined" && documentIsHTML ) {
var node, i, elems,
var node, i, elems, length,
elem = context.getElementById( id );

if ( elem ) {
Expand All @@ -669,8 +669,9 @@ setDocument = Sizzle.setDocument = function( node ) {

// Fall back on getElementsByName
elems = context.getElementsByName( id );
length = elems.length;
Copy link
Member

@gibson042 gibson042 Aug 30, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NodeList#length can be an element in IE<9 (and we should have a test where that is the case on this line). cf. f6e2fc5#diff-7d34356c0b7229dd5da8b6c7711b32b7R1733

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, something like this?

if (typeof elem.length === 'number') {
  fastPath();
} else {
  slowPathForLegacyBrowsers();
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, provided we can get it without excessive file size increase.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

example:

for ( ; i !== len && (elem = elems[i]) != null; i++ ) {

i = 0;
while ( (elem = elems[i++]) ) {
for ( ; i !== length && (elem = elems[ i ]) != null; i++ ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is probably not an issue, but I feel like i < length is safer.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i < length would definitely be an issue, because length might be a DOM node in IE<9.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

makes sense.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps we could put in a comment relating that as a reminder.

node = elem.getAttributeNode("id");
if ( node && node.value === id ) {
return [ elem ];
Expand Down Expand Up @@ -700,11 +701,12 @@ setDocument = Sizzle.setDocument = function( node ) {
tmp = [],
i = 0,
// By happy coincidence, a (broken) gEBTN appears on DocumentFragment nodes too
results = context.getElementsByTagName( tag );
results = context.getElementsByTagName( tag ),
length = results.length;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same problem here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.


// Filter out possible comments
if ( tag === "*" ) {
while ( (elem = results[i++]) ) {
for ( ; i !== length && (elem = results[ i ]) != null; i++ ) {
if ( elem.nodeType === 1 ) {
tmp.push( elem );
}
Expand Down Expand Up @@ -1049,17 +1051,19 @@ Sizzle.uniqueSort = function( results ) {
var elem,
duplicates = [],
j = 0,
i = 0;
i = 0,
length = results.length;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same problem here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.


// Unless we *know* we can detect duplicates, assume their presence
hasDuplicate = !support.detectDuplicates;
sortInput = !support.sortStable && results.slice( 0 );
results.sort( sortOrder );

if ( hasDuplicate ) {
while ( (elem = results[i++]) ) {
if ( elem === results[ i ] ) {
j = duplicates.push( i );
for ( ; i < length; i++ ) {
elem = results[ i ];
if ( i + 1 < length && elem === results[ i + 1 ] ) {
j = duplicates.push( i + 1 );
}
}
while ( j-- ) {
Expand Down