Closed Bug 220367 Opened 21 years ago Closed 21 years ago

NPE when accessing RegExp.$1 after matching /(a)|(b)/ against "b"

Categories

(Rhino Graveyard :: Core, defect)

defect
Not set
normal

Tracking

(Not tracked)

RESOLVED FIXED

People

(Reporter: igor, Assigned: igor)

Details

Attachments

(2 files)

The following script gives NullPointerException:

var regEx = /(a)|(b)/;
var t = regEx.test( 'b');
print( "RegExp.$1: '"+RegExp.$1+"'");

The script should print instead:

RegExp.$1: ''

In general NPE happens whenever after successful match one accesses a captured
group that was not captured as one of the branches of |.
If a branch of | with capturing () does not match , then RegExpImpl.parens will
contain null for corresponding $<group-number> according to code in
NativeRegExp.executeRegExp(). The current code does not check for a possible
null in RegExpImpl.parens when calling toString and the patch fixes that by
making sure that getParenSubString return SubString.emptySubString in such
cases.
Testcases added to JS testsuite:

  mozilla/js/tests/ecma_3/RegExp/regress-220367-001.js
  mozilla/js/tests/ecma_3/RegExp/regress-220367-002.js

The first one checks only the match array |str.match(regEx)|;
the second one checks the global properties |RegExp.$1|, |RegExp.$2|.
As Igor reported, the second one crashes in the current Rhino shell.
I committed the fix
Status: NEW → RESOLVED
Closed: 21 years ago
Resolution: --- → FIXED
With the fix, no longer crash on js/tests/ecma_3/RegExp/regress-220367-002.js.
However, we're getting a discrepancy in Section 3 of the test:


         Testcase ecma_3/RegExp/regress-220367-002.js failed:
         Failure messages were: 
         Section 3 of test -
         Expected value '', Actual value 'a'


For convenience, here is the body of the test:

var re = /(a)|(b)/;

re.test('a');
  status = inSection(1);
  actual = RegExp.$1;
  expect = 'a';
  addThis();

  status = inSection(2);
  actual = RegExp.$2;
  expect = '';
  addThis();

re.test('b');
  status = inSection(3);
  actual = RegExp.$1;
  expect = '';
  addThis();

  status = inSection(4);
  actual = RegExp.$2;
  expect = 'b';
  addThis();



For some reason, Rhino is not clearing out the value of RegExp.$1
It continues to hold the value 'a' from the previous re.test(). 

This differs from both SpiderMonkey and IE6, as can be seen via
the following javascript: URL

javascript: var msg = ''; var re = /(a)|(b)/; re.test('a'); msg += '$1= ' +
RegExp.$1 + '\n' +  '$2= ' +  RegExp.$2 + '\n\n\n'; re.test('b'); msg +=  '$1= '
+ RegExp.$1 + '\n' +  '$2= ' + RegExp.$2; alert(msg);
Still getting the test failure mentioned in Comment #4 -
reopening after wrong click on fixed button
Status: RESOLVED → REOPENED
Resolution: FIXED → ---
In the patch I replaced parens array in RegExpImpl from ObjArray to SubString[]
since reuse of parens that ObjArray provides does not justify complexity of
code to clean up it. The problem with old version was missed reset of initial
elements before calling ObjArray.setSize which was responsible for keeping the
old elements.
Fixing for real
Status: REOPENED → RESOLVED
Closed: 21 years ago21 years ago
Resolution: --- → FIXED
Trageting as resolved against 1.5R5
Target Milestone: --- → 1.5R5
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Creator:
Created:
Updated:
Size: