Coverage Report

Created: 2026-06-02 06:37

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/php-src/ext/standard/incomplete_class.c
Line
Count
Source
1
/*
2
   +----------------------------------------------------------------------+
3
   | Copyright © The PHP Group and Contributors.                          |
4
   +----------------------------------------------------------------------+
5
   | This source file is subject to the Modified BSD License that is      |
6
   | bundled with this package in the file LICENSE, and is available      |
7
   | through the World Wide Web at <https://www.php.net/license/>.        |
8
   |                                                                      |
9
   | SPDX-License-Identifier: BSD-3-Clause                                |
10
   +----------------------------------------------------------------------+
11
   | Author:  Sascha Schumann <sascha@schumann.cx>                        |
12
   +----------------------------------------------------------------------+
13
*/
14
15
#include "php.h"
16
#include "basic_functions.h"
17
#include "php_incomplete_class.h"
18
19
#define INCOMPLETE_CLASS_MSG \
20
0
    "The script tried to %s on an incomplete object. " \
21
0
    "Please ensure that the class definition \"%s\" of the object " \
22
0
    "you are trying to operate on was loaded _before_ " \
23
0
    "unserialize() gets called or provide an autoloader " \
24
0
    "to load the class definition"
25
26
PHPAPI zend_class_entry *php_ce_incomplete_class;
27
static zend_object_handlers php_incomplete_object_handlers;
28
29
static void incomplete_class_message(zend_object *object)
30
0
{
31
0
  zend_string *class_name = php_lookup_class_name(object);
32
0
  php_error_docref(NULL, E_WARNING, INCOMPLETE_CLASS_MSG,
33
0
    "access a property", class_name ? ZSTR_VAL(class_name) : "unknown");
34
0
  if (class_name) {
35
0
    zend_string_release_ex(class_name, 0);
36
0
  }
37
0
}
38
39
static void throw_incomplete_class_error(zend_object *object, const char *what)
40
0
{
41
0
  zend_string *class_name = php_lookup_class_name(object);
42
0
  zend_throw_error(NULL, INCOMPLETE_CLASS_MSG,
43
0
    what, class_name ? ZSTR_VAL(class_name) : "unknown");
44
0
  if (class_name) {
45
0
    zend_string_release_ex(class_name, 0);
46
0
  }
47
0
}
48
49
static zval *incomplete_class_get_property(zend_object *object, zend_string *member, int type, void **cache_slot, zval *rv) /* {{{ */
50
0
{
51
0
  incomplete_class_message(object);
52
53
0
  if (type == BP_VAR_W || type == BP_VAR_RW) {
54
0
    ZVAL_ERROR(rv);
55
0
    return rv;
56
0
  } else {
57
0
    return &EG(uninitialized_zval);
58
0
  }
59
0
}
60
/* }}} */
61
62
static zval *incomplete_class_write_property(zend_object *object, zend_string *member, zval *value, void **cache_slot) /* {{{ */
63
0
{
64
0
  throw_incomplete_class_error(object, "modify a property");
65
0
  return value;
66
0
}
67
/* }}} */
68
69
static zval *incomplete_class_get_property_ptr_ptr(zend_object *object, zend_string *member, int type, void **cache_slot) /* {{{ */
70
0
{
71
0
  throw_incomplete_class_error(object, "modify a property");
72
0
  return &EG(error_zval);
73
0
}
74
/* }}} */
75
76
static void incomplete_class_unset_property(zend_object *object, zend_string *member, void **cache_slot) /* {{{ */
77
0
{
78
0
  throw_incomplete_class_error(object, "modify a property");
79
0
}
80
/* }}} */
81
82
static int incomplete_class_has_property(zend_object *object, zend_string *member, int check_empty, void **cache_slot) /* {{{ */
83
0
{
84
0
  incomplete_class_message(object);
85
0
  return 0;
86
0
}
87
/* }}} */
88
89
static zend_function *incomplete_class_get_method(zend_object **object, zend_string *method, const zval *key) /* {{{ */
90
0
{
91
0
  throw_incomplete_class_error(*object, "call a method");
92
0
  return NULL;
93
0
}
94
/* }}} */
95
96
/* {{{ php_create_incomplete_class */
97
static zend_object *php_create_incomplete_object(zend_class_entry *class_type)
98
106k
{
99
106k
  zend_object *object;
100
101
106k
  object = zend_objects_new( class_type);
102
106k
  object->handlers = &php_incomplete_object_handlers;
103
104
106k
  object_properties_init(object, class_type);
105
106
106k
  return object;
107
106k
}
108
109
PHPAPI void php_register_incomplete_class_handlers(void)
110
2
{
111
2
  memcpy(&php_incomplete_object_handlers, &std_object_handlers, sizeof(zend_object_handlers));
112
2
  php_incomplete_object_handlers.read_property = incomplete_class_get_property;
113
2
  php_incomplete_object_handlers.has_property = incomplete_class_has_property;
114
2
  php_incomplete_object_handlers.unset_property = incomplete_class_unset_property;
115
2
  php_incomplete_object_handlers.write_property = incomplete_class_write_property;
116
2
  php_incomplete_object_handlers.get_property_ptr_ptr = incomplete_class_get_property_ptr_ptr;
117
2
  php_incomplete_object_handlers.get_method = incomplete_class_get_method;
118
119
2
  php_ce_incomplete_class->create_object = php_create_incomplete_object;
120
2
}
121
/* }}} */
122
123
/* {{{ php_lookup_class_name */
124
PHPAPI zend_string *php_lookup_class_name(zend_object *object)
125
0
{
126
0
  if (object->properties) {
127
0
    zval *val = zend_hash_str_find(object->properties, MAGIC_MEMBER, sizeof(MAGIC_MEMBER)-1);
128
129
0
    if (val != NULL && Z_TYPE_P(val) == IS_STRING) {
130
0
      return zend_string_copy(Z_STR_P(val));
131
0
    }
132
0
  }
133
134
0
  return NULL;
135
0
}
136
/* }}} */
137
138
/* {{{ php_store_class_name */
139
PHPAPI void php_store_class_name(zval *object, zend_string *name)
140
106k
{
141
106k
  zval val;
142
143
106k
  ZVAL_STR_COPY(&val, name);
144
106k
  zend_hash_str_update(Z_OBJPROP_P(object), MAGIC_MEMBER, sizeof(MAGIC_MEMBER)-1, &val);
145
106k
}
146
/* }}} */