protect against too many args

This commit is contained in:
Hans Dembinski
2017-03-15 23:32:07 +01:00
parent e9fa8aa820
commit d3203625c1

View File

@@ -11,7 +11,6 @@
#include <boost/histogram/serialization.hpp>
#include <boost/python.hpp>
#include <boost/python/raw_function.hpp>
#include <boost/foreach.hpp>
#include <boost/shared_ptr.hpp>
#ifdef HAVE_NUMPY
# define NO_IMPORT_ARRAY
@@ -204,11 +203,19 @@ histogram_value(python::tuple args, python::dict kwargs) {
using namespace python;
const dynamic_histogram<>& self = extract<const dynamic_histogram<>&>(args[0]);
if (self.dim() != (len(args) - 1)) {
const unsigned dim = len(args) - 1;
if (self.dim() != dim) {
PyErr_SetString(PyExc_RuntimeError, "wrong number of arguments");
throw_error_already_set();
}
if (dim >= BOOST_HISTOGRAM_AXIS_LIMIT) {
std::ostringstream os;
os << "too many axes, maximum is " << BOOST_HISTOGRAM_AXIS_LIMIT;
PyErr_SetString(PyExc_RuntimeError, os.str().c_str());
throw_error_already_set();
}
if (kwargs) {
PyErr_SetString(PyExc_RuntimeError, "no keyword arguments allowed");
throw_error_already_set();
@@ -226,11 +233,19 @@ histogram_variance(python::tuple args, python::dict kwargs) {
using namespace python;
const dynamic_histogram<>& self = extract<const dynamic_histogram<>&>(args[0]);
if (self.dim() != (len(args) - 1)) {
const unsigned dim = len(args) - 1;
if (self.dim() != dim) {
PyErr_SetString(PyExc_RuntimeError, "wrong number of arguments");
throw_error_already_set();
}
if (dim >= BOOST_HISTOGRAM_AXIS_LIMIT) {
std::ostringstream os;
os << "too many axes, maximum is " << BOOST_HISTOGRAM_AXIS_LIMIT;
PyErr_SetString(PyExc_RuntimeError, os.str().c_str());
throw_error_already_set();
}
if (kwargs) {
PyErr_SetString(PyExc_RuntimeError, "no keyword arguments allowed");
throw_error_already_set();