Skip to content

random: Convert RANDOM_SEED() from a macro to a function #13575

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

Merged
merged 2 commits into from
Mar 4, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions ext/random/php_random.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,19 @@

PHPAPI double php_combined_lcg(void);

static inline zend_long GENERATE_SEED()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be lowercase, the general convention AFAIK is that uppercase means macro, lowercase means functions, especially for a private API

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changing the casing would result in an unnecessary API break. As the function is static inline there is no behavioral change compared to the macro. Specifically it will not result in any more naming collisions than the macro would, but it does improve type safety and readability somewhat. It also allows to more easily adjust the seed generation, which is terrible (luckily GENERATE_SEED() is effectively only used if the CSPRNG fails, which it never does).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah it's a static inline in a header.... yeah sure that's fine

{
zend_ulong pid;

# ifdef PHP_WIN32
# define GENERATE_SEED() (((zend_long) ((zend_ulong) time(NULL) * (zend_ulong) GetCurrentProcessId())) ^ ((zend_long) (1000000.0 * php_combined_lcg())))
pid = (zend_ulong) GetCurrentProcessId();
# else
# define GENERATE_SEED() (((zend_long) ((zend_ulong) time(NULL) * (zend_ulong) getpid())) ^ ((zend_long) (1000000.0 * php_combined_lcg())))
pid = (zend_ulong) getpid();
# endif

return (((zend_long) ((zend_ulong) time(NULL) * pid)) ^ ((zend_long) (1000000.0 * php_combined_lcg())));
}

# define PHP_MT_RAND_MAX ((zend_long) (0x7FFFFFFF)) /* (1<<31) - 1 */

# define MT_RAND_MT19937 0
Expand Down