xref: /PHP-7.1/README.PARAMETER_PARSING_API (revision 7f6387b5)
1New parameter parsing functions
2===============================
3
4It should be easier to parse input parameters to an extension function.
5Hence, borrowing from Python's example, there are now a set of functions
6that given the string of type specifiers, can parse the input parameters
7and store the results in the user specified variables. This avoids most
8of the IS_* checks and convert_to_* conversions. The functions also
9check for the appropriate number of parameters, and try to output
10meaningful error messages.
11
12
13Prototypes
14----------
15/* Implemented. */
16int zend_parse_parameters(int num_args, char *type_spec, ...);
17int zend_parse_parameters_ex(int flags, int num_args, char *type_spec, ...);
18
19The zend_parse_parameters() function takes the number of parameters
20passed to the extension function, the type specifier string, and the
21list of pointers to variables to store the results in. The _ex() version
22also takes 'flags' argument -- current only ZEND_PARSE_PARAMS_QUIET can
23be used as 'flags' to specify that the function should operate quietly
24and not output any error messages.
25
26Both functions return SUCCESS or FAILURE depending on the result.
27
28The auto-conversions are performed as necessary. Arrays, objects, and
29resources cannot be auto-converted.
30
31PHP 5.5 includes a new function:
32
33int zend_parse_parameter(int flags, int arg_num, zval **arg, const char *spec, ...);
34
35This function behaves like zend_parse_parameters_ex() except that instead of
36reading the arguments from the stack, it receives a single zval to convert
37(passed with double indirection). The passed zval may be changed in place as
38part of the conversion process.
39
40See also https://wiki.php.net/rfc/zpp_improv#expose_zend_parse_arg_as_zend_parse_parameter
41
42
43Type specifiers
44---------------
45 The following list shows the type specifier, its meaning and the parameter
46 types that need to be passed by address. All passed parameters are set
47 if the PHP parameter is non optional and untouched if optional and the
48 parameter is not present. The only exception is O where the zend_class_entry*
49 has to be provided on input and is used to verify the PHP parameter is an
50 instance of that class.
51
52 a  - array (zval*)
53 A  - array or object (zval*)
54 b  - boolean (zend_bool)
55 C  - class (zend_class_entry*)
56 d  - double (double)
57 f  - function or array containing php method call info (returned as
58      zend_fcall_info and zend_fcall_info_cache)
59 h  - array (returned as HashTable*)
60 H  - array or HASH_OF(object) (returned as HashTable*)
61 l  - long (zend_long)
62 L  - long, limits out-of-range numbers to LONG_MAX/LONG_MIN (zend_long, ZEND_LONG_MAX/ZEND_LONG_MIN)
63 o  - object of any type (zval*)
64 O  - object of specific type given by class entry (zval*, zend_class_entry)
65 p  - valid path (string without null bytes in the middle) and its length (char*, size_t)
66 P  - valid path (string without null bytes in the middle) as zend_string (zend_string*)
67 r  - resource (zval*)
68 s  - string (with possible null bytes) and its length (char*, size_t)
69 S  - string (with possible null bytes) as zend_string (zend_string*)
70 z  - the actual zval (zval*)
71 *  - variable arguments list (0 or more)
72 +  - variable arguments list (1 or more)
73
74 The following characters also have a meaning in the specifier string:
75    | - indicates that the remaining parameters are optional, they
76        should be initialized to default values by the extension since they
77        will not be touched by the parsing function if they are not
78        passed to it.
79    / - use SEPARATE_ZVAL_IF_NOT_REF() on the parameter it follows
80    ! - the parameter it follows can be of specified type or NULL. If NULL is
81		passed and the output for such type is a pointer, then the output
82		pointer is set to a native NULL pointer.
83		For 'b', 'l' and 'd', an extra argument of type zend_bool* must be
84		passed after the corresponding bool*, zend_long* or double* arguments,
85		respectively. A non-zero value will be written to the zend_bool if a
86		PHP NULL is passed.
87
88
89Note on 64bit compatibility
90---------------------------
91Please note that since version 7 PHP uses zend_long as integer type and
92zend_string with size_t as length, so make sure you pass zend_longs to "l"
93and size_t to strings length (i.e. for "s" you need to pass char * and size_t),
94not the other way round!
95
96Both mistakes might cause memory corruptions and segfaults:
971)
98  char *str;
99  long str_len; /* XXX THIS IS WRONG!! Use size_t instead. */
100  zend_parse_parameters(ZEND_NUM_ARGS(), "s", &str, &str_len)
101
1022)
103  int num; /* XXX THIS IS WRONG!! Use zend_long instead. */
104  zend_parse_parameters(ZEND_NUM_ARGS(), "l", &num)
105
106If you're in doubt, use check_parameters.php script to the parameters
107and their types (it can be found in ./scripts/dev/ directory of PHP sources):
108
109# php ./scripts/dev/check_parameters.php /path/to/your/sources/
110
111
112Examples
113--------
114/* Gets a long, a string and its length, and a zval */
115zend_long l;
116char *s;
117size_t s_len;
118zval *param;
119if (zend_parse_parameters(ZEND_NUM_ARGS(), "lsz",
120                          &l, &s, &s_len, &param) == FAILURE) {
121    return;
122}
123
124
125/* Gets an object of class specified by my_ce, and an optional double. */
126zval *obj;
127double d = 0.5;
128zend_class_entry *my_ce;
129if (zend_parse_parameters(ZEND_NUM_ARGS(), "O|d",
130                          &obj, my_ce, &d) == FAILURE) {
131    return;
132}
133
134
135/* Gets an object or null, and an array.
136   If null is passed for object, obj will be set to NULL. */
137zval *obj;
138zval *arr;
139if (zend_parse_parameters(ZEND_NUM_ARGS(), "o!a",
140                          &obj, &arr) == FAILURE) {
141    return;
142}
143
144
145/* Gets a separated array which can also be null. */
146zval *arr;
147if (zend_parse_parameters(ZEND_NUM_ARGS(), "a/!",
148                          &arr) == FAILURE) {
149    return;
150}
151
152/* Get either a set of 3 longs or a string. */
153zend_long l1, l2, l3;
154char *s;
155/*
156 * The function expects a pointer to a size_t in this case, not a long
157 * or any other type.  If you specify a type which is larger
158 * than a 'size_t', the upper bits might not be initialized
159 * properly, leading to random crashes on platforms like
160 * Tru64 or Linux/Alpha.
161 */
162size_t length;
163
164if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS(),
165                             "lll", &l1, &l2, &l3) == SUCCESS) {
166    /* manipulate longs */
167} else if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS(),
168                                    "s", &s, &length) == SUCCESS) {
169    /* manipulate string */
170} else {
171    /* output error */
172
173    return;
174}
175
176
177/* Function that accepts only varargs (0 or more) */
178
179int i, num_varargs;
180zval *varargs = NULL;
181
182
183if (zend_parse_parameters(ZEND_NUM_ARGS(), "*", &varargs, &num_varargs) == FAILURE) {
184    return;
185}
186
187for (i = 0; i < num_varargs; i++) {
188    /* do something with varargs[i] */
189}
190
191if (varargs) {
192    efree(varargs);
193}
194
195
196/* Function that accepts a string, followed by varargs (1 or more) */
197
198char *str;
199size_t str_len;
200int i, num_varargs;
201zval *varargs = NULL;
202
203if (zend_parse_parameters(ZEND_NUM_ARGS(), "s+", &str, &str_len, &varargs, &num_varargs) == FAILURE) {
204    return;
205}
206
207for (i = 0; i < num_varargs; i++) {
208    /* do something with varargs[i] */
209}
210
211/* Function that takes an array, followed by varargs, and ending with a long */
212zend_long num;
213zval *array;
214int i, num_varargs;
215zval *varargs = NULL;
216
217if (zend_parse_parameters(ZEND_NUM_ARGS(), "a*l", &array, &varargs, &num_varargs, &num) == FAILURE) {
218    return;
219}
220
221for (i = 0; i < num_varargs; i++) {
222    /* do something with varargs[i] */
223}
224