Skip to content

Commit 2483557

Browse files
committed
zend_random_bytes_insecure is infallible
1 parent 6d45619 commit 2483557

File tree

4 files changed

+12
-26
lines changed

4 files changed

+12
-26
lines changed

Zend/zend.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ ZEND_API zend_string *(*zend_resolve_path)(zend_string *filename);
9595
ZEND_API zend_result (*zend_post_startup_cb)(void) = NULL;
9696
ZEND_API void (*zend_post_shutdown_cb)(void) = NULL;
9797
ZEND_ATTRIBUTE_NONNULL ZEND_API zend_result (*zend_random_bytes)(void *bytes, size_t size, char *errstr, size_t errstr_size) = NULL;
98-
ZEND_ATTRIBUTE_NONNULL ZEND_API zend_result (*zend_random_bytes_insecure)(zend_utility_random_bytes_insecure_state *state, void *bytes, size_t size) = NULL;
98+
ZEND_ATTRIBUTE_NONNULL ZEND_API void (*zend_random_bytes_insecure)(zend_utility_random_bytes_insecure_state *state, void *bytes, size_t size) = NULL;
9999

100100
/* This callback must be signal handler safe! */
101101
void (*zend_on_timeout)(int seconds);

Zend/zend.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ typedef struct _zend_utility_functions {
254254
char *(*getenv_function)(const char *name, size_t name_len);
255255
zend_string *(*resolve_path_function)(zend_string *filename);
256256
zend_result (*random_bytes_function)(void *bytes, size_t size, char *errstr, size_t errstr_size);
257-
zend_result (*random_bytes_insecure_function)(zend_utility_random_bytes_insecure_state *state, void *bytes, size_t size);
257+
void (*random_bytes_insecure_function)(zend_utility_random_bytes_insecure_state *state, void *bytes, size_t size);
258258
} zend_utility_functions;
259259

260260
typedef struct _zend_utility_values {
@@ -353,7 +353,7 @@ extern ZEND_ATTRIBUTE_NONNULL ZEND_API zend_result (*zend_random_bytes)(
353353
/* Generate 'size' random bytes into 'bytes' with a general purpose PRNG (not
354354
* crypto safe). 'state' must be zeroed before the first call and can be reused.
355355
*/
356-
extern ZEND_ATTRIBUTE_NONNULL ZEND_API zend_result (*zend_random_bytes_insecure)(
356+
extern ZEND_ATTRIBUTE_NONNULL ZEND_API void (*zend_random_bytes_insecure)(
357357
zend_utility_random_bytes_insecure_state *state, void *bytes, size_t size);
358358

359359
/* These two callbacks are especially for opcache */

Zend/zend_alloc.c

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2043,15 +2043,15 @@ static void zend_mm_free_huge(zend_mm_heap *heap, void *ptr ZEND_FILE_LINE_DC ZE
20432043
/* Initialization */
20442044
/******************/
20452045

2046-
static zend_result zend_mm_refresh_key(zend_mm_heap *heap)
2046+
static void zend_mm_refresh_key(zend_mm_heap *heap)
20472047
{
2048-
return zend_random_bytes_insecure(&heap->rand_state, &heap->shadow_key, sizeof(heap->shadow_key));
2048+
zend_random_bytes_insecure(&heap->rand_state, &heap->shadow_key, sizeof(heap->shadow_key));
20492049
}
20502050

2051-
static zend_result zend_mm_init_key(zend_mm_heap *heap)
2051+
static void zend_mm_init_key(zend_mm_heap *heap)
20522052
{
20532053
memset(&heap->rand_state, 0, sizeof(heap->rand_state));
2054-
return zend_mm_refresh_key(heap);
2054+
zend_mm_refresh_key(heap);
20552055
}
20562056

20572057
static zend_mm_heap *zend_mm_init(void)
@@ -2090,12 +2090,7 @@ static zend_mm_heap *zend_mm_init(void)
20902090
heap->size = 0;
20912091
heap->peak = 0;
20922092
#endif
2093-
if (zend_mm_init_key(heap) != SUCCESS) {
2094-
#if ZEND_MM_ERROR
2095-
fprintf(stderr, "Can't initialize heap\n");
2096-
#endif
2097-
return NULL;
2098-
}
2093+
zend_mm_init_key(heap);
20992094
#if ZEND_MM_LIMIT
21002095
heap->limit = (size_t)Z_L(-1) >> 1;
21012096
heap->overflow = 0;
@@ -2544,14 +2539,10 @@ void zend_mm_shutdown(zend_mm_heap *heap, bool full, bool silent)
25442539

25452540
pid_t pid = getpid();
25462541
if (heap->pid != pid) {
2547-
if (zend_mm_init_key(heap) != SUCCESS) {
2548-
zend_mm_panic("Can't initialize heap");
2549-
}
2542+
zend_mm_init_key(heap);
25502543
heap->pid = pid;
25512544
} else {
2552-
if (zend_mm_refresh_key(heap) != SUCCESS) {
2553-
zend_mm_panic("Can't initialize heap");
2554-
}
2545+
zend_mm_refresh_key(heap);
25552546
}
25562547
}
25572548
}
@@ -3240,12 +3231,7 @@ ZEND_API zend_mm_heap *zend_mm_startup_ex(const zend_mm_handlers *handlers, void
32403231
heap->size = 0;
32413232
heap->peak = 0;
32423233
#endif
3243-
if (zend_mm_init_key(heap) != SUCCESS) {
3244-
#if ZEND_MM_ERROR
3245-
fprintf(stderr, "Can't initialize heap\n");
3246-
#endif
3247-
return NULL;
3248-
}
3234+
zend_mm_init_key(heap);
32493235
#if ZEND_MM_LIMIT
32503236
heap->limit = (size_t)Z_L(-1) >> 1;
32513237
heap->overflow = 0;

ext/random/php_random_zend_utils.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ typedef struct _php_random_bytes_insecure_state_for_zend {
2929

3030
ZEND_STATIC_ASSERT(sizeof(zend_utility_random_bytes_insecure_state) >= sizeof(php_random_bytes_insecure_state_for_zend), "");
3131

32-
ZEND_ATTRIBUTE_NONNULL PHPAPI zend_result php_random_bytes_insecure_for_zend(
32+
ZEND_ATTRIBUTE_NONNULL PHPAPI void php_random_bytes_insecure_for_zend(
3333
zend_utility_random_bytes_insecure_state *state, void *bytes, size_t size);
3434

3535
#endif /* PHP_RANDOM_ZEND_UTILS_H */

0 commit comments

Comments
 (0)