Skip to content
This repository has been archived by the owner on Apr 30, 2021. It is now read-only.

Simplify some code #44

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 8 additions & 15 deletions pythonfuzz/corpus.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ def mutate(self, res):
if len(res) == 0:
return None
pos = self._rand(len(res))
res[pos] = numpy.uint8(INTERESTING8[self._rand(len(INTERESTING8))])
res[pos] = numpy.uint8(random.choice(INTERESTING8))
return res


Expand All @@ -302,7 +302,7 @@ def mutate(self, res):
if len(res) < 2:
return None
pos = self._rand(len(res) - 1)
v = numpy.uint16(INTERESTING16[self._rand(len(INTERESTING16))])
v = numpy.uint16(random.choice(INTERESTING16))
if bool(random.getrandbits(1)):
v = struct.pack('>H', v)
else:
Expand All @@ -322,7 +322,7 @@ def mutate(self, res):
if len(res) < 4:
return None
pos = self._rand(len(res) - 3)
v = numpy.uint32(INTERESTING32[self._rand(len(INTERESTING32))])
v = numpy.uint32(random.choice(INTERESTING32))
if bool(random.getrandbits(1)):
v = struct.pack('>I', v)
else:
Expand All @@ -345,12 +345,12 @@ def mutate(self, res):
digits.append(k)
if len(digits) == 0:
return None
pos = self._rand(len(digits))
was = res[digits[pos]]
pos = random.choice(digits)
was = res[pos]
now = was
while was == now:
now = self._rand(10) + ord('0')
res[digits[pos]] = now
res[pos] = now
return res


Expand Down Expand Up @@ -452,12 +452,6 @@ def _add_file(self, path):
def length(self):
return len(self._inputs)

@staticmethod
def _rand(n):
if n == 1 or n == 0:
return 0
return random.randint(0, n-1)

# Exp2 generates n with probability 1/2^(n+1).
@staticmethod
def _rand_exp():
Expand Down Expand Up @@ -493,7 +487,7 @@ def generate_input(self):
self.put(zero_test_case)
return zero_test_case
else:
buf = self._inputs[self._rand(len(self._inputs))]
buf = random.choice(self._inputs)
return self.mutate(buf)

def mutate(self, buf):
Expand All @@ -506,8 +500,7 @@ def mutate(self, buf):
# We'll try up to 20 times, but if we don't find a
# suitable mutator after that, we'll just give up.
for n in range(20):
x = self._rand(len(self.mutators))
mutator = self.mutators[x]
mutator = random.choice(self.mutators)

#print("Mutate with {}".format(mutator.__class__.__name__))
newres = mutator.mutate(res)
Expand Down