Skip to content
Draft
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
98 changes: 48 additions & 50 deletions ext/ffi/ffi.c
Original file line number Diff line number Diff line change
Expand Up @@ -880,15 +880,15 @@ static zend_always_inline zend_string *zend_ffi_mangled_func_name(zend_string *n
switch (type->func.abi) {
# ifdef HAVE_FFI_FASTCALL
case FFI_FASTCALL:
return strpprintf(0, "@%s@%zu", ZSTR_VAL(name), zend_ffi_arg_size(type));
return strpprintf(0, "@%pS@%zu", name, zend_ffi_arg_size(type));
# endif
# ifdef HAVE_FFI_STDCALL
case FFI_STDCALL:
return strpprintf(0, "_%s@%zu", ZSTR_VAL(name), zend_ffi_arg_size(type));
return strpprintf(0, "_%pS@%zu", name, zend_ffi_arg_size(type));
# endif
# ifdef HAVE_FFI_VECTORCALL_PARTIAL
case FFI_VECTORCALL_PARTIAL:
return strpprintf(0, "%s@@%zu", ZSTR_VAL(name), zend_ffi_arg_size(type));
return strpprintf(0, "%pS@@%zu", name, zend_ffi_arg_size(type));
# endif
default:
/* other calling conventions don't apply name mangling */
Expand Down Expand Up @@ -1250,13 +1250,13 @@ static zval *zend_ffi_cdata_read_field(zend_object *obj, zend_string *field_name
type = ZEND_FFI_TYPE(type->pointer.type);
}
if (UNEXPECTED(type->kind != ZEND_FFI_TYPE_STRUCT)) {
zend_throw_error(zend_ffi_exception_ce, "Attempt to read field '%s' of non C struct/union", ZSTR_VAL(field_name));
zend_throw_error(zend_ffi_exception_ce, "Attempt to read field '%pS' of non C struct/union", field_name);
return &EG(uninitialized_zval);
}

field = zend_hash_find_ptr(&type->record.fields, field_name);
if (UNEXPECTED(!field)) {
zend_throw_error(zend_ffi_exception_ce, "Attempt to read undefined field '%s' of C struct/union", ZSTR_VAL(field_name));
zend_throw_error(zend_ffi_exception_ce, "Attempt to read undefined field '%pS' of C struct/union", field_name);
return &EG(uninitialized_zval);
}

Expand Down Expand Up @@ -1319,20 +1319,20 @@ static zval *zend_ffi_cdata_write_field(zend_object *obj, zend_string *field_nam
field = *(cache_slot + 1);
} else {
if (UNEXPECTED(type == NULL)) {
zend_throw_error(zend_ffi_exception_ce, "Attempt to assign field '%s' to uninitialized FFI\\CData object", ZSTR_VAL(field_name));
zend_throw_error(zend_ffi_exception_ce, "Attempt to assign field '%pS' to uninitialized FFI\\CData object", field_name);
return value;
}
if (type->kind == ZEND_FFI_TYPE_POINTER) {
type = ZEND_FFI_TYPE(type->pointer.type);
}
if (UNEXPECTED(type->kind != ZEND_FFI_TYPE_STRUCT)) {
zend_throw_error(zend_ffi_exception_ce, "Attempt to assign field '%s' of non C struct/union", ZSTR_VAL(field_name));
zend_throw_error(zend_ffi_exception_ce, "Attempt to assign field '%pS' of non C struct/union", field_name);
return value;
}

field = zend_hash_find_ptr(&type->record.fields, field_name);
if (UNEXPECTED(!field)) {
zend_throw_error(zend_ffi_exception_ce, "Attempt to assign undefined field '%s' of C struct/union", ZSTR_VAL(field_name));
zend_throw_error(zend_ffi_exception_ce, "Attempt to assign undefined field '%pS' of C struct/union", field_name);
return value;
}

Expand Down Expand Up @@ -1366,7 +1366,7 @@ static zval *zend_ffi_cdata_write_field(zend_object *obj, zend_string *field_nam
zend_throw_error(zend_ffi_exception_ce, "Attempt to assign read-only location");
return value;
} else if (UNEXPECTED(field->is_const)) {
zend_throw_error(zend_ffi_exception_ce, "Attempt to assign read-only field '%s'", ZSTR_VAL(field_name));
zend_throw_error(zend_ffi_exception_ce, "Attempt to assign read-only field '%pS'", field_name);
return value;
}

Expand Down Expand Up @@ -1693,7 +1693,7 @@ static ZEND_COLD void zend_ffi_pass_incompatible(const zval *arg, const zend_ffi

buf1.start = buf1.end = buf1.buf + ((MAX_TYPE_NAME_LEN * 3) / 4);
if (!zend_ffi_ctype_name(&buf1, type)) {
zend_throw_error(zend_ffi_exception_ce, "Passing incompatible argument %d of C function '%s'", n + 1, ZSTR_VAL(EX(func)->internal_function.function_name));
zend_throw_error(zend_ffi_exception_ce, "Passing incompatible argument %d of C function '%pS'", n + 1, EX(func)->internal_function.function_name);
} else {
*buf1.end = 0;
if (Z_TYPE_P(arg) == IS_OBJECT && Z_OBJCE_P(arg) == zend_ffi_cdata_ce) {
Expand All @@ -1702,13 +1702,13 @@ static ZEND_COLD void zend_ffi_pass_incompatible(const zval *arg, const zend_ffi
type = ZEND_FFI_TYPE(cdata->type);
buf2.start = buf2.end = buf2.buf + ((MAX_TYPE_NAME_LEN * 3) / 4);
if (!zend_ffi_ctype_name(&buf2, type)) {
zend_throw_error(zend_ffi_exception_ce, "Passing incompatible argument %d of C function '%s', expecting '%s'", n + 1, ZSTR_VAL(EX(func)->internal_function.function_name), buf1.start);
zend_throw_error(zend_ffi_exception_ce, "Passing incompatible argument %d of C function '%pS', expecting '%s'", n + 1, EX(func)->internal_function.function_name, buf1.start);
} else {
*buf2.end = 0;
zend_throw_error(zend_ffi_exception_ce, "Passing incompatible argument %d of C function '%s', expecting '%s', found '%s'", n + 1, ZSTR_VAL(EX(func)->internal_function.function_name), buf1.start, buf2.start);
zend_throw_error(zend_ffi_exception_ce, "Passing incompatible argument %d of C function '%pS', expecting '%s', found '%s'", n + 1, EX(func)->internal_function.function_name, buf1.start, buf2.start);
}
} else {
zend_throw_error(zend_ffi_exception_ce, "Passing incompatible argument %d of C function '%s', expecting '%s', found PHP '%s'", n + 1, ZSTR_VAL(EX(func)->internal_function.function_name), buf1.start, zend_zval_value_name(arg));
zend_throw_error(zend_ffi_exception_ce, "Passing incompatible argument %d of C function '%pS', expecting '%s', found PHP '%s'", n + 1, EX(func)->internal_function.function_name, buf1.start, zend_zval_value_name(arg));
}
}
}
Expand Down Expand Up @@ -2502,7 +2502,7 @@ static zval *zend_ffi_read_var(zend_object *obj, zend_string *var_name, int read
}
}
if (!sym) {
zend_throw_error(zend_ffi_exception_ce, "Attempt to read undefined C variable '%s'", ZSTR_VAL(var_name));
zend_throw_error(zend_ffi_exception_ce, "Attempt to read undefined C variable '%pS'", var_name);
return &EG(uninitialized_zval);
}

Expand Down Expand Up @@ -2546,12 +2546,12 @@ static zval *zend_ffi_write_var(zend_object *obj, zend_string *var_name, zval *v
}
}
if (!sym) {
zend_throw_error(zend_ffi_exception_ce, "Attempt to assign undefined C variable '%s'", ZSTR_VAL(var_name));
zend_throw_error(zend_ffi_exception_ce, "Attempt to assign undefined C variable '%pS'", var_name);
return value;
}

if (sym->is_const) {
zend_throw_error(zend_ffi_exception_ce, "Attempt to assign read-only C variable '%s'", ZSTR_VAL(var_name));
zend_throw_error(zend_ffi_exception_ce, "Attempt to assign read-only C variable '%pS'", var_name);
return value;
}

Expand Down Expand Up @@ -2771,7 +2771,7 @@ static ZEND_FUNCTION(ffi_trampoline) /* {{{ */
arg_count = type->func.args ? zend_hash_num_elements(type->func.args) : 0;
if (type->attr & ZEND_FFI_ATTR_VARIADIC) {
if (arg_count > EX_NUM_ARGS()) {
zend_throw_error(zend_ffi_exception_ce, "Incorrect number of arguments for C function '%s', expecting at least %d parameter%s", ZSTR_VAL(EX(func)->internal_function.function_name), arg_count, (arg_count != 1) ? "s" : "");
zend_throw_error(zend_ffi_exception_ce, "Incorrect number of arguments for C function '%pS', expecting at least %d parameter%s", EX(func)->internal_function.function_name, arg_count, (arg_count != 1) ? "s" : "");
goto exit;
}
if (EX_NUM_ARGS()) {
Expand Down Expand Up @@ -2816,7 +2816,7 @@ static ZEND_FUNCTION(ffi_trampoline) /* {{{ */
}
} else {
if (arg_count != EX_NUM_ARGS()) {
zend_throw_error(zend_ffi_exception_ce, "Incorrect number of arguments for C function '%s', expecting exactly %d parameter%s", ZSTR_VAL(EX(func)->internal_function.function_name), arg_count, (arg_count != 1) ? "s" : "");
zend_throw_error(zend_ffi_exception_ce, "Incorrect number of arguments for C function '%pS', expecting exactly %d parameter%s", EX(func)->internal_function.function_name, arg_count, (arg_count != 1) ? "s" : "");
goto exit;
}
if (EX_NUM_ARGS()) {
Expand Down Expand Up @@ -2944,7 +2944,7 @@ static zend_function *zend_ffi_get_func(zend_object **obj, zend_string *name, co
}
}
if (!sym) {
zend_throw_error(zend_ffi_exception_ce, "Attempt to call undefined C function '%s'", ZSTR_VAL(name));
zend_throw_error(zend_ffi_exception_ce, "Attempt to call undefined C function '%pS'", name);
return NULL;
}

Expand Down Expand Up @@ -3071,13 +3071,13 @@ ZEND_METHOD(FFI, cdef) /* {{{ */
char *err = GET_DL_ERROR();
#ifdef PHP_WIN32
if (err && err[0]) {
zend_throw_error(zend_ffi_exception_ce, "Failed loading '%s' (%s)", ZSTR_VAL(lib), err);
zend_throw_error(zend_ffi_exception_ce, "Failed loading '%pS' (%s)", lib, err);
php_win32_error_msg_free(err);
} else {
zend_throw_error(zend_ffi_exception_ce, "Failed loading '%s' (Unknown reason)", ZSTR_VAL(lib));
zend_throw_error(zend_ffi_exception_ce, "Failed loading '%pS' (Unknown reason)", lib);
}
#else
zend_throw_error(zend_ffi_exception_ce, "Failed loading '%s' (%s)", ZSTR_VAL(lib), err);
zend_throw_error(zend_ffi_exception_ce, "Failed loading '%pS' (%s)", lib, err);
GET_DL_ERROR(); /* free the buffer storing the error */
#endif
RETURN_THROWS();
Expand Down Expand Up @@ -3110,7 +3110,7 @@ ZEND_METHOD(FFI, cdef) /* {{{ */
if (sym->kind == ZEND_FFI_SYM_VAR) {
addr = DL_FETCH_SYMBOL(handle, ZSTR_VAL(name));
if (!addr) {
zend_throw_error(zend_ffi_exception_ce, "Failed resolving C variable '%s'", ZSTR_VAL(name));
zend_throw_error(zend_ffi_exception_ce, "Failed resolving C variable '%pS'", name);
goto cleanup;
}
sym->addr = addr;
Expand All @@ -3120,7 +3120,7 @@ ZEND_METHOD(FFI, cdef) /* {{{ */
addr = DL_FETCH_SYMBOL(handle, ZSTR_VAL(mangled_name));
zend_string_release(mangled_name);
if (!addr) {
zend_throw_error(zend_ffi_exception_ce, "Failed resolving C function '%s'", ZSTR_VAL(name));
zend_throw_error(zend_ffi_exception_ce, "Failed resolving C function '%pS'", name);
goto cleanup;
}
sym->addr = addr;
Expand Down Expand Up @@ -3461,9 +3461,9 @@ static zend_ffi *zend_ffi_load(const char *filename, bool preload) /* {{{ */
addr = DL_FETCH_SYMBOL(handle, ZSTR_VAL(name));
if (!addr) {
if (preload) {
zend_error(E_WARNING, "FFI: failed pre-loading '%s', cannot resolve C variable '%s'", filename, ZSTR_VAL(name));
zend_error(E_WARNING, "FFI: failed pre-loading '%s', cannot resolve C variable '%pS'", filename, name);
} else {
zend_throw_error(zend_ffi_exception_ce, "Failed resolving C variable '%s'", ZSTR_VAL(name));
zend_throw_error(zend_ffi_exception_ce, "Failed resolving C variable '%pS'", name);
}
if (lib) {
DL_UNLOAD(handle);
Expand All @@ -3478,9 +3478,9 @@ static zend_ffi *zend_ffi_load(const char *filename, bool preload) /* {{{ */
zend_string_release(mangled_name);
if (!addr) {
if (preload) {
zend_error(E_WARNING, "failed pre-loading '%s', cannot resolve C function '%s'", filename, ZSTR_VAL(name));
zend_error(E_WARNING, "failed pre-loading '%s', cannot resolve C function '%pS'", filename, name);
} else {
zend_throw_error(zend_ffi_exception_ce, "Failed resolving C function '%s'", ZSTR_VAL(name));
zend_throw_error(zend_ffi_exception_ce, "Failed resolving C function '%pS'", name);
}
if (lib) {
DL_UNLOAD(handle);
Expand All @@ -3501,7 +3501,7 @@ static zend_ffi *zend_ffi_load(const char *filename, bool preload) /* {{{ */
zend_ffi_type_dtor(type);
}
} else {
zend_error(E_WARNING, "FFI: failed pre-loading '%s', redefinition of '%s'", filename, ZSTR_VAL(name));
zend_error(E_WARNING, "FFI: failed pre-loading '%s', redefinition of '%pS'", filename, name);
if (lib) {
DL_UNLOAD(handle);
}
Expand All @@ -3526,7 +3526,7 @@ static zend_ffi *zend_ffi_load(const char *filename, bool preload) /* {{{ */
zend_ffi_type_dtor(type);
}
} else {
zend_error(E_WARNING, "FFI: failed pre-loading '%s', redefinition of '%s %s'", filename, zend_ffi_tag_kind_name[tag->kind], ZSTR_VAL(name));
zend_error(E_WARNING, "FFI: failed pre-loading '%s', redefinition of '%s %pS'", filename, zend_ffi_tag_kind_name[tag->kind], name);
if (lib) {
DL_UNLOAD(handle);
}
Expand Down Expand Up @@ -3658,7 +3658,7 @@ ZEND_METHOD(FFI, scope) /* {{{ */
}

if (!scope) {
zend_throw_error(zend_ffi_exception_ce, "Failed loading scope '%s'", ZSTR_VAL(scope_name));
zend_throw_error(zend_ffi_exception_ce, "Failed loading scope '%pS'", scope_name);
RETURN_THROWS();
}

Expand All @@ -3684,18 +3684,17 @@ void zend_ffi_cleanup_dcl(zend_ffi_dcl *dcl) /* {{{ */
static void zend_ffi_throw_parser_error(const char *format, ...) /* {{{ */
{
va_list va;
char *message = NULL;

va_start(va, format);
zend_vspprintf(&message, 0, format, va);
zend_string *message = zend_vstrpprintf(0, format, va);

if (EG(current_execute_data)) {
zend_throw_exception(zend_ffi_parser_exception_ce, message, 0);
zend_throw_exception_ex(zend_ffi_parser_exception_ce, 0, "%pS", message);
} else {
zend_error(E_WARNING, "FFI Parser: %s", message);
zend_error(E_WARNING, "FFI Parser: %pS", message);
}

efree(message);
zend_string_release(message);
va_end(va);
}
/* }}} */
Expand All @@ -3720,11 +3719,11 @@ static zend_result zend_ffi_validate_incomplete_type(const zend_ffi_type *type,
ZEND_HASH_MAP_FOREACH_STR_KEY_PTR(FFI_G(tags), key, tag) {
if (ZEND_FFI_TYPE(tag->type) == type) {
if (type->kind == ZEND_FFI_TYPE_ENUM) {
zend_ffi_throw_parser_error("Incomplete enum \"%s\" at line %d", ZSTR_VAL(key), FFI_G(line));
zend_ffi_throw_parser_error("Incomplete enum \"%pS\" at line %d", key, FFI_G(line));
} else if (type->attr & ZEND_FFI_ATTR_UNION) {
zend_ffi_throw_parser_error("Incomplete union \"%s\" at line %d", ZSTR_VAL(key), FFI_G(line));
zend_ffi_throw_parser_error("Incomplete union \"%pS\" at line %d", key, FFI_G(line));
} else {
zend_ffi_throw_parser_error("Incomplete struct \"%s\" at line %d", ZSTR_VAL(key), FFI_G(line));
zend_ffi_throw_parser_error("Incomplete struct \"%pS\" at line %d", key, FFI_G(line));
}
return FAILURE;
}
Expand All @@ -3736,7 +3735,7 @@ static zend_result zend_ffi_validate_incomplete_type(const zend_ffi_type *type,

ZEND_HASH_MAP_FOREACH_STR_KEY_PTR(FFI_G(symbols), key, sym) {
if (type == ZEND_FFI_TYPE(sym->type)) {
zend_ffi_throw_parser_error("Incomplete C type %s at line %d", ZSTR_VAL(key), FFI_G(line));
zend_ffi_throw_parser_error("Incomplete C type %pS at line %d", key, FFI_G(line));
return FAILURE;
}
} ZEND_HASH_FOREACH_END();
Expand Down Expand Up @@ -5123,14 +5122,14 @@ static char *zend_ffi_parse_directives(const char *filename, char *code_pos, cha

static ZEND_COLD zend_function *zend_fake_get_constructor(zend_object *object) /* {{{ */
{
zend_throw_error(NULL, "Instantiation of %s is not allowed", ZSTR_VAL(object->ce->name));
zend_throw_error(NULL, "Instantiation of %pS is not allowed", object->ce->name);
return NULL;
}
/* }}} */

static ZEND_COLD zend_never_inline void zend_bad_array_access(zend_class_entry *ce) /* {{{ */
{
zend_throw_error(NULL, "Cannot use object of type %s as array", ZSTR_VAL(ce->name));
zend_throw_error(NULL, "Cannot use object of type %pS as array", ce->name);
}
/* }}} */

Expand Down Expand Up @@ -5162,7 +5161,7 @@ static ZEND_COLD void zend_fake_unset_dimension(zend_object *obj, zval *offset)

static ZEND_COLD zend_never_inline void zend_bad_property_access(zend_class_entry *ce) /* {{{ */
{
zend_throw_error(NULL, "Cannot access property of object of type %s", ZSTR_VAL(ce->name));
zend_throw_error(NULL, "Cannot access property of object of type %pS", ce->name);
}
/* }}} */

Expand Down Expand Up @@ -5202,7 +5201,7 @@ static zval *zend_fake_get_property_ptr_ptr(zend_object *obj, zend_string *membe
static ZEND_COLD zend_function *zend_fake_get_method(zend_object **obj_ptr, zend_string *method_name, const zval *key) /* {{{ */
{
zend_class_entry *ce = (*obj_ptr)->ce;
zend_throw_error(NULL, "Object of type %s does not support method calls", ZSTR_VAL(ce->name));
zend_throw_error(NULL, "Object of type %pS does not support method calls", ce->name);
return NULL;
}
/* }}} */
Expand Down Expand Up @@ -5762,18 +5761,17 @@ ZEND_GET_MODULE(ffi)
void zend_ffi_parser_error(const char *format, ...) /* {{{ */
{
va_list va;
char *message = NULL;

va_start(va, format);
zend_vspprintf(&message, 0, format, va);
zend_string *message = zend_vstrpprintf(0, format, va);

if (EG(current_execute_data)) {
zend_throw_exception(zend_ffi_parser_exception_ce, message, 0);
zend_throw_exception_ex(zend_ffi_parser_exception_ce, 0, "%pS", message);
} else {
zend_error(E_WARNING, "FFI Parser: %s", message);
zend_error(E_WARNING, "FFI Parser: %pS", message);
}

efree(message);
zend_string_release(message);
va_end(va);

LONGJMP(FFI_G(bailout), FAILURE);
Expand Down Expand Up @@ -6228,7 +6226,7 @@ void zend_ffi_add_anonymous_field(zend_ffi_dcl *struct_dcl, zend_ffi_dcl *field_
if (!zend_hash_add_ptr(&struct_type->record.fields, key, new_field)) {
zend_ffi_type_dtor(new_field->type);
pefree(new_field, FFI_G(persistent));
zend_ffi_parser_error("Duplicate field name \"%s\" at line %d", ZSTR_VAL(key), FFI_G(line));
zend_ffi_parser_error("Duplicate field name \"%pS\" at line %d", key, FFI_G(line));
return;
}
} else {
Expand Down
Loading