slowbro/phpircd

Coding error (invalid strpos check)

Closed this issue · 1 comments

Example snippet: strpos($data, "\n") >= 0

The specs for strpos say that it returns an index i, 0 <= i < len($data), or if the search string does not exist, it returns false.

In PHP (and most languages), false == 0.

x >= 0 is short hand for (x > 0 || x == 0)

So, if x is false:

false == 0 evaluates to true.

So basically, strpos >= 0 will always be true whether or not the string exists or not.

The valid method of checking the return of strpos is:

if(strpos(...) === false) {
// does not exist
}

Or:

if(false !== ($idx = strpos(...))) {
// exists, and $idx contains starting index
}

Removed control block in ad6a79d- it was un-needed and accidentally got kept from an old version. Thanks for the pointers :)