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

Flags undefined in tokenize #986

Closed
isglass opened this issue Jun 30, 2016 · 6 comments
Closed

Flags undefined in tokenize #986

isglass opened this issue Jun 30, 2016 · 6 comments

Comments

@isglass
Copy link

isglass commented Jun 30, 2016

Hi,

Can't reproduce it properly, but sometimes when using FileHighlighter plugin we get flags undefined in tokenize. Which leads to RegExp exception, it tries undefinedg.

Since I can't reproduce when debugging i propose the following:

var flags = pattern.pattern.toString().match(/[imuy]*$/)[0];
pattern.pattern = RegExp(pattern.pattern.source, flags + "g");

Replaced with:


var flags = pattern.pattern.toString().match(/[imuy]*$/)[0];
pattern.pattern = RegExp(pattern.pattern.source, (flags || "") + "g");

Regards,
Johan

@zeitgeist87
Copy link
Collaborator

Hi @isglass,

Thanks for reporting this. I cannot see how flags could become undefined. The expression 'anystring'.match(/[imuy]*$/)[0]; returns at least an empty string for any input string.

'xxx'.match(/[imuy]*$/)[0];
''.match(/[imuy]*$/)[0];
RegExp('', '').toString().match(/[imuy]*$/)[0];

And if pattern.pattern.toString() would somehow return undefined, then you would get an error right there.

The following is the only way I can think of, that would cause this bug:

RegExp.prototype.toString = function() {
    return {
        match: function() { return [] }
    }
}

I think we should get to the root of this problem. Could you provide more details for this bug? What browser did you use? Maybe a simple online demo that reproduces it?

Thanks!

@zeitgeist87
Copy link
Collaborator

For flags to be undefined, match() has to return an empty array, which cannot happen under normal circumstances:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/match

An Array containing the entire match result and any parentheses-captured matched results, or null if there were no matches.

@isglass
Copy link
Author

isglass commented Jul 1, 2016

Hi @zeitgeist87 ,

Unfortunately I can't reproduce it, have only noticed it in our "visitor-exception-logs" over the last couple of days. All I have got is their user agent:

Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.80 Safari/537.36

And a stack trace:

new RegExp  @ (native):
RegExp  @ (native):
Object._self.Prism.tokenize @ prism.js:3
Object._self.Prism.highlight    @ prism.js:3
Object._self.Prism.highlightElement @ prism.js:3
XMLHttpRequest.i.onreadystatechange @ prism.js:8

And exception message:

Invalid flags supplied to RegExp constructor 'undefinedg'

@zeitgeist87
Copy link
Collaborator

The user-agent would suggest, that it is a 64bit Linux system running Chromium 47.0.2526.80 which is over a year old now. Do all these users have the same user-agent? Could it be some kind of crawler that uses a fake user-agent?

The only way for flags to become undefined is if you change either the prototype of RegExp or String. At least as far as I can see. I don't think we will add these changes if you cannot reproduce the bug, or show a plausible way how flags can be undefined.

Maybe you could add some debug code to your site that provides more information:

if (flags === undefined) {
    console.log(pattern, pattern.pattern, pattern.pattern.toString(), pattern.pattern.toString().match(/[imuy]*$/));
}

@LeaVerou
Copy link
Member

What other scripts are you using? Perhaps one of them is overwriting native methods, and making match() return an empty array?

@Golmote
Copy link
Contributor

Golmote commented Mar 29, 2017

Please reopen if you can provide more information regarding this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants