1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/js/src/tests/ecma_3/RegExp/regress-223273.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,245 @@ 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 + * 1.11 + * Date: 23 October 2003 1.12 + * SUMMARY: Unescaped, unbalanced parens in a regexp should cause SyntaxError. 1.13 + * 1.14 + * The same would also be true for unescaped, unbalanced brackets or braces 1.15 + * if we followed the ECMA-262 Ed. 3 spec on this. But it was decided for 1.16 + * backward compatibility reasons to follow Perl 5, which permits 1.17 + * 1.18 + * 1. an unescaped, unbalanced right bracket ] 1.19 + * 2. an unescaped, unbalanced left brace { 1.20 + * 3. an unescaped, unbalanced right brace } 1.21 + * 1.22 + * If any of these should occur, Perl treats each as a literal 1.23 + * character. Therefore we permit all three of these cases, even 1.24 + * though not ECMA-compliant. Note Perl errors on an unescaped, 1.25 + * unbalanced left bracket; so will we. 1.26 + * 1.27 + * See http://bugzilla.mozilla.org/show_bug.cgi?id=223273 1.28 + * 1.29 + */ 1.30 +//----------------------------------------------------------------------------- 1.31 +var UBound = 0; 1.32 +var BUGNUMBER = 223273; 1.33 +var summary = 'Unescaped, unbalanced parens in regexp should be a SyntaxError'; 1.34 +var TEST_PASSED = 'SyntaxError'; 1.35 +var TEST_FAILED = 'Generated an error, but NOT a SyntaxError!'; 1.36 +var TEST_FAILED_BADLY = 'Did not generate ANY error!!!'; 1.37 +var CHECK_PASSED = 'Should not generate an error'; 1.38 +var CHECK_FAILED = 'Generated an error!'; 1.39 +var status = ''; 1.40 +var statusitems = []; 1.41 +var actual = ''; 1.42 +var actualvalues = []; 1.43 +var expect= ''; 1.44 +var expectedvalues = []; 1.45 + 1.46 + 1.47 +/* 1.48 + * All the following contain unescaped, unbalanced parens and 1.49 + * should generate SyntaxErrors. That's what we're testing for. 1.50 + * 1.51 + * To allow the test to compile and run, we have to hide the errors 1.52 + * inside eval strings, and check they are caught at run-time. 1.53 + * 1.54 + * Inside such strings, remember to escape any escape character! 1.55 + */ 1.56 +status = inSection(1); 1.57 +testThis(' /(/ '); 1.58 + 1.59 +status = inSection(2); 1.60 +testThis(' /)/ '); 1.61 + 1.62 +status = inSection(3); 1.63 +testThis(' /(abc\\)def(g/ '); 1.64 + 1.65 +status = inSection(4); 1.66 +testThis(' /\\(abc)def)g/ '); 1.67 + 1.68 + 1.69 +/* 1.70 + * These regexp patterns are correct and should not generate 1.71 + * any errors. Note we use checkThis() instead of testThis(). 1.72 + */ 1.73 +status = inSection(5); 1.74 +checkThis(' /\\(/ '); 1.75 + 1.76 +status = inSection(6); 1.77 +checkThis(' /\\)/ '); 1.78 + 1.79 +status = inSection(7); 1.80 +checkThis(' /(abc)def\\(g/ '); 1.81 + 1.82 +status = inSection(8); 1.83 +checkThis(' /(abc\\)def)g/ '); 1.84 + 1.85 +status = inSection(9); 1.86 +checkThis(' /(abc(\\))def)g/ '); 1.87 + 1.88 +status = inSection(10); 1.89 +checkThis(' /(abc([x\\)yz]+)def)g/ '); 1.90 + 1.91 + 1.92 + 1.93 +/* 1.94 + * Unescaped, unbalanced left brackets should be a SyntaxError 1.95 + */ 1.96 +status = inSection(11); 1.97 +testThis(' /[/ '); 1.98 + 1.99 +status = inSection(12); 1.100 +testThis(' /[abc\\]def[g/ '); 1.101 + 1.102 + 1.103 +/* 1.104 + * We permit unescaped, unbalanced right brackets, as does Perl. 1.105 + * No error should result, even though this is not ECMA-compliant. 1.106 + * Note we use checkThis() instead of testThis(). 1.107 + */ 1.108 +status = inSection(13); 1.109 +checkThis(' /]/ '); 1.110 + 1.111 +status = inSection(14); 1.112 +checkThis(' /\\[abc]def]g/ '); 1.113 + 1.114 + 1.115 +/* 1.116 + * These regexp patterns are correct and should not generate 1.117 + * any errors. Note we use checkThis() instead of testThis(). 1.118 + */ 1.119 +status = inSection(15); 1.120 +checkThis(' /\\[/ '); 1.121 + 1.122 +status = inSection(16); 1.123 +checkThis(' /\\]/ '); 1.124 + 1.125 +status = inSection(17); 1.126 +checkThis(' /[abc]def\\[g/ '); 1.127 + 1.128 +status = inSection(18); 1.129 +checkThis(' /[abc\\]def]g/ '); 1.130 + 1.131 +status = inSection(19); 1.132 +checkThis(' /(abc[\\]]def)g/ '); 1.133 + 1.134 +status = inSection(20); 1.135 +checkThis(' /[abc(x\\]yz+)def]g/ '); 1.136 + 1.137 + 1.138 + 1.139 +/* 1.140 + * Run some tests for unbalanced braces. We again follow Perl, and 1.141 + * thus permit unescaped unbalanced braces - both left and right, 1.142 + * even though this is not ECMA-compliant. 1.143 + * 1.144 + * Note we use checkThis() instead of testThis(). 1.145 + */ 1.146 +status = inSection(21); 1.147 +checkThis(' /abc{def/ '); 1.148 + 1.149 +status = inSection(22); 1.150 +checkThis(' /abc}def/ '); 1.151 + 1.152 +status = inSection(23); 1.153 +checkThis(' /a{2}bc{def/ '); 1.154 + 1.155 +status = inSection(24); 1.156 +checkThis(' /a}b{3}c}def/ '); 1.157 + 1.158 + 1.159 +/* 1.160 + * These regexp patterns are correct and should not generate 1.161 + * any errors. Note we use checkThis() instead of testThis(). 1.162 + */ 1.163 +status = inSection(25); 1.164 +checkThis(' /abc\\{def/ '); 1.165 + 1.166 +status = inSection(26); 1.167 +checkThis(' /abc\\}def/ '); 1.168 + 1.169 +status = inSection(27); 1.170 +checkThis(' /a{2}bc\\{def/ '); 1.171 + 1.172 +status = inSection(28); 1.173 +checkThis(' /a\\}b{3}c\\}def/ '); 1.174 + 1.175 + 1.176 + 1.177 + 1.178 +//----------------------------------------------------------------------------- 1.179 +test(); 1.180 +//----------------------------------------------------------------------------- 1.181 + 1.182 + 1.183 + 1.184 + 1.185 +/* 1.186 + * Invalid syntax should generate a SyntaxError 1.187 + */ 1.188 +function testThis(sInvalidSyntax) 1.189 +{ 1.190 + expect = TEST_PASSED; 1.191 + actual = TEST_FAILED_BADLY; 1.192 + 1.193 + try 1.194 + { 1.195 + eval(sInvalidSyntax); 1.196 + } 1.197 + catch(e) 1.198 + { 1.199 + if (e instanceof SyntaxError) 1.200 + actual = TEST_PASSED; 1.201 + else 1.202 + actual = TEST_FAILED; 1.203 + } 1.204 + 1.205 + statusitems[UBound] = status; 1.206 + expectedvalues[UBound] = expect; 1.207 + actualvalues[UBound] = actual; 1.208 + UBound++; 1.209 +} 1.210 + 1.211 + 1.212 +/* 1.213 + * Valid syntax shouldn't generate any errors 1.214 + */ 1.215 +function checkThis(sValidSyntax) 1.216 +{ 1.217 + expect = CHECK_PASSED; 1.218 + actual = CHECK_PASSED; 1.219 + 1.220 + try 1.221 + { 1.222 + eval(sValidSyntax); 1.223 + } 1.224 + catch(e) 1.225 + { 1.226 + actual = CHECK_FAILED; 1.227 + } 1.228 + 1.229 + statusitems[UBound] = status; 1.230 + expectedvalues[UBound] = expect; 1.231 + actualvalues[UBound] = actual; 1.232 + UBound++; 1.233 +} 1.234 + 1.235 + 1.236 +function test() 1.237 +{ 1.238 + enterFunc('test'); 1.239 + printBugNumber(BUGNUMBER); 1.240 + printStatus(summary); 1.241 + 1.242 + for (var i=0; i<UBound; i++) 1.243 + { 1.244 + reportCompare(expectedvalues[i], actualvalues[i], statusitems[i]); 1.245 + } 1.246 + 1.247 + exitFunc ('test'); 1.248 +}