Skip to content
Permalink
Browse files

Use _SSE2_ instrs to to sanitize name

  • Loading branch information
laruence committed Mar 9, 2020
1 parent 8eb4f1f commit 9a673c65e02f0342279ba10b9fa023319b59181d
Showing with 39 additions and 4 deletions.
  1. +8 −3 tests/issue297.phpt
  2. +31 −1 yaf_loader.c
@@ -10,15 +10,20 @@ yaf.lowcase_path=0
yaf.throw_exception=0
yaf.catch_exception=1
yaf.use_namespace=1
yaf.use_spl_autoload=0
--FILE--
<?php
$loader = Yaf\Loader::getInstance(__DIR__, __DIR__);
var_dump(new \Test\Test);
var_dump($loader->autoload("Test\Test"));
var_dump(new \Test\Test\Test\Test\Test\Test\T\Test\Test); //for __SSE2__
?>
--EXPECTF--
Warning: Yaf\Loader::autoload(): Failed opening script %s/Test/Test.php: No such file or directory in %sissue297.php on line %d
Warning: Yaf\Loader::autoload(): Failed opening script %s%cTest/Test.php: No such file or directory in %sissue297.php on line %d
bool(true)

Fatal error: Uncaught Error: Class 'Test\Test' not found in %sissue297.php:%d
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

Fatal error: Uncaught Error: Class 'Test\Test\Test\Test\Test\Test\T\Test\Test' not found in %sissue297.php:%d
Stack trace:
#0 {main}
thrown in %sissue297.php on line %d
@@ -21,6 +21,10 @@
#include "php.h"
#include "zend_smart_str.h" /* for smart_str */

#ifdef __SSE2__
#include <emmintrin.h>
#endif

#include "php_yaf.h"
#include "yaf_application.h"
#include "yaf_namespace.h"
@@ -221,7 +225,33 @@ static inline char* yaf_loader_sanitize_name(char *name, size_t len) /* {{{ */ {
/* replace all '\' to '_' */
sanitized_name = estrndup(name, len);
pos = sanitized_name + (pos - name);
while ((*pos = '_', pos = memchr(pos, '\\', len - (pos - sanitized_name))));
#ifdef __SSE2__
do {
const __m128i slash = _mm_set1_epi8('\\');
const __m128i delta = _mm_set1_epi8('_' - '\\');
len -= (pos - sanitized_name);
while (len >= 16) {
__m128i op = _mm_loadu_si128((__m128i *)pos);
__m128i eq = _mm_cmpeq_epi8(op, slash);
if (_mm_movemask_epi8(eq)) {
eq = _mm_and_si128(eq, delta);
op = _mm_add_epi8(op, eq);
_mm_storeu_si128((__m128i*)pos, op);
}
len -= 16;
pos += 16;
}
} while (0);

if (len) {
name = pos;
while ((pos = memchr(pos, '\\', len - (pos - name)))) {
*pos++ = '_';
}
}
#else
while ((*pos++ = '_', pos = memchr(pos, '\\', len - (pos - sanitized_name))));
#endif
}

return sanitized_name;

0 comments on commit 9a673c6

Please sign in to comment.
You can’t perform that action at this time.