Skip to content

Commit 38e553e

Browse files
Lung-Alexandranielsdos
authored andcommitted
Fix GH-18082: Memory leaks in fuzzer SAPI error paths
Closes GH-18081.
1 parent 005c7b5 commit 38e553e

File tree

5 files changed

+22
-17
lines changed

5 files changed

+22
-17
lines changed

NEWS

+4
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ PHP NEWS
2525
- Embed:
2626
. Fixed bug GH-8533 (Unable to link dynamic libphp on Mac). (Kévin Dunglas)
2727

28+
- Fuzzer:
29+
. Fixed bug GH-18081 (Memory leaks in error paths of fuzzer SAPI).
30+
(Lung-Alexandra)
31+
2832
- Mbstring:
2933
. Fixed bug GH-17989 (mb_output_handler crash with unset
3034
http_output_conv_mimetypes). (nielsdos)

sapi/fuzzer/fuzzer-json.c

+5-6
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
+----------------------------------------------------------------------+
1616
*/
1717

18-
19-
2018
#include "fuzzer.h"
2119

2220
#include "Zend/zend.h"
@@ -31,14 +29,15 @@
3129
#include "ext/json/php_json_parser.h"
3230

3331
int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
34-
char *data = malloc(Size+1);
35-
memcpy(data, Data, Size);
36-
data[Size] = '\0';
3732

38-
if (fuzzer_request_startup() == FAILURE) {
33+
if (fuzzer_request_startup() == FAILURE){
3934
return 0;
4035
}
4136

37+
char *data = malloc(Size + 1);
38+
memcpy(data, Data, Size);
39+
data[Size] = '\0';
40+
4241
for (int option = 0; option <=1; ++option) {
4342
zval result;
4443
php_json_parser parser;

sapi/fuzzer/fuzzer-mbregex.c

+5-4
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,16 @@
3030

3131
int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
3232
#ifdef HAVE_MBREGEX
33-
char *args[2];
34-
char *data = malloc(Size+1);
35-
memcpy(data, Data, Size);
36-
data[Size] = '\0';
3733

3834
if (fuzzer_request_startup() == FAILURE) {
3935
return 0;
4036
}
4137

38+
char *args[2];
39+
char *data = malloc(Size+1);
40+
memcpy(data, Data, Size);
41+
data[Size] = '\0';
42+
4243
fuzzer_setup_dummy_frame();
4344

4445
args[0] = data;

sapi/fuzzer/fuzzer-unserialize.c

+4-3
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,15 @@
3030
#include "ext/standard/php_var.h"
3131

3232
int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
33-
unsigned char *orig_data = malloc(Size+1);
34-
memcpy(orig_data, Data, Size);
35-
orig_data[Size] = '\0';
3633

3734
if (fuzzer_request_startup() == FAILURE) {
3835
return 0;
3936
}
4037

38+
unsigned char *orig_data = malloc(Size+1);
39+
memcpy(orig_data, Data, Size);
40+
orig_data[Size] = '\0';
41+
4142
fuzzer_setup_dummy_frame();
4243

4344
{

sapi/fuzzer/fuzzer-unserializehash.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,15 @@ int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t FullSize) {
3434
}
3535
++Start;
3636

37+
if (fuzzer_request_startup() == FAILURE) {
38+
return 0;
39+
}
40+
3741
size_t Size = (Data + FullSize) - Start;
3842
unsigned char *orig_data = malloc(Size+1);
3943
memcpy(orig_data, Start, Size);
4044
orig_data[Size] = '\0';
4145

42-
if (fuzzer_request_startup() == FAILURE) {
43-
return 0;
44-
}
45-
4646
fuzzer_setup_dummy_frame();
4747

4848
{

0 commit comments

Comments
 (0)