ShortURL icon indicating copy to clipboard operation
ShortURL copied to clipboard

for PHP: validate input and support integers up to PHP_INT_MAX

Open divinity76 opened this issue 8 years ago • 3 comments

use regular php division if safe (<=0x7FFFFFFF ), else use bcdiv if available (because it's faster), and finally fallback to an userland php division based on an algorithm from http://justinparrtech.com/JustinParr-Tech/an-algorithm-for-arbitrary-precision-integer-division

i can't guarantee that the algorithm is flawless, but it passes all my own tests (without using bcmath), i can't get it to fail, i have tested a lot of large integers, including PHP_INT_MAX.

note, i have not extensively tested 32bit php support, due to the fact that i dont have a 32bit system. ( i have barely tested it using ideone.com , which uses 32bit php), but because i use normal PHP division if the $num <= 0x7FFFFFFF , i can't imagine it making any difference.

divinity76 avatar Oct 24 '16 12:10 divinity76

if you wonder about all the (int) casts, it's because the algorithm was SUPPOSED to be implemented using integer types, but PHP likes to automatically convert ints to float/double's during potential overflows or potential loss of precision, i had to make sure that didn't happen (and indeed, removing the int casts breaks the algorithm)

divinity76 avatar Oct 24 '16 13:10 divinity76

Thanks for this!

I'm not really sure about the custom implementation of the division. I'd rather add BC Math or GMP as a mandatory dependency, I guess.

Alternatively, we could add those as recommended dependencies and explain that they allow for the conversion of larger integers, together with your conditional check for the availability of BC Math or GMP.

ocram avatar Nov 01 '16 16:11 ocram

@ocram i made an alternative implementation which makes BCMath or GMP a mandatory dependency for integers above 0x7FFFFFFF , see https://github.com/delight-im/ShortURL/pull/12

divinity76 avatar Nov 01 '16 18:11 divinity76