contrib/fuzz/bdiff.cc
changeset 38173 fa0ddd5e8fff
parent 35670 2b9e2415f5b5
child 43613 dbc39f028c9f
equal deleted inserted replaced
38172:24cc2969abae 38173:fa0ddd5e8fff
     4  * Copyright 2018, Google Inc.
     4  * Copyright 2018, Google Inc.
     5  *
     5  *
     6  * This software may be used and distributed according to the terms of
     6  * This software may be used and distributed according to the terms of
     7  * the GNU General Public License, incorporated herein by reference.
     7  * the GNU General Public License, incorporated herein by reference.
     8  */
     8  */
       
     9 #include <memory>
     9 #include <stdlib.h>
    10 #include <stdlib.h>
       
    11 
       
    12 #include "fuzzutil.h"
    10 
    13 
    11 extern "C" {
    14 extern "C" {
    12 #include "bdiff.h"
    15 #include "bdiff.h"
    13 
    16 
    14 int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size)
    17 int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size)
    15 {
    18 {
    16 	if (!Size) {
    19 	auto maybe_inputs = SplitInputs(Data, Size);
       
    20 	if (!maybe_inputs) {
    17 		return 0;
    21 		return 0;
    18 	}
    22 	}
    19 	// figure out a random point in [0, Size] to split our input.
    23 	auto inputs = std::move(maybe_inputs.value());
    20 	size_t split = Data[0] / 255.0 * Size;
       
    21 
       
    22 	// left input to diff is data[1:split]
       
    23 	const uint8_t *left = Data + 1;
       
    24 	// which has len split-1
       
    25 	size_t left_size = split - 1;
       
    26 	// right starts at the next byte after left ends
       
    27 	const uint8_t *right = left + left_size;
       
    28 	size_t right_size = Size - split;
       
    29 
    24 
    30 	struct bdiff_line *a, *b;
    25 	struct bdiff_line *a, *b;
    31 	int an = bdiff_splitlines((const char *)left, split - 1, &a);
    26 	int an = bdiff_splitlines(inputs.left.get(), inputs.left_size, &a);
    32 	int bn = bdiff_splitlines((const char *)right, right_size, &b);
    27 	int bn = bdiff_splitlines(inputs.right.get(), inputs.right_size, &b);
    33 	struct bdiff_hunk l;
    28 	struct bdiff_hunk l;
    34 	bdiff_diff(a, an, b, bn, &l);
    29 	bdiff_diff(a, an, b, bn, &l);
    35 	free(a);
    30 	free(a);
    36 	free(b);
    31 	free(b);
    37 	bdiff_freehunks(l.next);
    32 	bdiff_freehunks(l.next);