| 1 |
/* -*- tab-width: 2; indent-tabs-mode: nil; js-indent-level: 2 -*- */ |
| 2 |
/* This Source Code Form is subject to the terms of the Mozilla Public |
| 3 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 4 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 5 |
|
| 6 |
/* |
| 7 |
* Date: 26 November 2000 |
| 8 |
* |
| 9 |
* |
| 10 |
* SUMMARY: This test arose from Bugzilla bug 57631: |
| 11 |
* "RegExp with invalid pattern or invalid flag causes segfault" |
| 12 |
* |
| 13 |
* Either error should throw an exception of type SyntaxError, |
| 14 |
* and we check to see that it does... |
| 15 |
*/ |
| 16 |
//----------------------------------------------------------------------------- |
| 17 |
var BUGNUMBER = '57631'; |
| 18 |
var summary = 'Testing new RegExp(pattern,flag) with illegal pattern or flag'; |
| 19 |
var statprefix = 'Testing for error creating illegal RegExp object on pattern '; |
| 20 |
var statsuffix = 'and flag '; |
| 21 |
var cnSUCCESS = 'SyntaxError'; |
| 22 |
var cnFAILURE = 'not a SyntaxError'; |
| 23 |
var singlequote = "'"; |
| 24 |
var i = -1; var j = -1; var s = ''; var f = ''; |
| 25 |
var obj = {}; |
| 26 |
var status = ''; var actual = ''; var expect = ''; var msg = ''; |
| 27 |
var legalpatterns = new Array(); var illegalpatterns = new Array(); |
| 28 |
var legalflags = new Array(); var illegalflags = new Array(); |
| 29 |
|
| 30 |
|
| 31 |
// valid regular expressions to try - |
| 32 |
legalpatterns[0] = ''; |
| 33 |
legalpatterns[1] = 'abc'; |
| 34 |
legalpatterns[2] = '(.*)(3-1)\s\w'; |
| 35 |
legalpatterns[3] = '(.*)(...)\\s\\w'; |
| 36 |
legalpatterns[4] = '[^A-Za-z0-9_]'; |
| 37 |
legalpatterns[5] = '[^\f\n\r\t\v](123.5)([4 - 8]$)'; |
| 38 |
|
| 39 |
// invalid regular expressions to try - |
| 40 |
illegalpatterns[0] = '(?)'; |
| 41 |
illegalpatterns[1] = '(a'; |
| 42 |
illegalpatterns[2] = '( ]'; |
| 43 |
//illegalpatterns[3] = '\d{1,s}'; |
| 44 |
|
| 45 |
// valid flags to try - |
| 46 |
legalflags[0] = 'i'; |
| 47 |
legalflags[1] = 'g'; |
| 48 |
legalflags[2] = 'm'; |
| 49 |
legalflags[3] = undefined; |
| 50 |
|
| 51 |
// invalid flags to try - |
| 52 |
illegalflags[0] = 'a'; |
| 53 |
illegalflags[1] = 123; |
| 54 |
illegalflags[2] = new RegExp(); |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
//------------------------------------------------------------------------------------------------- |
| 59 |
test(); |
| 60 |
//------------------------------------------------------------------------------------------------- |
| 61 |
|
| 62 |
|
| 63 |
function test() |
| 64 |
{ |
| 65 |
printBugNumber(BUGNUMBER); |
| 66 |
printStatus (summary); |
| 67 |
|
| 68 |
testIllegalRegExps(legalpatterns, illegalflags); |
| 69 |
testIllegalRegExps(illegalpatterns, legalflags); |
| 70 |
testIllegalRegExps(illegalpatterns, illegalflags); |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
// This function will only be called where all the patterns are illegal, or all the flags |
| 75 |
function testIllegalRegExps(patterns, flags) |
| 76 |
{ |
| 77 |
for (i in patterns) |
| 78 |
{ |
| 79 |
s = patterns[i]; |
| 80 |
|
| 81 |
for (j in flags) |
| 82 |
{ |
| 83 |
f = flags[j]; |
| 84 |
status = getStatus(s, f); |
| 85 |
actual = cnFAILURE; |
| 86 |
expect = cnSUCCESS; |
| 87 |
|
| 88 |
try |
| 89 |
{ |
| 90 |
// This should cause an exception if either s or f is illegal - |
| 91 |
eval('obj = new RegExp(s, f);'); |
| 92 |
} |
| 93 |
catch(e) |
| 94 |
{ |
| 95 |
// We expect to get a SyntaxError - test for this: |
| 96 |
if (e instanceof SyntaxError) |
| 97 |
actual = cnSUCCESS; |
| 98 |
} |
| 99 |
|
| 100 |
reportCompare(expect, actual, status); |
| 101 |
} |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
function getStatus(regexp, flag) |
| 107 |
{ |
| 108 |
return (statprefix + quote(regexp) + statsuffix + quote(flag)); |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
function quote(text) |
| 113 |
{ |
| 114 |
return (singlequote + text + singlequote); |
| 115 |
} |