Skip to content

Commit 9a673c6

Browse files
committed
Use _SSE2_ instrs to to sanitize name
1 parent 8eb4f1f commit 9a673c6

File tree

2 files changed

+39
-4
lines changed

2 files changed

+39
-4
lines changed

tests/issue297.phpt

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,20 @@ yaf.lowcase_path=0
1010
yaf.throw_exception=0
1111
yaf.catch_exception=1
1212
yaf.use_namespace=1
13+
yaf.use_spl_autoload=0
1314
--FILE--
1415
<?php
1516
$loader = Yaf\Loader::getInstance(__DIR__, __DIR__);
16-
var_dump(new \Test\Test);
17+
var_dump($loader->autoload("Test\Test"));
18+
var_dump(new \Test\Test\Test\Test\Test\Test\T\Test\Test); //for __SSE2__
1719
?>
1820
--EXPECTF--
19-
Warning: Yaf\Loader::autoload(): Failed opening script %s/Test/Test.php: No such file or directory in %sissue297.php on line %d
21+
Warning: Yaf\Loader::autoload(): Failed opening script %s%cTest/Test.php: No such file or directory in %sissue297.php on line %d
22+
bool(true)
2023

21-
Fatal error: Uncaught Error: Class 'Test\Test' not found in %sissue297.php:%d
24+
Warning: Yaf\Loader::autoload(): Failed opening script %s/Test/Test/Test/Test/Test/Test/T/Test/Test.php: No such file or directory in %sissue297.php on line %d
25+
26+
Fatal error: Uncaught Error: Class 'Test\Test\Test\Test\Test\Test\T\Test\Test' not found in %sissue297.php:%d
2227
Stack trace:
2328
#0 {main}
2429
thrown in %sissue297.php on line %d

yaf_loader.c

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@
2121
#include "php.h"
2222
#include "zend_smart_str.h" /* for smart_str */
2323

24+
#ifdef __SSE2__
25+
#include <emmintrin.h>
26+
#endif
27+
2428
#include "php_yaf.h"
2529
#include "yaf_application.h"
2630
#include "yaf_namespace.h"
@@ -221,7 +225,33 @@ static inline char* yaf_loader_sanitize_name(char *name, size_t len) /* {{{ */ {
221225
/* replace all '\' to '_' */
222226
sanitized_name = estrndup(name, len);
223227
pos = sanitized_name + (pos - name);
224-
while ((*pos = '_', pos = memchr(pos, '\\', len - (pos - sanitized_name))));
228+
#ifdef __SSE2__
229+
do {
230+
const __m128i slash = _mm_set1_epi8('\\');
231+
const __m128i delta = _mm_set1_epi8('_' - '\\');
232+
len -= (pos - sanitized_name);
233+
while (len >= 16) {
234+
__m128i op = _mm_loadu_si128((__m128i *)pos);
235+
__m128i eq = _mm_cmpeq_epi8(op, slash);
236+
if (_mm_movemask_epi8(eq)) {
237+
eq = _mm_and_si128(eq, delta);
238+
op = _mm_add_epi8(op, eq);
239+
_mm_storeu_si128((__m128i*)pos, op);
240+
}
241+
len -= 16;
242+
pos += 16;
243+
}
244+
} while (0);
245+
246+
if (len) {
247+
name = pos;
248+
while ((pos = memchr(pos, '\\', len - (pos - name)))) {
249+
*pos++ = '_';
250+
}
251+
}
252+
#else
253+
while ((*pos++ = '_', pos = memchr(pos, '\\', len - (pos - sanitized_name))));
254+
#endif
225255
}
226256

227257
return sanitized_name;

0 commit comments

Comments
 (0)