xref: /PHP-5.3/scripts/dev/check_parameters.php (revision 2df73864)
1<?php
2/*
3  +----------------------------------------------------------------------+
4  | PHP Version 5                                                        |
5  +----------------------------------------------------------------------+
6  | Copyright (c) 1997-2007 The PHP Group                                |
7  +----------------------------------------------------------------------+
8  | This source file is subject to version 3.01 of the PHP license,      |
9  | that is bundled with this package in the file LICENSE, and is        |
10  | available through the world-wide-web at the following url:           |
11  | http://www.php.net/license/3_01.txt                                  |
12  | If you did not receive a copy of the PHP license and are unable to   |
13  | obtain it through the world-wide-web, please send a note to          |
14  | license@php.net so we can mail you a copy immediately.               |
15  +----------------------------------------------------------------------+
16  | Author: Nuno Lopes <nlopess@php.net>                                 |
17  +----------------------------------------------------------------------+
18*/
19
20/* $Id$ */
21
22
23define('REPORT_LEVEL', 2); // 0 reports less false-positives. up to level 5.
24define('VERSION', '5.2');  // minimum is 5.2
25define('PHPDIR', realpath(dirname(__FILE__) . '/../..'));
26
27
28// be sure you have enough memory and stack for PHP. pcre will push the limits!
29ini_set('pcre.backtrack_limit', 10000000);
30
31
32// ------------------------ end of config ----------------------------
33
34
35$API_params = array(
36	'a' => array('zval**'), // array as zval*
37	'b' => array('zend_bool*'), // boolean
38	'C' => array('zend_class_entry**'), // class
39	'd' => array('double*'), // double
40	'f' => array('zend_fcall_info*', 'zend_fcall_info_cache*'), // function
41	'h' => array('HashTable**'), // array as an HashTable*
42	'l' => array('long*'), // long
43	'o' => array('zval**'), //object
44	'O' => array('zval**', 'zend_class_entry*'), // object of given type
45	'r' => array('zval**'), // resource
46	's' => array('char**', 'int*'), // string
47	'z' => array('zval**'), // zval*
48	'Z' => array('zval***') // zval**
49);
50
51// specific to PHP >= 6
52if (version_compare(VERSION, '6', 'ge')) {
53	$API_params['S'] = $API_params['s']; // binary string
54	$API_params['t'] = array('zstr*', 'int*', 'zend_uchar*'); // text
55	$API_params['T'] = $API_params['t'];
56	$API_params['u'] = array('UChar**', 'int*'); // unicode
57	$API_params['U'] = $API_params['u'];
58}
59
60
61/** reports an error, according to its level */
62function error($str, $level = 0)
63{
64	global $current_file, $current_function, $line;
65
66	if ($level <= REPORT_LEVEL) {
67		if (strpos($current_file,PHPDIR) === 0) {
68			$filename = substr($current_file, strlen(PHPDIR)+1);
69		} else {
70			$filename = $current_file;
71		}
72		echo $filename , " [$line] $current_function : $str\n";
73	}
74}
75
76
77/** this updates the global var $line (for error reporting) */
78function update_lineno($offset)
79{
80	global $lines_offset, $line;
81
82	$left  = 0;
83	$right = $count = count($lines_offset)-1;
84
85	// a nice binary search :)
86	do {
87		$mid = intval(($left + $right)/2);
88		$val = $lines_offset[$mid];
89
90		if ($val < $offset) {
91			if (++$mid > $count || $lines_offset[$mid] > $offset) {
92				$line = $mid;
93				return;
94			} else {
95				$left = $mid;
96			}
97		} else if ($val > $offset) {
98			if ($lines_offset[--$mid] < $offset) {
99				$line = $mid+1;
100				return;
101			} else {
102				$right = $mid;
103			}
104		} else {
105			$line = $mid+1;
106			return;
107		}
108	} while (true);
109}
110
111
112/** parses the sources and fetches its vars name, type and if they are initialized or not */
113function get_vars($txt)
114{
115	$ret =  array();
116	preg_match_all('/((?:(?:unsigned|struct)\s+)?\w+)(?:\s*(\*+)\s+|\s+(\**))(\w+(?:\[\s*\w*\s*\])?)\s*(?:(=)[^,;]+)?((?:\s*,\s*\**\s*\w+(?:\[\s*\w*\s*\])?\s*(?:=[^,;]+)?)*)\s*;/S', $txt, $m, PREG_SET_ORDER);
117
118	foreach ($m as $x) {
119		// the first parameter is special
120		if (!in_array($x[1], array('else', 'endif', 'return'))) // hack to skip reserved words
121			$ret[$x[4]] = array($x[1] . $x[2] . $x[3], $x[5]);
122
123		// are there more vars?
124		if ($x[6]) {
125			preg_match_all('/(\**)\s*(\w+(?:\[\s*\w*\s*\])?)\s*(=?)/S', $x[6], $y, PREG_SET_ORDER);
126			foreach ($y as $z) {
127				$ret[$z[2]] = array($x[1] . $z[1], $z[3]);
128			}
129		}
130	}
131
132//	if ($GLOBALS['current_function'] == 'for_debugging') { print_r($m);print_r($ret); }
133	return $ret;
134}
135
136
137/** run diagnostic checks against one var. */
138function check_param($db, $idx, $exp, $optional)
139{
140	global $error_few_vars_given;
141
142	if ($idx >= count($db)) {
143		if (!$error_few_vars_given) {
144			error("too few variables passed to function");
145			$error_few_vars_given = true;
146		}
147		return;
148	} elseif ($db[$idx][0] === '**dummy**') {
149		return;
150	}
151
152	if ($db[$idx][1] != $exp) {
153		error("{$db[$idx][0]}: expected '$exp' but got '{$db[$idx][1]}' [".($idx+1).']');
154	}
155
156	if ($optional && !$db[$idx][2]) {
157		error("optional var not initialized: {$db[$idx][0]} [".($idx+1).']', 1);
158
159	} elseif (!$optional && $db[$idx][2]) {
160		error("not optional var is initialized: {$db[$idx][0]} [".($idx+1).']', 2);
161	}
162}
163
164
165/** fetch params passed to zend_parse_params*() */
166function get_params($vars, $str)
167{
168	$ret = array();
169	preg_match_all('/(?:\([^)]+\))?(&?)([\w>.()-]+(?:\[\w+\])?)\s*,?((?:\)*\s*=)?)/S', $str, $m, PREG_SET_ORDER);
170
171	foreach ($m as $x) {
172		$name = $x[2];
173
174		// little hack for last parameter
175		if (strpos($name, '(') === false) {
176			$name = rtrim($name, ')');
177		}
178
179		if (empty($vars[$name][0])) {
180			error("variable not found: '$name'", 3);
181			$ret[][] = '**dummy**';
182
183		} else {
184			$ret[] = array($name, $vars[$name][0] . ($x[1] ? '*' : ''), $vars[$name][1]);
185		}
186
187		// the end (yes, this is a little hack :P)
188		if ($x[3]) {
189			break;
190		}
191	}
192
193//	if ($GLOBALS['current_function'] == 'for_debugging') { var_dump($m); var_dump($ret); }
194	return $ret;
195}
196
197
198/** run tests on a function. the code is passed in $txt */
199function check_function($name, $txt, $offset)
200{
201	global $API_params;
202
203	if (preg_match_all('/zend_parse_parameters(?:_ex\s*\([^,]+,[^,]+|\s*\([^,]+),\s*"([^"]*)"\s*,\s*([^{;]*)/S', $txt, $matches, PREG_SET_ORDER | PREG_OFFSET_CAPTURE)) {
204
205		$GLOBALS['current_function'] = $name;
206
207		foreach ($matches as $m) {
208			$GLOBALS['error_few_vars_given'] = false;
209			update_lineno($offset + $m[2][1]);
210
211			$vars = get_vars(substr($txt, 0, $m[0][1])); // limit var search to current location
212			$params = get_params($vars, $m[2][0]);
213			$optional = $varargs = false;
214			$last_last_char = $last_char = '';
215			$j = -1;
216			$len = strlen($m[1][0]);
217
218			for ($i = 0; $i < $len; ++$i) {
219				switch($char = $m[1][0][$i]) {
220					// separator for optional parameters
221					case '|':
222						if ($optional) {
223							error("more than one optional separator at char #$i");
224						} else {
225							$optional = true;
226							if ($i == $len-1) {
227								error("unnecessary optional separator");
228							}
229						}
230					break;
231
232					// separate_zval_if_not_ref
233					case '/':
234						if (!in_array($last_char, array('r', 'z'))) {
235							error("the '/' specifier cannot be applied to '$last_char'");
236						}
237					break;
238
239					// nullable arguments
240					case '!':
241						if (!in_array($last_char, array('a', 'C', 'f', 'h', 'o', 'O', 'r', 's', 't', 'z', 'Z'))) {
242							error("the '!' specifier cannot be applied to '$last_char'");
243						}
244					break;
245
246					case '&':
247						if (version_compare(VERSION, '6', 'ge')) {
248							if ($last_char == 's' || ($last_last_char == 's' && $last_char == '!')) {
249								check_param($params, ++$j, 'UConverter*', $optional);
250
251							} else {
252								error("the '&' specifier cannot be applied to '$last_char'");
253							}
254						} else {
255							error("unknown char ('&') at column $i");
256						}
257					break;
258
259					case '+':
260					case '*':
261						if (version_compare(VERSION, '6', 'ge')) {
262							if ($varargs) {
263								error("A varargs specifier can only be used once. repeated char at column $i");
264							} else {
265								check_param($params, ++$j, 'zval****', $optional);
266								check_param($params, ++$j, 'int*', $optional);
267								$varargs = true;
268							}
269						} else {
270							error("unknown char ('$char') at column $i");
271						}
272					break;
273
274					default:
275						if (isset($API_params[$char])) {
276							foreach($API_params[$char] as $exp) {
277								check_param($params, ++$j, $exp, $optional);
278							}
279						} else {
280							error("unknown char ('$char') at column $i");
281						}
282				}
283
284				$last_last_char = $last_char;
285				$last_char = $char;
286			}
287		}
288	}
289}
290
291
292/** the main recursion function. splits files in functions and calls the other functions */
293function recurse($path)
294{
295	foreach (scandir($path) as $file) {
296		if ($file == '.' || $file == '..' || $file == 'CVS') continue;
297
298		$file = "$path/$file";
299		if (is_dir($file)) {
300			recurse($file);
301			continue;
302		}
303
304		// parse only .c and .cpp files
305		if (substr_compare($file, '.c', -2) && substr_compare($file, '.cpp', -4)) continue;
306
307		$txt = file_get_contents($file);
308		// remove comments (but preserve the number of lines)
309		$txt = preg_replace(array('@//.*@S', '@/\*.*\*/@SsUe'), array('', 'preg_replace("/[^\r\n]+/S", "", \'$0\')'), $txt);
310
311
312		$split = preg_split('/PHP_(?:NAMED_)?(?:FUNCTION|METHOD)\s*\((\w+(?:,\s*\w+)?)\)/S', $txt, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_OFFSET_CAPTURE);
313
314		if (count($split) < 2) continue; // no functions defined on this file
315		array_shift($split); // the first part isn't relevant
316
317
318		// generate the line offsets array
319		$j = 0;
320		$lines = preg_split("/(\r\n?|\n)/S", $txt, -1, PREG_SPLIT_DELIM_CAPTURE);
321		$lines_offset = array();
322
323		for ($i = 0; $i < count($lines); ++$i) {
324			$j += strlen($lines[$i]) + strlen(@$lines[++$i]);
325			$lines_offset[] = $j;
326		}
327
328		$GLOBALS['lines_offset'] = $lines_offset;
329		$GLOBALS['current_file'] = $file;
330
331
332		for ($i = 0; $i < count($split); $i+=2) {
333			// if the /* }}} */ comment is found use it to reduce false positives
334			// TODO: check the other indexes
335			list($f) = preg_split('@/\*\s*}}}\s*\*/@S', $split[$i+1][0]);
336			check_function(preg_replace('/\s*,\s*/S', '::', $split[$i][0]), $f, $split[$i][1]);
337		}
338	}
339}
340
341$dirs = array();
342
343if (isset($argc) && $argc > 1) {
344	if ($argv[1] == '-h' || $argv[1] == '-help' || $argv[1] == '--help') {
345		echo <<<HELP
346Synopsis:
347    php check_parameters.php [directories]
348
349HELP;
350		exit(0);
351	}
352	for ($i = 1; $i < $argc; $i++) {
353		$dirs[] = $argv[$i];
354	}
355} else {
356	$dirs[] = PHPDIR;
357}
358
359foreach($dirs as $dir) {
360	if (is_dir($dir)) {
361		if (!is_readable($dir)) {
362			echo "ERROR: directory '", $dir ,"' is not readable\n";
363			exit(1);
364		}
365	} else {
366		echo "ERROR: bogus directory '", $dir ,"'\n";
367		exit(1);
368	}
369}
370
371foreach ($dirs as $dir) {
372	recurse(realpath($dir));
373}
374