check_parameters.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. <?php
  2. /*
  3. +----------------------------------------------------------------------+
  4. | PHP Version 7 |
  5. +----------------------------------------------------------------------+
  6. | Copyright (c) 1997-2018 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. define('REPORT_LEVEL', 1); // 0 reports less false-positives. up to level 5.
  20. define('VERSION', '7.0'); // minimum is 7.0
  21. define('PHPDIR', realpath(dirname(__FILE__) . '/../..'));
  22. // be sure you have enough memory and stack for PHP. pcre will push the limits!
  23. ini_set('pcre.backtrack_limit', 10000000);
  24. // ------------------------ end of config ----------------------------
  25. $API_params = array(
  26. 'a' => array('zval**'), // array
  27. 'A' => array('zval**'), // array or object
  28. 'b' => array('zend_bool*'), // boolean
  29. 'd' => array('double*'), // double
  30. 'f' => array('zend_fcall_info*', 'zend_fcall_info_cache*'), // function
  31. 'h' => array('HashTable**'), // array as an HashTable*
  32. 'H' => array('HashTable**'), // array or HASH_OF(object)
  33. 'l' => array('zend_long*'), // long
  34. //TODO 'L' => array('zend_long*, '), // long
  35. 'o' => array('zval**'), //object
  36. 'O' => array('zval**', 'zend_class_entry*'), // object of given type
  37. 'P' => array('zend_string**'), // valid path
  38. 'r' => array('zval**'), // resource
  39. 'S' => array('zend_string**'), // string
  40. 'z' => array('zval**'), // zval*
  41. 'Z' => array('zval***') // zval**
  42. // 's', 'p', 'C' handled separately
  43. );
  44. /** reports an error, according to its level */
  45. function error($str, $level = 0)
  46. {
  47. global $current_file, $current_function, $line;
  48. if ($level <= REPORT_LEVEL) {
  49. if (strpos($current_file,PHPDIR) === 0) {
  50. $filename = substr($current_file, strlen(PHPDIR)+1);
  51. } else {
  52. $filename = $current_file;
  53. }
  54. echo $filename , " [$line] $current_function : $str\n";
  55. }
  56. }
  57. /** this updates the global var $line (for error reporting) */
  58. function update_lineno($offset)
  59. {
  60. global $lines_offset, $line;
  61. $left = 0;
  62. $right = $count = count($lines_offset)-1;
  63. // a nice binary search :)
  64. do {
  65. $mid = intval(($left + $right)/2);
  66. $val = $lines_offset[$mid];
  67. if ($val < $offset) {
  68. if (++$mid > $count || $lines_offset[$mid] > $offset) {
  69. $line = $mid;
  70. return;
  71. } else {
  72. $left = $mid;
  73. }
  74. } else if ($val > $offset) {
  75. if ($lines_offset[--$mid] < $offset) {
  76. $line = $mid+1;
  77. return;
  78. } else {
  79. $right = $mid;
  80. }
  81. } else {
  82. $line = $mid+1;
  83. return;
  84. }
  85. } while (true);
  86. }
  87. /** parses the sources and fetches its vars name, type and if they are initialized or not */
  88. function get_vars($txt)
  89. {
  90. $ret = array();
  91. 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);
  92. foreach ($m as $x) {
  93. // the first parameter is special
  94. if (!in_array($x[1], array('else', 'endif', 'return'))) // hack to skip reserved words
  95. $ret[$x[4]] = array($x[1] . $x[2] . $x[3], $x[5]);
  96. // are there more vars?
  97. if ($x[6]) {
  98. preg_match_all('/(\**)\s*(\w+(?:\[\s*\w*\s*\])?)\s*(=?)/S', $x[6], $y, PREG_SET_ORDER);
  99. foreach ($y as $z) {
  100. $ret[$z[2]] = array($x[1] . $z[1], $z[3]);
  101. }
  102. }
  103. }
  104. // if ($GLOBALS['current_function'] == 'for_debugging') { print_r($m);print_r($ret); }
  105. return $ret;
  106. }
  107. /** run diagnostic checks against one var. */
  108. function check_param($db, $idx, $exp, $optional, $allow_uninit = false)
  109. {
  110. global $error_few_vars_given;
  111. if ($idx >= count($db)) {
  112. if (!$error_few_vars_given) {
  113. error("too few variables passed to function");
  114. $error_few_vars_given = true;
  115. }
  116. return;
  117. } elseif ($db[$idx][0] === '**dummy**') {
  118. return;
  119. }
  120. if ($db[$idx][1] != $exp) {
  121. error("{$db[$idx][0]}: expected '$exp' but got '{$db[$idx][1]}' [".($idx+1).']');
  122. }
  123. if (!$optional && $db[$idx][2]) {
  124. error("not optional var is initialized: {$db[$idx][0]} [".($idx+1).']', 2);
  125. }
  126. if (!$allow_uninit && $optional && !$db[$idx][2]) {
  127. error("optional var not initialized: {$db[$idx][0]} [".($idx+1).']', 1);
  128. }
  129. }
  130. /** fetch params passed to zend_parse_params*() */
  131. function get_params($vars, $str)
  132. {
  133. $ret = array();
  134. preg_match_all('/(?:\([^)]+\))?(&?)([\w>.()-]+(?:\[\w+\])?)\s*,?((?:\)*\s*=)?)/S', $str, $m, PREG_SET_ORDER);
  135. foreach ($m as $x) {
  136. $name = $x[2];
  137. // little hack for last parameter
  138. if (strpos($name, '(') === false) {
  139. $name = rtrim($name, ')');
  140. }
  141. if (empty($vars[$name][0])) {
  142. error("variable not found: '$name'", 3);
  143. $ret[][] = '**dummy**';
  144. } else {
  145. $ret[] = array($name, $vars[$name][0] . ($x[1] ? '*' : ''), $vars[$name][1]);
  146. }
  147. // the end (yes, this is a little hack :P)
  148. if ($x[3]) {
  149. break;
  150. }
  151. }
  152. // if ($GLOBALS['current_function'] == 'for_debugging') { var_dump($m); var_dump($ret); }
  153. return $ret;
  154. }
  155. /** run tests on a function. the code is passed in $txt */
  156. function check_function($name, $txt, $offset)
  157. {
  158. global $API_params;
  159. $regex = '/
  160. (?: zend_parse_parameters(?:_throw)? \s*\([^,]+
  161. | zend_parse_(?:parameters_ex|method_parameters) \s*\([^,]+,[^,]+
  162. | zend_parse_method_parameters_ex \s*\([^,]+,[^,]+,[^,+]
  163. )
  164. ,\s*"([^"]*)"\s*
  165. ,\s*([^{;]*)
  166. /Sx';
  167. if (preg_match_all($regex, $txt, $matches, PREG_SET_ORDER | PREG_OFFSET_CAPTURE)) {
  168. $GLOBALS['current_function'] = $name;
  169. foreach ($matches as $m) {
  170. $GLOBALS['error_few_vars_given'] = false;
  171. update_lineno($offset + $m[2][1]);
  172. $vars = get_vars(substr($txt, 0, $m[0][1])); // limit var search to current location
  173. $params = get_params($vars, $m[2][0]);
  174. $optional = $varargs = false;
  175. $last_char = '';
  176. $j = -1;
  177. $spec = $m[1][0];
  178. $len = strlen($spec);
  179. for ($i = 0; $i < $len; ++$i) {
  180. $char = $spec[$i];
  181. switch ($char = $spec[$i]) {
  182. // separator for optional parameters
  183. case '|':
  184. if ($optional) {
  185. error("more than one optional separator at char #$i");
  186. } else {
  187. $optional = true;
  188. if ($i == $len-1) {
  189. error("unnecessary optional separator");
  190. }
  191. }
  192. break;
  193. // separate_zval_if_not_ref
  194. case '/':
  195. if (in_array($last_char, array('l', 'L', 'd', 'b'))) {
  196. error("the '/' specifier should not be applied to '$last_char'");
  197. }
  198. break;
  199. // nullable arguments
  200. case '!':
  201. if (in_array($last_char, array('l', 'L', 'd', 'b'))) {
  202. check_param($params, ++$j, 'zend_bool*', $optional);
  203. }
  204. break;
  205. // variadic arguments
  206. case '+':
  207. case '*':
  208. if ($varargs) {
  209. error("A varargs specifier can only be used once. repeated char at column $i");
  210. } else {
  211. check_param($params, ++$j, 'zval**', $optional);
  212. check_param($params, ++$j, 'int*', $optional);
  213. $varargs = true;
  214. }
  215. break;
  216. case 's':
  217. case 'p':
  218. check_param($params, ++$j, 'char**', $optional, $allow_uninit=true);
  219. check_param($params, ++$j, 'size_t*', $optional, $allow_uninit=true);
  220. if ($optional && !$params[$j-1][2] && !$params[$j][2]
  221. && $params[$j-1][0] !== '**dummy**' && $params[$j][0] !== '**dummy**') {
  222. error("one of optional vars {$params[$j-1][0]} or {$params[$j][0]} must be initialized", 1);
  223. }
  224. break;
  225. case 'C':
  226. // C must always be initialized, independently of whether it's optional
  227. check_param($params, ++$j, 'zend_class_entry**', false);
  228. break;
  229. default:
  230. if (!isset($API_params[$char])) {
  231. error("unknown char ('$char') at column $i");
  232. }
  233. // If an is_null flag is in use, only that flag is required to be
  234. // initialized
  235. $allow_uninit = $i+1 < $len && $spec[$i+1] === '!'
  236. && in_array($char, array('l', 'L', 'd', 'b'));
  237. foreach ($API_params[$char] as $exp) {
  238. check_param($params, ++$j, $exp, $optional, $allow_uninit);
  239. }
  240. }
  241. $last_char = $char;
  242. }
  243. }
  244. }
  245. }
  246. /** the main recursion function. splits files in functions and calls the other functions */
  247. function recurse($path)
  248. {
  249. foreach (scandir($path) as $file) {
  250. if ($file == '.' || $file == '..' || $file == 'CVS') continue;
  251. $file = "$path/$file";
  252. if (is_dir($file)) {
  253. recurse($file);
  254. continue;
  255. }
  256. // parse only .c and .cpp files
  257. if (substr_compare($file, '.c', -2) && substr_compare($file, '.cpp', -4)) continue;
  258. $txt = file_get_contents($file);
  259. // remove comments (but preserve the number of lines)
  260. $txt = preg_replace('@//.*@S', '', $txt);
  261. $txt = preg_replace_callback('@/\*.*\*/@SsU', function($matches) {
  262. return preg_replace("/[^\r\n]+/S", "", $matches[0]);
  263. }, $txt);
  264. $split = preg_split('/PHP_(?:NAMED_)?(?:FUNCTION|METHOD)\s*\((\w+(?:,\s*\w+)?)\)/S', $txt, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_OFFSET_CAPTURE);
  265. if (count($split) < 2) continue; // no functions defined on this file
  266. array_shift($split); // the first part isn't relevant
  267. // generate the line offsets array
  268. $j = 0;
  269. $lines = preg_split("/(\r\n?|\n)/S", $txt, -1, PREG_SPLIT_DELIM_CAPTURE);
  270. $lines_offset = array();
  271. for ($i = 0; $i < count($lines); ++$i) {
  272. $j += strlen($lines[$i]) + strlen(@$lines[++$i]);
  273. $lines_offset[] = $j;
  274. }
  275. $GLOBALS['lines_offset'] = $lines_offset;
  276. $GLOBALS['current_file'] = $file;
  277. for ($i = 0; $i < count($split); $i+=2) {
  278. // if the /* }}} */ comment is found use it to reduce false positives
  279. // TODO: check the other indexes
  280. list($f) = preg_split('@/\*\s*}}}\s*\*/@S', $split[$i+1][0]);
  281. check_function(preg_replace('/\s*,\s*/S', '::', $split[$i][0]), $f, $split[$i][1]);
  282. }
  283. }
  284. }
  285. $dirs = array();
  286. if (isset($argc) && $argc > 1) {
  287. if ($argv[1] == '-h' || $argv[1] == '-help' || $argv[1] == '--help') {
  288. echo <<<HELP
  289. Synopsis:
  290. php check_parameters.php [directories]
  291. HELP;
  292. exit(0);
  293. }
  294. for ($i = 1; $i < $argc; $i++) {
  295. $dirs[] = $argv[$i];
  296. }
  297. } else {
  298. $dirs[] = PHPDIR;
  299. }
  300. foreach($dirs as $dir) {
  301. if (is_dir($dir)) {
  302. if (!is_readable($dir)) {
  303. echo "ERROR: directory '", $dir ,"' is not readable\n";
  304. exit(1);
  305. }
  306. } else {
  307. echo "ERROR: bogus directory '", $dir ,"'\n";
  308. exit(1);
  309. }
  310. }
  311. foreach ($dirs as $dir) {
  312. recurse(realpath($dir));
  313. }