Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 18 additions & 7 deletions ext/ffi/ffi.c
Original file line number Diff line number Diff line change
Expand Up @@ -7124,11 +7124,16 @@ void zend_ffi_align_as_val(zend_ffi_dcl *dcl, const zend_ffi_val *align_val) /*
switch (align_val->kind) {
case ZEND_FFI_VAL_INT32:
case ZEND_FFI_VAL_UINT32:
dcl->align = zend_ffi_type_uint32.align;
break;
case ZEND_FFI_VAL_INT64:
case ZEND_FFI_VAL_UINT64:
dcl->align = zend_ffi_type_uint64.align;
/* _Alignas(0) has no effect */
if (align_val->u64 == 0) {
break;
}
if (align_val->u64 > 0x80000000 || (align_val->u64 & (align_val->u64 - 1)) != 0) {
zend_ffi_parser_error("Invalid alignment %" PRIu64 " at line %d", align_val->u64, FFI_G(line));
}
dcl->align = (uint32_t)align_val->u64;
break;
case ZEND_FFI_VAL_FLOAT:
dcl->align = zend_ffi_type_float.align;
Expand Down Expand Up @@ -7306,7 +7311,7 @@ void zend_ffi_align_as_val(zend_ffi_dcl *dcl, const zend_ffi_val *align_val) /*
val->i64 = (zend_ffi_double)val->u64 OP op2->d; \
} else if (op2->kind == ZEND_FFI_VAL_CHAR) { \
val->kind = ZEND_FFI_VAL_INT32; \
val->i64 = val->u64 OP op2->d; \
val->i64 = val->u64 OP op2->ch; \
} else { \
val->kind = ZEND_FFI_VAL_ERROR; \
} \
Expand Down Expand Up @@ -7492,6 +7497,11 @@ void zend_ffi_expr_div(zend_ffi_val *val, const zend_ffi_val *op2) /* {{{ */

