{"action":"created","issue":{"url":"https://api.github.com/repos/squizlabs/PHP_CodeSniffer/issues/1206","repository_url":"https://api.github.com/repos/squizlabs/PHP_CodeSniffer","labels_url":"https://api.github.com/repos/squizlabs/PHP_CodeSniffer/issues/1206/labels{/name}","comments_url":"https://api.github.com/repos/squizlabs/PHP_CodeSniffer/issues/1206/comments","events_url":"https://api.github.com/repos/squizlabs/PHP_CodeSniffer/issues/1206/events","html_url":"https://github.com/squizlabs/PHP_CodeSniffer/issues/1206","id":187045840,"number":1206,"title":"Possible infinite loop in findPrevious()/findNext()","user":{"login":"niconoe-","id":9560327,"avatar_url":"https://avatars.githubusercontent.com/u/9560327?v=3","gravatar_id":"","url":"https://api.github.com/users/niconoe-","html_url":"https://github.com/niconoe-","followers_url":"https://api.github.com/users/niconoe-/followers","following_url":"https://api.github.com/users/niconoe-/following{/other_user}","gists_url":"https://api.github.com/users/niconoe-/gists{/gist_id}","starred_url":"https://api.github.com/users/niconoe-/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/niconoe-/subscriptions","organizations_url":"https://api.github.com/users/niconoe-/orgs","repos_url":"https://api.github.com/users/niconoe-/repos","events_url":"https://api.github.com/users/niconoe-/events{/privacy}","received_events_url":"https://api.github.com/users/niconoe-/received_events","type":"User","site_admin":false},"labels":[{"id":144368661,"url":"https://api.github.com/repos/squizlabs/PHP_CodeSniffer/labels/Awaiting%20Feedback","name":"Awaiting Feedback","color":"207de5","default":false}],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":3,"created_at":"2016-11-03T12:26:20Z","updated_at":"2016-11-20T23:53:45Z","closed_at":"2016-11-14T14:04:06Z","body":"Hi developers,\r\n\r\nI'm using PHPCS since a while and I just encountered an issue with an infinite loop by using the method `\\PHP_CodeSniffer_File::findPrevious`. \r\n\r\nThis method loops over tokens positions from `$start` and rewind to `$end` in a `for` loop. But I experienced, (probably writing my own sniff) that when setting `$start` to a negative number and `$end` to a boolean, you trigger an infinite loop.\r\n\r\n`for ($i = $start; $i >= $end; $i--);`\r\n\r\nHere, a numerical comparison is required between `$i` and `$end`, but PHP does something that might be seen as unexpected behavior at first sight. As `$end` is a boolean, it transtypes `$i` as a boolean first for comparison. Then, the comparision is made with **integer values of those booleans**.\r\n\r\nThis means that if `$i = -8` and `$end = false`, the really comparison will be [-8 -> true -> 1] >= [false -> 0], which is actually `true` while you're expecting that `-8 >= 0` is `false`.\r\n\r\nAs you understand now easily, decrementing `$i` will change nothing about its true value when comparating, and so we're creating an infinite loop.\r\n\r\nTo fix this issue, you must transtype `$start` and `$end` to integers, so the comparison will not implicitly transtype `$i` in boolean just before comparing. The implicit transtyping will be done in integer, which is the type we want.\r\n\r\nThis problem also occurs on `\\PHP_CodeSniffer_File::findNext`, as the loop is reversed but the same.\r\n\r\nThank you for making PHP_CodeSniffer better every day!\r\nBest regards,\r\n"},"comment":{"url":"https://api.github.com/repos/squizlabs/PHP_CodeSniffer/issues/comments/261816201","html_url":"https://github.com/squizlabs/PHP_CodeSniffer/issues/1206#issuecomment-261816201","issue_url":"https://api.github.com/repos/squizlabs/PHP_CodeSniffer/issues/1206","id":261816201,"user":{"login":"gsherwood","id":185976,"avatar_url":"https://avatars.githubusercontent.com/u/185976?v=3","gravatar_id":"","url":"https://api.github.com/users/gsherwood","html_url":"https://github.com/gsherwood","followers_url":"https://api.github.com/users/gsherwood/followers","following_url":"https://api.github.com/users/gsherwood/following{/other_user}","gists_url":"https://api.github.com/users/gsherwood/gists{/gist_id}","starred_url":"https://api.github.com/users/gsherwood/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/gsherwood/subscriptions","organizations_url":"https://api.github.com/users/gsherwood/orgs","repos_url":"https://api.github.com/users/gsherwood/repos","events_url":"https://api.github.com/users/gsherwood/events{/privacy}","received_events_url":"https://api.github.com/users/gsherwood/received_events","type":"User","site_admin":false},"created_at":"2016-11-20T23:53:45Z","updated_at":"2016-11-20T23:53:45Z","body":"Thanks for getting back to me. I understand your problem now, but I'll leave the methods unchanged as you have a different solution that wont impact other calls to the methods."}}
{"actor":{"display_login":"gsherwood"}}