xref: /PHP-8.2/sapi/fuzzer/fuzzer-mbstring.c (revision 49202116)
1 /*
2    +----------------------------------------------------------------------+
3    | Copyright (c) The PHP Group                                          |
4    +----------------------------------------------------------------------+
5    | This source file is subject to version 3.01 of the PHP license,      |
6    | that is bundled with this package in the file LICENSE, and is        |
7    | available through the world-wide-web at the following url:           |
8    | https://www.php.net/license/3_01.txt                                 |
9    | If you did not receive a copy of the PHP license and are unable to   |
10    | obtain it through the world-wide-web, please send a note to          |
11    | license@php.net so we can mail you a copy immediately.               |
12    +----------------------------------------------------------------------+
13    | Authors: Stanislav Malyshev <stas@php.net>                           |
14    +----------------------------------------------------------------------+
15  */
16 
17 
18 #include "zend.h"
19 #include "fuzzer.h"
20 #include "fuzzer-sapi.h"
21 #include "ext/mbstring/mbstring.h"
22 
LLVMFuzzerTestOneInput(const uint8_t * Data,size_t Size)23 int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
24 	const uint8_t *Comma1 = memchr(Data, ',', Size);
25 	if (!Comma1) {
26 		return 0;
27 	}
28 
29 	size_t ToEncodingNameLen = Comma1 - Data;
30 	char *ToEncodingName = estrndup((char *) Data, ToEncodingNameLen);
31 	Data = Comma1 + 1;
32 	Size -= ToEncodingNameLen + 1;
33 
34 	const uint8_t *Comma2 = memchr(Data, ',', Size);
35 	if (!Comma2) {
36 		efree(ToEncodingName);
37 		return 0;
38 	}
39 
40 	size_t FromEncodingNameLen = Comma2 - Data;
41 	char *FromEncodingName = estrndup((char *) Data, FromEncodingNameLen);
42 	Data = Comma2 + 1;
43 	Size -= FromEncodingNameLen + 1;
44 
45 	const mbfl_encoding *ToEncoding = mbfl_name2encoding(ToEncodingName);
46 	const mbfl_encoding *FromEncoding = mbfl_name2encoding(FromEncodingName);
47 
48 	if (!ToEncoding || !FromEncoding || fuzzer_request_startup() == FAILURE) {
49 		efree(ToEncodingName);
50 		efree(FromEncodingName);
51 		return 0;
52 	}
53 
54 	zend_string *Result = php_mb_convert_encoding_ex((char *) Data, Size, ToEncoding, FromEncoding);
55 	zend_string_release(Result);
56 	efree(ToEncodingName);
57 	efree(FromEncodingName);
58 
59 	fuzzer_request_shutdown();
60 	return 0;
61 }
62 
LLVMFuzzerInitialize(int * argc,char *** argv)63 int LLVMFuzzerInitialize(int *argc, char ***argv) {
64 	fuzzer_init_php(NULL);
65 	return 0;
66 }
67