Skip to content

Commit 343d1c2

Browse files
committed
fileinfo: Obey POSIX in pattern conversion
Signed-off-by: Anatol Belski <[email protected]>
1 parent 34bdab5 commit 343d1c2

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

ext/fileinfo/libmagic/softmagic.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2025,6 +2025,7 @@ public zend_string* convert_libmagic_pattern(const char *val, size_t len, uint32
20252025
j = 0;
20262026
ZSTR_VAL(t)[j++] = '~';
20272027

2028+
bool add_newline = false;
20282029
for (i = 0; i < len; i++, j++) {
20292030
switch (val[i]) {
20302031
case '~':
@@ -2037,6 +2038,21 @@ public zend_string* convert_libmagic_pattern(const char *val, size_t len, uint32
20372038
ZSTR_VAL(t)[j++] = '0';
20382039
ZSTR_VAL(t)[j] = '0';
20392040
break;
2041+
/* If POSIX regex class negates, make sure possible '\n' is taken into
2042+
account. For example "[^;]" is converted to "[^;]\n?" to obey POSIX. */
2043+
case '^':
2044+
ZSTR_VAL(t)[j] = val[i];
2045+
add_newline = (j >= 1 && '[' == ZSTR_VAL(t)[j-1]);
2046+
break;
2047+
case ']':
2048+
ZSTR_VAL(t)[j] = val[i];
2049+
if (add_newline) {
2050+
t = zend_string_realloc(t, ZSTR_LEN(t) + 3, GC_TYPE_INFO(t) & IS_STR_PERSISTENT);
2051+
ZSTR_VAL(t)[j++] = '\\';
2052+
ZSTR_VAL(t)[j++] = 'n';
2053+
ZSTR_VAL(t)[j++] = '?';
2054+
add_newline = false;
2055+
}
20402056
default:
20412057
ZSTR_VAL(t)[j] = val[i];
20422058
break;

0 commit comments

Comments
 (0)