fuzzers/fuzzer_json: add simple fuzzing for our json code

This commit is contained in:
Kacper Michajłow 2024-11-02 04:23:05 +01:00
parent 207aa647a5
commit d78c35d360
2 changed files with 49 additions and 0 deletions

44
fuzzers/fuzzer_json.c Normal file
View File

@ -0,0 +1,44 @@
/*
* This file is part of mpv.
*
* mpv is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* mpv is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with mpv. If not, see <http://www.gnu.org/licenses/>.
*/
#include "common.h"
#include "misc/json.h"
#include "mpv_talloc.h"
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
{
void *tmp = talloc_new(NULL);
char *s = talloc_array_ptrtype(tmp, s, size + 1);
memcpy(s, data, size);
s[size] = '\0';
json_skip_whitespace(&s);
struct mpv_node res;
if (!json_parse(tmp, &res, &s, MAX_JSON_DEPTH)) {
char *d = talloc_strdup(tmp, "");
json_write(&d, &res);
d[0] = '\0';
json_write_pretty(&d, &res);
}
talloc_free(tmp);
return 0;
}

View File

@ -76,4 +76,9 @@ fuzzers += executable('fuzzer_options_parser', 'fuzzer_options_parser.c',
objects: libmpv.extract_all_objects(recursive: true), objects: libmpv.extract_all_objects(recursive: true),
dependencies: dependencies) dependencies: dependencies)
fuzzers += executable('fuzzer_json', 'fuzzer_json.c',
link_language: 'cpp', include_directories: incdir,
objects: libmpv.extract_all_objects(recursive: true),
dependencies: dependencies)
alias_target('fuzzers', fuzzers) alias_target('fuzzers', fuzzers)