void zend_ffi_expr_mod(zend_ffi_val *val, const zend_ffi_val *op2) /* {{{ */
{
/* the operands of % must have integer type */
if (val->kind == ZEND_FFI_VAL_FLOAT || val->kind == ZEND_FFI_VAL_DOUBLE || val->kind == ZEND_FFI_VAL_LONG_DOUBLE
|| op2->kind == ZEND_FFI_VAL_FLOAT || op2->kind == ZEND_FFI_VAL_DOUBLE || op2->kind == ZEND_FFI_VAL_LONG_DOUBLE) {
zend_ffi_parser_error("Invalid operand of '%%' in constant expression at line %d", FFI_G(line));
}
zend_ffi_expr_int_math(val, op2, %); // ???
}
/* }}} */
Expand Down Expand Up @@ -7704,7 +7714,7 @@ void zend_ffi_expr_sizeof_val(zend_ffi_val *val) /* {{{ */
#endif
} else if (val->kind == ZEND_FFI_VAL_CHAR) {
val->kind = ZEND_FFI_VAL_UINT32;
val->u64 = zend_ffi_type_char.size;
val->u64 = zend_ffi_type_sint32.size; /* a character constant has type int */
} else if (val->kind == ZEND_FFI_VAL_STRING) {
if (memchr(val->str, '\\', val->len)) {
// TODO: support for escape sequences ???
Expand Down Expand Up @@ -7752,7 +7762,7 @@ void zend_ffi_expr_alignof_val(zend_ffi_val *val) /* {{{ */
#endif
} else if (val->kind == ZEND_FFI_VAL_CHAR) {
val->kind = ZEND_FFI_VAL_UINT32;
val->u64 = zend_ffi_type_char.size;
val->u64 = zend_ffi_type_sint32.align; /* a character constant has type int */
} else if (val->kind == ZEND_FFI_VAL_STRING) {
val->kind = ZEND_FFI_VAL_UINT32;
val->u64 = _Alignof(char*);
Expand Down Expand Up @@ -7853,7 +7863,8 @@ void zend_ffi_val_character(zend_ffi_val *val, const char *str, size_t str_len)
if (str_len == 3) {
val->ch = str[1];
} else if (str[1] == '\\') {
if (str[2] == 'a') {
if (str[2] == 'a' && str_len == 4) {
val->ch = '\a';
} else if (str[2] == 'b' && str_len == 4) {
val->ch = '\b';
} else if (str[2] == 'f' && str_len == 4) {
Expand Down
30 changes: 30 additions & 0 deletions ext/ffi/tests/gh25573_alignas.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
--TEST--
GH-25573 (FFI _Alignas with an integer constant)
--EXTENSIONS--
ffi
--INI--
ffi.enable=1
--FILE--
<?php
function size(string $expr) {
try {
$ffi = FFI::cdef("typedef char A[$expr];");
return FFI::sizeof($ffi->type("A"));
} catch (FFI\ParserException $e) {
return $e->getMessage();
}
}
$ffi = FFI::cdef("typedef _Alignas(16) char T;");
var_dump(FFI::alignof($ffi->type("T")));
$ffi = FFI::cdef("typedef _Alignas(0) int T;");
var_dump(FFI::alignof($ffi->type("T")));
try {
FFI::cdef("typedef _Alignas(3) char T;");
} catch (FFI\ParserException $e) {
echo $e->getMessage(), "\n";
}
?>
--EXPECT--
int(16)
int(4)
Invalid alignment 3 at line 1
20 changes: 20 additions & 0 deletions ext/ffi/tests/gh25573_bell.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
--TEST--
GH-25573 (FFI '\a' character constant)
--EXTENSIONS--
ffi
--INI--
ffi.enable=1
--FILE--
<?php
function size(string $expr) {
try {
$ffi = FFI::cdef("typedef char A[$expr];");
return FFI::sizeof($ffi->type("A"));
} catch (FFI\ParserException $e) {
return $e->getMessage();
}
}
var_dump(size("'\\a'"));
?>
--EXPECT--
int(7)
20 changes: 20 additions & 0 deletions ext/ffi/tests/gh25573_char_cmp.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
--TEST--
GH-25573 (FFI comparison of an unsigned value with a character constant)
--EXTENSIONS--
ffi
--INI--
ffi.enable=1
--FILE--
<?php
function size(string $expr) {
try {
$ffi = FFI::cdef("typedef char A[$expr];");
return FFI::sizeof($ffi->type("A"));
} catch (FFI\ParserException $e) {
return $e->getMessage();
}
}
var_dump(size("(0xFFFFFFFFu > 'A') ? 7 : 13"));
?>
--EXPECT--
int(7)
22 changes: 22 additions & 0 deletions ext/ffi/tests/gh25573_char_sizeof.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
--TEST--
GH-25573 (FFI sizeof and __alignof__ of a character constant)
--EXTENSIONS--
ffi
--INI--
ffi.enable=1
--FILE--
<?php
function size(string $expr) {
try {
$ffi = FFI::cdef("typedef char A[$expr];");
return FFI::sizeof($ffi->type("A"));
} catch (FFI\ParserException $e) {
return $e->getMessage();
}
}
var_dump(size("sizeof('a')"));
var_dump(size("__alignof__('a')"));
?>
--EXPECT--
int(4)
int(4)
24 changes: 24 additions & 0 deletions ext/ffi/tests/gh25573_mod_float.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
--TEST--
GH-25573 (FFI % with a floating operand)
--EXTENSIONS--
ffi
--INI--
ffi.enable=1
--FILE--
<?php
function size(string $expr) {
try {
$ffi = FFI::cdef("typedef char A[$expr];");
return FFI::sizeof($ffi->type("A"));
} catch (FFI\ParserException $e) {
return $e->getMessage();
}
}
var_dump(size("(1.0 % 2) ? 7 : 13"));
var_dump(size("(1 % 2.0) ? 7 : 13"));
var_dump(size("(7 % 4) ? 7 : 13"));
?>
--EXPECT--
string(55) "Invalid operand of '%' in constant expression at line 1"
string(55) "Invalid operand of '%' in constant expression at line 1"
int(7)
Loading