Skip to content

Commit

Permalink
Make parsing more strict
Browse files Browse the repository at this point in the history
  • Loading branch information
Howard McLauchlan committed Apr 13, 2018
1 parent 7c27c27 commit fb3c0a7
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion tools/inject.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
# 16-Mar-2018 Howard McLauchlan Created this.

import argparse
import re
from bcc import BPF


Expand Down Expand Up @@ -350,6 +351,7 @@ def _parse_frames(self):
frames = []
cur_frame = []
i = 0
last_frame_added = 0

while i < len(data):
# improper input
Expand All @@ -360,6 +362,10 @@ def _parse_frames(self):
count -= c == ')'
if not count:
if c == '\0' or (c == '=' and data[i + 1] == '>'):
# This block is closing a chunk. This means cur_frame must
# have something in it.
if not cur_frame:
raise Exception("Cannot parse spec, missing parens")
if len(cur_frame) == 2:
frame = tuple(cur_frame)
elif cur_frame[0][0] == '(':
Expand All @@ -373,7 +379,12 @@ def _parse_frames(self):
elif c == ')':
cur_frame.append(data[start:i + 1].strip())
start = i + 1
last_frame_added = start
i += 1

# We only permit spaces after the last frame
if self.spec[last_frame_added:].strip():
raise Exception("Invalid characters found after last frame");
# improper input
if count:
raise Exception("Check your parentheses")
Expand All @@ -389,7 +400,9 @@ def _parse_spec(self):
func, pred = f[0], f[1]

if not self._validate_predicate(pred):
raise Exception
raise Exception("Invalid predicate")
if not self._validate_identifier(func):
raise Exception("Invalid function identifier")
tup = (pred, absolute_order)

if func not in self.map:
Expand All @@ -405,6 +418,16 @@ def _parse_spec(self):

self.length = absolute_order

def _validate_identifier(self, func):
# We've already established paren balancing. We will only look for
# identifier validity here.
paren_index = func.find("(")
potential_id = func[:paren_index]
pattern = '[_a-zA-z][_a-zA-Z0-9]*$'
if re.match(pattern, potential_id):
return True
return False

def _validate_predicate(self, pred):

if len(pred) > 0 and pred[0] == "(":
Expand Down

0 comments on commit fb3c0a7

Please sign in to comment.