Skip to content

Commit dca7abe

Browse files
committed
Fix #307. Invalid escape in string literals.
1 parent db53f36 commit dca7abe

File tree

9 files changed

+28
-13
lines changed

9 files changed

+28
-13
lines changed

exts/jphp-zend-ext/src/main/tests/resources/ext/strings/ltrim_001.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
--FILE--
44
<?php
55
$text = "\t\tThese are a few words :) ... ";
6-
$binary = "\x09\Example string\x0A";
6+
$binary = "\x09Example string\x0A";
77
$hello = "Hello World";
88
var_dump($text, $binary, $hello);
99

exts/jphp-zend-ext/src/main/tests/resources/ext/strings/rtrim_001.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
--FILE--
44
<?php
55
$text = "\t\tThese are a few words :) ... ";
6-
$binary = "\x09\Example string\x0A";
6+
$binary = "\x09Example string\x0A";
77
$hello = "Hello World";
88
var_dump($text, $binary, $hello);
99

exts/jphp-zend-ext/src/main/tests/resources/ext/strings/trim_001.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
--FILE--
44
<?php
55
$text = "\t\tThese are a few words :) ... ";
6-
$binary = "\x09\Example string\x0A";
6+
$binary = "\x09Example string\x0A";
77
$hello = "Hello World";
88
var_dump($text, $binary, $hello);
99

jphp-core/src/org/develnext/jphp/core/tokenizer/Tokenizer.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -453,12 +453,14 @@ protected ValueExprToken readString(StringExprToken.Quote quote, int startPositi
453453
break;
454454
case 'x':
455455
int t = i + 1;
456+
int cnt = 0;
456457
for(int j = 1; j < 5; j++){
457458
t = i + j;
458459
if (t < codeLength){
459460
char digit = code.charAt(t);
460461
if (Character.isDigit(digit) || (digit >= 'A' && digit <= 'F') || (digit >= 'a' && digit <= 'f')){
461-
// nop;
462+
cnt++;
463+
if (cnt == 3) break; // only \xhh
462464
} else {
463465
break;
464466
}
@@ -478,8 +480,12 @@ protected ValueExprToken readString(StringExprToken.Quote quote, int startPositi
478480
break;
479481
case '$':
480482
case '"':
483+
slash = false;
484+
sb.append(ch); break;
485+
481486
default:
482487
slash = false;
488+
sb.append("\\");
483489
sb.append(ch); break;
484490
}
485491
} else {

jphp-core/tests/org/develnext/jphp/TokenizerTest.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -201,12 +201,21 @@ public void testMagicString() throws IOException {
201201

202202
token = tokenizer.nextToken();
203203
assertTrue(token instanceof StringExprToken);
204-
assertEquals(".{$foo}", ((StringExprToken) token).getValue());
204+
assertEquals("\\.{$foo}", ((StringExprToken) token).getValue());
205205
assertEquals(1, ((StringExprToken) token).getSegments().size());
206206

207207
StringExprToken.Segment segment = ((StringExprToken) token).getSegments().get(0);
208-
assertEquals(1, segment.from);
209-
assertEquals(7, segment.to);
208+
assertEquals(2, segment.from);
209+
assertEquals(8, segment.to);
210+
}
211+
212+
@Test
213+
public void testEscapeBug307() throws IOException {
214+
Token token;
215+
Tokenizer tokenizer = new Tokenizer(new Context("\"Ymd\\THis\\Z\""));
216+
token = tokenizer.nextToken();
217+
assertTrue(token instanceof StringExprToken);
218+
assertEquals("Ymd\\THis\\Z", ((StringExprToken) token).getValue());
210219
}
211220

212221
@Test

jphp-core/tests/resources/cast/array.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,18 @@
2121
return 'fail_object';
2222

2323

24-
class Foo {
24+
class Xoo {
2525
protected $x = 20;
2626
private $y = 30;
2727
public $z = 40;
2828
}
29-
$foo = new Foo();
29+
$foo = new Xoo();
3030
$arr = (array)$foo;
3131

3232
if ($arr["\x0*\x0x"] !== 20)
3333
return 'fail_object_protected';
3434

35-
if ($arr["\x0\Foo\x0y"] !== 30)
35+
if ($arr["\x0Xoo\x0y"] !== 30)
3636
return 'fail_object_private';
3737

3838
if ($arr['z'] !== 40)

jphp-core/tests/resources/ext/strings/ltrim_001.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
--FILE--
44
<?php
55
$text = "\t\tThese are a few words :) ... ";
6-
$binary = "\x09\Example string\x0A";
6+
$binary = "\x09Example string\x0A";
77
$hello = "Hello World";
88
var_dump($text, $binary, $hello);
99

jphp-core/tests/resources/ext/strings/rtrim_001.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
--FILE--
44
<?php
55
$text = "\t\tThese are a few words :) ... ";
6-
$binary = "\x09\Example string\x0A";
6+
$binary = "\x09Example string\x0A";
77
$hello = "Hello World";
88
var_dump($text, $binary, $hello);
99

jphp-core/tests/resources/ext/strings/trim_001.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
--FILE--
44
<?php
55
$text = "\t\tThese are a few words :) ... ";
6-
$binary = "\x09\Example string\x0A";
6+
$binary = "\x09Example string\x0A";
77
$hello = "Hello World";
88
var_dump($text, $binary, $hello);
99

0 commit comments

Comments
 (0)