Skip to content

Commit 3c5fcac

Browse files
committed
Updated to use zend_string but getting memory leaks. Doh!
1 parent 26e4ed2 commit 3c5fcac

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

ext/standard/random.c

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ static void php_bin_to_hex(char *old, const zend_long old_len, char *hex)
4646
}
4747

4848
// Copy/pasted from mcrypt.c
49-
static int php_random_bytes(char *bytes, zend_long size)
49+
static int php_random_bytes(zend_string *bytes, zend_long size)
5050
{
5151
int n = 0;
5252

@@ -90,7 +90,7 @@ Return an arbitrary length of pseudo-random bytes as binary string */
9090
PHP_FUNCTION(random_bytes)
9191
{
9292
zend_long size;
93-
char *bytes;
93+
zend_string *bytes;
9494

9595
if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &size) == FAILURE) {
9696
return;
@@ -101,16 +101,16 @@ PHP_FUNCTION(random_bytes)
101101
RETURN_FALSE;
102102
}
103103

104-
bytes = ecalloc(size + 1, 1);
104+
bytes = zend_string_alloc(size + 1, 0);
105105

106106
if (php_random_bytes(bytes, size) == FAILURE) {
107-
efree(bytes);
107+
zend_string_release(bytes);
108108
return;
109109
}
110110

111111
RETVAL_STRINGL(bytes, size);
112112

113-
efree(bytes);
113+
zend_string_release(bytes);
114114
}
115115
/* }}} */
116116

@@ -119,8 +119,8 @@ Return an arbitrary length of pseudo-random bytes as hexadecimal string */
119119
PHP_FUNCTION(random_hex)
120120
{
121121
zend_long size;
122-
char *bytes;
123-
char *hex;
122+
zend_string *bytes;
123+
zend_string *hex;
124124

125125
if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &size) == FAILURE) {
126126
return;
@@ -132,21 +132,21 @@ PHP_FUNCTION(random_hex)
132132
}
133133

134134
// @todo should we half the size for hex? How for odd num of chars?
135-
bytes = ecalloc(size + 1, 1);
135+
bytes = zend_string_alloc(size + 1, 0);
136136

137137
if (php_random_bytes(bytes, size) == FAILURE) {
138-
efree(bytes);
138+
zend_string_release(bytes);
139139
return;
140140
}
141141

142142
int hex_size = size * 2;
143-
hex = ecalloc(hex_size + 1, 1);
143+
hex = zend_string_alloc(hex_size + 1, 0);
144144
php_bin_to_hex(bytes, hex_size, hex);
145145

146146
RETVAL_STRINGL(hex, hex_size);
147147

148-
efree(bytes);
149-
efree(hex);
148+
zend_string_release(bytes);
149+
zend_string_release(hex);
150150
}
151151
/* }}} */
152152

0 commit comments

Comments
 (0)