You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

381 lines
11 KiB

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