New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Bug: random_string() Implicit conversion from float 4.5 to int loses precision #6330
Comments
Thank you for reporting! It seems So we should throw an InvalidArgumentException? |
I see it differently. The problem is that the function random_bytes() expects an integer, but gets a float. So it would be right (In my opinion) to round up the number or cast it as int. |
@turbopixel You're correct in a way that the float to int conversion is the immediate problem which can be patched by simply rounding up the float or discarding the decimal thru casting to int. But, if you'll look at the design deeper, the On the other hand, another way is to just pass the |
@paulbalandan The secret will probably be the PHP function itself. In the example you can see that a length of 5 is specified and the return value is 10 characters long. This will probably be the reason why the originally specified length is divided by 2. https://www.php.net/manual/en/function.random-bytes.php#refsect1-function.random-bytes-examples Now, there are two options:
|
PHP Version
8.1
CodeIgniter4 Version
4.2.1
CodeIgniter4 Installation Method
Composer (using
codeigniter4/appstarter
)Which operating systems have you tested for this bug?
Linux Debian
Which server did you use?
fpm-fcgi
Database
MariaDB
What happened?
I'm using the codeigniter build in helper
helper('text');
and want to create a random string called the functionrandom_string("crypto", 9);
With an odd number I always get the error: Implicit conversion from float 4.5 to int loses precision. So the numbers 3, 5, 7, 9, 11 (...) are not usable. With even numbers I always get a random string.What function is called?
This case I call with
random_string("crypto", 9)
:Steps to Reproduce
Didn't work:
Works fine:
Expected Output
A random string like b3da1087d or something else.
Anything else?
Last two Backtrace rows:
Solution maybe
Cast the result to an integer:
The text was updated successfully, but these errors were encountered: