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