diff --git a/ext/ffi/ffi.c b/ext/ffi/ffi.c index 8e2a205838cf..337e55638822 100644 --- a/ext/ffi/ffi.c +++ b/ext/ffi/ffi.c @@ -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; @@ -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; \ } \ @@ -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, %); // ??? } /* }}} */ @@ -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 ??? @@ -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*); @@ -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) { diff --git a/ext/ffi/tests/gh25573_alignas.phpt b/ext/ffi/tests/gh25573_alignas.phpt new file mode 100644 index 000000000000..b684db9aa9da --- /dev/null +++ b/ext/ffi/tests/gh25573_alignas.phpt @@ -0,0 +1,30 @@ +--TEST-- +GH-25573 (FFI _Alignas with an integer constant) +--EXTENSIONS-- +ffi +--INI-- +ffi.enable=1 +--FILE-- +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 diff --git a/ext/ffi/tests/gh25573_bell.phpt b/ext/ffi/tests/gh25573_bell.phpt new file mode 100644 index 000000000000..48d2a700a880 --- /dev/null +++ b/ext/ffi/tests/gh25573_bell.phpt @@ -0,0 +1,20 @@ +--TEST-- +GH-25573 (FFI '\a' character constant) +--EXTENSIONS-- +ffi +--INI-- +ffi.enable=1 +--FILE-- +type("A")); + } catch (FFI\ParserException $e) { + return $e->getMessage(); + } +} +var_dump(size("'\\a'")); +?> +--EXPECT-- +int(7) diff --git a/ext/ffi/tests/gh25573_char_cmp.phpt b/ext/ffi/tests/gh25573_char_cmp.phpt new file mode 100644 index 000000000000..7974b414f03e --- /dev/null +++ b/ext/ffi/tests/gh25573_char_cmp.phpt @@ -0,0 +1,20 @@ +--TEST-- +GH-25573 (FFI comparison of an unsigned value with a character constant) +--EXTENSIONS-- +ffi +--INI-- +ffi.enable=1 +--FILE-- +type("A")); + } catch (FFI\ParserException $e) { + return $e->getMessage(); + } +} +var_dump(size("(0xFFFFFFFFu > 'A') ? 7 : 13")); +?> +--EXPECT-- +int(7) diff --git a/ext/ffi/tests/gh25573_char_sizeof.phpt b/ext/ffi/tests/gh25573_char_sizeof.phpt new file mode 100644 index 000000000000..1ef4ebe11cb5 --- /dev/null +++ b/ext/ffi/tests/gh25573_char_sizeof.phpt @@ -0,0 +1,22 @@ +--TEST-- +GH-25573 (FFI sizeof and __alignof__ of a character constant) +--EXTENSIONS-- +ffi +--INI-- +ffi.enable=1 +--FILE-- +type("A")); + } catch (FFI\ParserException $e) { + return $e->getMessage(); + } +} +var_dump(size("sizeof('a')")); +var_dump(size("__alignof__('a')")); +?> +--EXPECT-- +int(4) +int(4) diff --git a/ext/ffi/tests/gh25573_mod_float.phpt b/ext/ffi/tests/gh25573_mod_float.phpt new file mode 100644 index 000000000000..25030a46fb9a --- /dev/null +++ b/ext/ffi/tests/gh25573_mod_float.phpt @@ -0,0 +1,24 @@ +--TEST-- +GH-25573 (FFI % with a floating operand) +--EXTENSIONS-- +ffi +--INI-- +ffi.enable=1 +--FILE-- +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)