File tree 1 file changed +14
-10
lines changed
1 file changed +14
-10
lines changed Original file line number Diff line number Diff line change @@ -2401,13 +2401,24 @@ RendererAgg::tostring_rgba_minimized(const Py::Tuple& args)
2401
2401
newheight = ymax - ymin;
2402
2402
int newsize = newwidth * newheight * 4 ;
2403
2403
2404
- unsigned char * buf = new unsigned char [newsize];
2405
- if (buf == NULL )
2404
+ // NULL pointer causes Python to allocate uninitialized memory.
2405
+ // We then grab Python's pointer to uninitialized memory using
2406
+ // the _AsString() API.
2407
+ unsigned int * dst;
2408
+
2409
+ #if PY3K
2410
+ data = Py::Bytes (static_cast <const char *>(NULL ), (int ) newsize);
2411
+ dst = reinterpret_cast <unsigned int *>(PyBytes_AsString (data.ptr ()));
2412
+ #else
2413
+ data = Py::String (static_cast <const char *>(NULL ), (int ) newsize);
2414
+ dst = reinterpret_cast <unsigned int *>(PyString_AsString (data.ptr ()));
2415
+ #endif
2416
+
2417
+ if (dst == NULL )
2406
2418
{
2407
2419
throw Py::MemoryError (" RendererAgg::tostring_minimized could not allocate memory" );
2408
2420
}
2409
2421
2410
- unsigned int * dst = (unsigned int *)buf;
2411
2422
unsigned int * src = (unsigned int *)pixBuffer;
2412
2423
for (int y = ymin; y < ymax; ++y)
2413
2424
{
@@ -2416,13 +2427,6 @@ RendererAgg::tostring_rgba_minimized(const Py::Tuple& args)
2416
2427
*dst = src[y * width + x];
2417
2428
}
2418
2429
}
2419
-
2420
- // The Py::String will take over the buffer
2421
- #if PY3K
2422
- data = Py::Bytes ((const char *)buf, (int ) newsize);
2423
- #else
2424
- data = Py::String ((const char *)buf, (int ) newsize);
2425
- #endif
2426
2430
}
2427
2431
2428
2432
Py::Tuple bounds (4 );
You can’t perform that action at this time.
0 commit comments