1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/js/src/tests/ecma_3/RegExp/regress-57631.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,118 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +/* 1.10 + * Date: 26 November 2000 1.11 + * 1.12 + * 1.13 + * SUMMARY: This test arose from Bugzilla bug 57631: 1.14 + * "RegExp with invalid pattern or invalid flag causes segfault" 1.15 + * 1.16 + * Either error should throw an exception of type SyntaxError, 1.17 + * and we check to see that it does... 1.18 + */ 1.19 +//----------------------------------------------------------------------------- 1.20 +var BUGNUMBER = '57631'; 1.21 +var summary = 'Testing new RegExp(pattern,flag) with illegal pattern or flag'; 1.22 +var statprefix = 'Testing for error creating illegal RegExp object on pattern '; 1.23 +var statsuffix = 'and flag '; 1.24 +var cnSUCCESS = 'SyntaxError'; 1.25 +var cnFAILURE = 'not a SyntaxError'; 1.26 +var singlequote = "'"; 1.27 +var i = -1; var j = -1; var s = ''; var f = ''; 1.28 +var obj = {}; 1.29 +var status = ''; var actual = ''; var expect = ''; var msg = ''; 1.30 +var legalpatterns = new Array(); var illegalpatterns = new Array(); 1.31 +var legalflags = new Array(); var illegalflags = new Array(); 1.32 + 1.33 + 1.34 +// valid regular expressions to try - 1.35 +legalpatterns[0] = ''; 1.36 +legalpatterns[1] = 'abc'; 1.37 +legalpatterns[2] = '(.*)(3-1)\s\w'; 1.38 +legalpatterns[3] = '(.*)(...)\\s\\w'; 1.39 +legalpatterns[4] = '[^A-Za-z0-9_]'; 1.40 +legalpatterns[5] = '[^\f\n\r\t\v](123.5)([4 - 8]$)'; 1.41 + 1.42 +// invalid regular expressions to try - 1.43 +illegalpatterns[0] = '(?)'; 1.44 +illegalpatterns[1] = '(a'; 1.45 +illegalpatterns[2] = '( ]'; 1.46 +//illegalpatterns[3] = '\d{1,s}'; 1.47 + 1.48 +// valid flags to try - 1.49 +legalflags[0] = 'i'; 1.50 +legalflags[1] = 'g'; 1.51 +legalflags[2] = 'm'; 1.52 +legalflags[3] = undefined; 1.53 + 1.54 +// invalid flags to try - 1.55 +illegalflags[0] = 'a'; 1.56 +illegalflags[1] = 123; 1.57 +illegalflags[2] = new RegExp(); 1.58 + 1.59 + 1.60 + 1.61 +//------------------------------------------------------------------------------------------------- 1.62 +test(); 1.63 +//------------------------------------------------------------------------------------------------- 1.64 + 1.65 + 1.66 +function test() 1.67 +{ 1.68 + enterFunc ('test'); 1.69 + printBugNumber(BUGNUMBER); 1.70 + printStatus (summary); 1.71 + 1.72 + testIllegalRegExps(legalpatterns, illegalflags); 1.73 + testIllegalRegExps(illegalpatterns, legalflags); 1.74 + testIllegalRegExps(illegalpatterns, illegalflags); 1.75 + 1.76 + exitFunc ('test'); 1.77 +} 1.78 + 1.79 + 1.80 +// This function will only be called where all the patterns are illegal, or all the flags 1.81 +function testIllegalRegExps(patterns, flags) 1.82 +{ 1.83 + for (i in patterns) 1.84 + { 1.85 + s = patterns[i]; 1.86 + 1.87 + for (j in flags) 1.88 + { 1.89 + f = flags[j]; 1.90 + status = getStatus(s, f); 1.91 + actual = cnFAILURE; 1.92 + expect = cnSUCCESS; 1.93 + 1.94 + try 1.95 + { 1.96 + // This should cause an exception if either s or f is illegal - 1.97 + eval('obj = new RegExp(s, f);'); 1.98 + } 1.99 + catch(e) 1.100 + { 1.101 + // We expect to get a SyntaxError - test for this: 1.102 + if (e instanceof SyntaxError) 1.103 + actual = cnSUCCESS; 1.104 + } 1.105 + 1.106 + reportCompare(expect, actual, status); 1.107 + } 1.108 + } 1.109 +} 1.110 + 1.111 + 1.112 +function getStatus(regexp, flag) 1.113 +{ 1.114 + return (statprefix + quote(regexp) + statsuffix + quote(flag)); 1.115 +} 1.116 + 1.117 + 1.118 +function quote(text) 1.119 +{ 1.120 + return (singlequote + text + singlequote); 1.121 +}