qemu-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Qemu-devel] [PATCH V3 3/5] fuzz: Fuzzing functions for qcow2 images


From: Stefan Hajnoczi
Subject: Re: [Qemu-devel] [PATCH V3 3/5] fuzz: Fuzzing functions for qcow2 images
Date: Fri, 18 Jul 2014 10:26:56 +0100
User-agent: Mutt/1.5.23 (2014-03-12)

On Wed, Jul 16, 2014 at 11:49:37PM +0400, Maria Kustova wrote:
> +def string_validator(current, strings):
> +    """Return a random string value from the list not equal to the current.
> +
> +    This function is useful for selection from valid values except current 
> one.
> +    """
> +    val = random.choice(strings)
> +    if val == current:
> +        return string_validator(current, strings)
> +    else:
> +        return val

There is a pattern to these validator functions:

def validator(current, pick, choices):
    while True:
        val = pick(choices)
        if val != current:
            return val

int_validator is validator(_, random_from_intervals, _)
bit_validator is validator(_, random_bits, _)
string_validator is validator(_, random.choice, _)

The code duplication in int_validator, bit_validator, and
string_validator can be eliminated.

> +
> +
> +def selector(current, constraints, fmt=None):
> +    """Select one value from all defined by constraints
> +
> +    Each constraint produces one random value satisfying to it. The function
> +    randomly selects one value satisfying at least one constraint (depending 
> on
> +    constraints overlaps).
> +    """
> +    validate = {
> +        'bitmask': bit_validator,
> +        'string': string_validator
> +    }.get(fmt, int_validator)

Is the indirection through a string necessary?

def selector(current, constraints, fmt=int_validator):

Callers can pass bit_validator or string_validator if they want.  It
makes the code a little simpler and it stays readable since
string_validator's name tells you what it does.

Attachment: pgpT65SZ58s_T.pgp
Description: PGP signature


reply via email to

[Prev in Thread] Current Thread [Next in Thread]