Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.
Sign upOverridden RegExp "global" property potentially causes infinite loop #528
Comments
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
littledan
Apr 8, 2016
Member
I think it's fair to say that it's possible to cause bad situations by overriding the flags getters. But Is this so surprising and bad, that when a protocol is violated, that weird results happen? I bet you could also mess up the Array methods pretty easily by returning a Proxy from an Array subclass constructor.
Because RegExp methods like RegExp.prototype[Symbol.replace] are not specialized to objects with a [[RegExpMatcher]] internal slot (a possibility that I proposed at the November 2015 TC39 meeting, but which was rejected), they cannot refer to the [[OriginalFlags]] value.
|
I think it's fair to say that it's possible to cause bad situations by overriding the flags getters. But Is this so surprising and bad, that when a protocol is violated, that weird results happen? I bet you could also mess up the Array methods pretty easily by returning a Proxy from an Array subclass constructor. Because RegExp methods like |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
ljharb
Apr 8, 2016
Member
What's the use case where someone would do that via a defineProperty, when they can new RegExp(re, re.flags + 'g') if they want the same regex with the global flag set?
|
What's the use case where someone would do that via a |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
bterlson
Apr 12, 2016
Member
I find this a bit gross but I doubt this is an issue that someone would come across in real code. Basically the guidance is: if you want to modify a regexp instance or implement a subclass, and you want the getters for global and sticky to differ from the internal flags, then you need to implement @@match/@@replace to handle this.
|
I find this a bit gross but I doubt this is an issue that someone would come across in real code. Basically the guidance is: if you want to modify a regexp instance or implement a subclass, and you want the getters for global and sticky to differ from the internal flags, then you need to implement @@match/@@replace to handle this. |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
goyakin
Apr 15, 2016
Member
@ljharb, I don't personally see why one would want to do this, but the language shouldn't allow such a thing if possible.
@littledan, I was thinking of using [[OriginalFlags]] if it exists and fall back to the properties if not, but this doesn't work well with subclasses. It now also looks like the use of this internal slot was rejected by the committee.
@bterlson,
I agree with the reasoning behind #494. Given that there is no good way of fixing this, I'm closing the issue.
|
@ljharb, I don't personally see why one would want to do this, but the language shouldn't allow such a thing if possible. @littledan, I was thinking of using @bterlson, I agree with the reasoning behind #494. Given that there is no good way of fixing this, I'm closing the issue. |
goyakin commentedApr 8, 2016
#494 updated
RegExpBuiltinExecto inferglobalfrom[[OriginalFlags]]instead of performing aGetfor the property. If we have a RegExp instance created without theglobalflag, but we overridde theglobalproperty to returntrue, this potentially creates an infinite loop sinceRegExpBuiltinExecnow resetslastIndexto0. For example:This applies to
RegExp.prototype[Symbol.match]andRegExp.prototype[Symbol.replace].From the caller's perspective, I don't think this should happen since the property returns a consistent value.