Bug 41614 - RegExp constructor should throw a SyntaxError if invalid flags are specified
Summary: RegExp constructor should throw a SyntaxError if invalid flags are specified
Status: RESOLVED WORKSFORME
Alias: None
Product: WebKit
Classification: Unclassified
Component: JavaScriptCore (show other bugs)
Version: 528+ (Nightly build)
Hardware: PC All
: P3 Normal
Assignee: Nobody
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2010-07-05 07:20 PDT by Kent Hansen
Modified: 2011-06-06 12:22 PDT (History)
6 users (show)

See Also:


Attachments
Patch (20.65 KB, patch)
2010-07-21 08:50 PDT, Kent Hansen
oliver: review-
Details | Formatted Diff | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Kent Hansen 2010-07-05 07:20:07 PDT
ECMA-262 5th edition, section 15.10.4.1: "If [the flags string] contains any character other than "g", "i", or "m", or if it contains the same character more than once,
then throw a SyntaxError exception."

This is currently not the case in the JSC implementation; invalid characters are ignored, as are duplicate characters.
Comment 1 Kent Hansen 2010-07-19 02:21:22 PDT
Covered by LayoutTests/fast/js/sputnik/Conformance/15_Native_Objects/15.10_RegExp/15.10.4/S15.10.4.1_A5_T1.html et al.
Comment 2 Kent Hansen 2010-07-21 08:50:30 PDT
Created attachment 62187 [details]
Patch

I could split the refactoring to move flags parsing out of the RegExp ctor into a separate commit, if that's preferred.

I made the error message generic to keep things simple; arguably "Invalid flag 'x'" is better, but it'd require dynamic (de)allocation of the error message, which RegExp::m_constructionError is not made for.

Actually, the whole process of flag validation/error reporting could be decoupled completely from the RegExp class, i.e. it wouldn't be possible to create a RegExp with invalid flags. But that would require modifying all the callsites of regExpCache->lookupOrCreate() to call some other create-regexp-but-maybe-not function and change the error handling according. Not sure if that would really be an improvement.
Comment 3 Eric Seidel (no email) 2010-08-06 13:37:25 PDT
Olliej would either be able to review this or would know the right person to cc.
Comment 4 Oliver Hunt 2010-08-06 15:05:39 PDT
Comment on attachment 62187 [details]
Patch

provisional r- -- if you can convince me that the "error" constructor is necessary i'll r+ this, but currently it seems really hacky.

JavaScriptCore/runtime/RegExpCache.cpp:40
 +          return RegExp::createWithInvalidFlags();
Why return a new "magic" regexp rather than null?
Comment 5 Zoltan Herczeg 2010-08-07 00:20:54 PDT
EcmaScript-262: section 15.10.4.1 new RegExp(pattern, flags)

[...] and let F be the empty String if flags is undefined and ToString(flags) otherwise. [...] If F contains any character other than "g", "i", or "m", or if it contains the same character more than once, then throw a SyntaxError exception.

I am agree some cleanup could be useful on the code.
Comment 6 Kent Hansen 2010-08-09 01:09:19 PDT
(In reply to comment #4)
> (From update of attachment 62187 [details])
> provisional r- -- if you can convince me that the "error" constructor is necessary i'll r+ this, but currently it seems really hacky.
> 
> JavaScriptCore/runtime/RegExpCache.cpp:40
>  +          return RegExp::createWithInvalidFlags();
> Why return a new "magic" regexp rather than null?

So that existing code that calls lookupOrCreate() can remain the same. No need to check for a null-RegExp (without information about what went wrong); instead isValid() and errorMessage() is used, just as with other types of RegExp construction errors. This is what I was trying to highlight in the last paragraph of comment #2.
Comment 7 Kent Hansen 2010-08-09 01:12:14 PDT
(In reply to comment #5)
> EcmaScript-262: section 15.10.4.1 new RegExp(pattern, flags)
> 
> [...] and let F be the empty String if flags is undefined and ToString(flags) otherwise. [...] If F contains any character other than "g", "i", or "m", or if it contains the same character more than once, then throw a SyntaxError exception.

This is already stated in the bug description; I think Oliver was objecting to the createWithInvalidFlags() "helper" function.

> 
> I am agree some cleanup could be useful on the code.

Could you be more specific?
Comment 8 Zoltan Herczeg 2010-08-09 01:56:50 PDT
> Could you be more specific?

As I am not a reviewer, it means not much, but:

1) I think a pure global regExpFlagsFromString is unnecessary, we could at least move to this function as a static member function of RegExp (or a static inline function in the .cpp file).

2) As Oliver mentioned, I don't see the reason why two constructor is needed. An "if" in the first constructor would probably be enough.
Comment 9 Gavin Barraclough 2011-06-06 12:22:13 PDT
This is fixed by https://bugs.webkit.org/show_bug.cgi?id=56041