Skip to content

Commit d741050

Browse files
committed
Fix use-after-free bug when plotting unknown numeric types
1 parent edb4074 commit d741050

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

matplotlibcpp.h

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -299,22 +299,24 @@ template <> struct select_npy_type<uint64_t> { const static NPY_TYPES type = NPY
299299
template<typename Numeric>
300300
PyObject* get_array(const std::vector<Numeric>& v)
301301
{
302-
detail::_interpreter::get(); //interpreter needs to be initialized for the numpy commands to work
302+
detail::_interpreter::get(); //interpreter needs to be initialized for the numpy commands to work
303+
npy_intp vsize = v.size();
303304
NPY_TYPES type = select_npy_type<Numeric>::type;
304-
if (type == NPY_NOTYPE)
305-
{
306-
std::vector<double> vd(v.size());
307-
npy_intp vsize = v.size();
308-
std::copy(v.begin(),v.end(),vd.begin());
309-
PyObject* varray = PyArray_SimpleNewFromData(1, &vsize, NPY_DOUBLE, (void*)(vd.data()));
305+
if (type == NPY_NOTYPE) {
306+
size_t memsize = v.size()*sizeof(double);
307+
double* dp = static_cast<double*>(::malloc(memsize));
308+
for (size_t i=0; i<v.size(); ++i)
309+
dp[i] = v[i];
310+
PyObject* varray = PyArray_SimpleNewFromData(1, &vsize, NPY_DOUBLE, dp);
311+
PyArray_UpdateFlags(reinterpret_cast<PyArrayObject*>(varray), NPY_ARRAY_OWNDATA);
310312
return varray;
311313
}
312-
313-
npy_intp vsize = v.size();
314+
314315
PyObject* varray = PyArray_SimpleNewFromData(1, &vsize, type, (void*)(v.data()));
315316
return varray;
316317
}
317318

319+
318320
template<typename Numeric>
319321
PyObject* get_2darray(const std::vector<::std::vector<Numeric>>& v)
320322
{

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy