Closed Bug 1304737 Opened 9 years ago Closed 9 years ago

RegExp.test with global or sticky flags does not correctly set RegExp.lastIndex

Categories

(Core :: JavaScript Engine, defect)

46 Branch
defect
Not set
normal

Tracking

()

VERIFIED FIXED
mozilla52
Tracking Status
firefox49 --- wontfix
firefox50 - verified
firefox51 + verified
firefox52 + verified

People

(Reporter: pdaehne, Assigned: arai)

References

Details

(Keywords: regression)

Attachments

(1 file)

User Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:49.0) Gecko/20100101 Firefox/49.0 Build ID: 20160916101415 Steps to reproduce: Open the Web Console and enter the following line: var r = /f.*/g; r.test('foo'); r.lastIndex; Actual results: I get '1' as result. Expected results: I expect to get '3' as result.
Blocks: 1207922
Component: Untriaged → JavaScript Engine: JIT
Flags: needinfo?(arai.unmht)
Keywords: regression
Product: Firefox → Core
Version: 49 Branch → 46 Branch
thank you for pointing it out! optimization for the trailing ".*" goes wrong.
Assignee: nobody → arai.unmht
Status: UNCONFIRMED → ASSIGNED
Ever confirmed: true
https://dxr.mozilla.org/mozilla-central/rev/560b2c805bf7bebeb3ceebc495a81b2aa4c0c755/js/src/irregexp/RegExpParser.cpp#1851 > // Try to strip a trailing '.*' from the RegExp, which as above will > // affect the captures but not whether there is a match. Only do this > // when there are no other meta characters in the RegExp, so that we > // are sure this will not affect how the RegExp is parsed. > if (length >= 3 && !HasRegExpMetaChars(chars, length - 2) && > chars[length - 2] == '.' && chars[length - 1] == '*') > { > length -= 2; > } This affects lastIndex. also, we cannot use input string's length for lastIndex, as the pattern doesn't match whole string if the string contains newline. so, this optimization should need to be removed.
Flags: needinfo?(arai.unmht)
I was wrong. We have been used non-matchOnly execution for global/sticky test. Will try adding back equivalent.
Previously, we were using non-matchOnly execution for test with global/sticky, to update lastIndex correctly. https://dxr.mozilla.org/mozilla-esr45/rev/7dedca747e536c65ddbd2f487d0112c88159a6a3/js/src/builtin/RegExp.cpp#808 > Maybe<ScopedMatchPairs> alternateMatches; > if (!reobj->needUpdateLastIndex()) { > searchIndex = 0; > } else if (!matches) { > alternateMatches.emplace(&cx->tempLifoAlloc()); > matches = &alternateMatches.ref(); > } > ... > } else if (reobj->needUpdateLastIndex()) { > /* Steps 18.a-b. */ > MOZ_ASSERT(matches && !matches->empty()); > if (!SetLastIndex(cx, reobj, (*matches)[0].limit)) > return RegExpRunStatus_Error; > } now, we're returning lastIndex value also on matchOnly execution. also, bug 1263340 patches changed RegExpBuiltinExec to use internal global and sticky flags, so global and sticky values are known at compile time. we just need to avoid stripping trailing ".*" for global/sticky case, to make lastIndex value correct. this change won't affect most case that tests with non-global and non-sticky RegExp.
Attachment #8793914 - Flags: review?(hv1989)
Component: JavaScript Engine: JIT → JavaScript Engine
Summary: RegExp.test does not correctly set RegExp.lastIndex → RegExp.test with global or sticky flags does not correctly set RegExp.lastIndex
Comment on attachment 8793914 [details] [diff] [review] Do not ignore trailing .* on matchOnly RegExp execution. Review of attachment 8793914 [details] [diff] [review]: ----------------------------------------------------------------- Looks good.
Attachment #8793914 - Flags: review?(hv1989) → review+
Tracking 52+ for this JS regression.
https://hg.mozilla.org/integration/mozilla-inbound/rev/f3800637757590adbf18a09c18b9351a2d04fa76 Bug 1304737 - Do not ignore trailing .* on matchOnly RegExp execution. r=h4writer
Track for 51+ as this is JS regression.
Comment on attachment 8793914 [details] [diff] [review] Do not ignore trailing .* on matchOnly RegExp execution. looks like m-c merge is taking time. requesting approval in advance. same patch is applicable to mozilla-aurora. Approval Request Comment > [Feature/regressing bug #] bug 1207922 > [User impact if declined] Wrong JavaScript behavior. > [Describe test coverage new/current, TreeHerder] tested in m-i > [Risks and why] low. this patch disables unexpected optimization for specific case. it does fallback to normal path. > [String/UUID change made/needed] None
Attachment #8793914 - Flags: approval-mozilla-aurora?
Comment on attachment 8793914 [details] [diff] [review] Do not ignore trailing .* on matchOnly RegExp execution. same patch is applicable to mozilla-beta. Approval Request Comment > [Feature/regressing bug #] bug 1207922 > [User impact if declined] Wrong JavaScript behavior. > [Describe test coverage new/current, TreeHerder] tested in m-i > [Risks and why] low. this patch disables unexpected optimization for specific case. it does fallback to normal path. > [String/UUID change made/needed] None
Attachment #8793914 - Flags: approval-mozilla-beta?
Status: ASSIGNED → RESOLVED
Closed: 9 years ago
Resolution: --- → FIXED
Target Milestone: --- → mozilla52
Hi Patrick, could you please verify this issue is fixed as expected on a latest Nightly build? Thanks!
Flags: needinfo?(pdaehne)
Comment on attachment 8793914 [details] [diff] [review] Do not ignore trailing .* on matchOnly RegExp execution. Fixes a JS regression, Aurora51+, Beta50+
Attachment #8793914 - Flags: approval-mozilla-beta?
Attachment #8793914 - Flags: approval-mozilla-beta+
Attachment #8793914 - Flags: approval-mozilla-aurora?
Attachment #8793914 - Flags: approval-mozilla-aurora+
Don't feel the need to track it now that it's fixed.
It's working now as expected in the Nightly build. Thanks for fixing!
Flags: needinfo?(pdaehne)
QA Whiteboard: [good first verify]
I have reproduced this Bug on Nightly 52.0a1 (2016-09-22) on Windows 10, 64 Bit! The bug's fix is now verified on latest Nightly 52.0a1 Nightly 52.0a1: Build ID : 20160929030426 User Agent : Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:52.0) Gecko/20100101 Firefox/52.0 [testday-20160930]
Fixed on Firefox 50.0b3 using Linux Mint 18
It's working as expected on Firefox 50.0b3 on Windows 10 Pro
arai, is this something that might help with the slow script warning issues?
Flags: needinfo?(arai.unmht)
It's great to see so many verification points. Thanks everyone!
Status: RESOLVED → VERIFIED
(In reply to Liz Henry (:lizzard) (needinfo? me) from comment #22) > arai, is this something that might help with the slow script warning issues? No, not related.
Flags: needinfo?(arai.unmht)
The fix is verified on Beta 51.0b3 Build ID :20161124073320 User Agenet :Mozilla/5.0 (Windows NT 10.0; rv:51.0) Gecko/20100101 Firefox/51.0
It is verified on Firefox Beta 51.0b3 [testday-20161125]
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: