Skip to content

Commit

Permalink
Fix std::vector out-of-bound index check.
Browse files Browse the repository at this point in the history
Add support for validate_arg_in prototype feature.
  • Loading branch information
ejulien committed Jun 21, 2018
1 parent 4f206d5 commit 643169a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
17 changes: 12 additions & 5 deletions gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -659,11 +659,18 @@ def _prepare_c_arg_self(self, conv, out_var, ctx='none', features=[]):
def _declare_c_arg(self, ctype, var):
return self.decl_var(ctype, var)

def _convert_c_arg(self, idx, conv, var, ctx='default'):
return conv.to_c_call(self.get_arg(idx, ctx), '&%s' % var)
def _convert_c_arg(self, idx, conv, var, ctx='default', features=[]):
out = conv.to_c_call(self.get_arg(idx, ctx), '&%s' % var)

def _prepare_c_arg(self, idx, conv, var, ctx='default'):
return self._declare_c_arg(conv.arg_storage_ctype, var) + self._convert_c_arg(idx, conv, var, ctx)
if 'validate_arg_in' in features:
validator = features['validate_arg_in'][idx]
if validator is not None:
out += validator(self, var, ctx)

return out

def _prepare_c_arg(self, idx, conv, var, ctx='default', features=[]):
return self._declare_c_arg(conv.arg_storage_ctype, var) + self._convert_c_arg(idx, conv, var, ctx, features)

def declare_rval(self, out_var):
return ''
Expand Down Expand Up @@ -720,7 +727,7 @@ def __proto_call(self, self_conv, proto, expr_eval, ctx, fixed_arg_count=None):
else:
arg_ctype = conv.arg_storage_ctype
self._source += self._declare_c_arg(conv.arg_storage_ctype, var)
self._source += self._convert_c_arg(argin_idx, conv, var, ctx)
self._source += self._convert_c_arg(argin_idx, conv, var, ctx, features)
argin_idx += 1

c_call_args.append(transform_var_ref_to(var, arg_ctype.get_ref(), arg['carg'].ctype.get_ref()))
Expand Down
4 changes: 2 additions & 2 deletions lib/std.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ def get_size(self, self_var, out_var):
return '%s = %s->size();\n' % (out_var, self_var)

def get_item(self, self_var, idx, out_var, error_var):
out = 'if (size_t(%s) < %s->size())\n' % (idx, self_var)
out = 'if ((%s->size() > 0) && (size_t(%s) < %s->size()))\n' % (self_var, idx, self_var)
out += ' %s = (*%s)[%s];\n' % (out_var, self_var, idx)
out += 'else\n'
out += ' %s = true;\n' % error_var
return out

def set_item(self, self_var, idx, in_var, error_var):
t_in_var = self.wrapped_conv.prepare_var_from_conv(in_var, self.wrapped_conv.ctype.get_ref())
out = 'if (size_t(%s) < %s->size())\n' % (idx, self_var)
out = 'if ((%s->size() > 0) && (size_t(%s) < %s->size()))\n' % (self_var, idx, self_var)
out += ' (*%s)[%s] = %s;\n' % (self_var, idx, t_in_var)
out += 'else\n'
out += ' %s = true;\n' % error_var
Expand Down

0 comments on commit 643169a

Please sign in to comment.