Skip to content

Fix memory leak in tostring_rgba_minimize(). (#3197) #3207

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 11, 2014
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions src/_backend_agg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2401,13 +2401,24 @@ RendererAgg::tostring_rgba_minimized(const Py::Tuple& args)
newheight = ymax - ymin;
int newsize = newwidth * newheight * 4;

unsigned char* buf = new unsigned char[newsize];
if (buf == NULL)
// NULL pointer causes Python to allocate uninitialized memory.
// We then grab Python's pointer to uninitialized memory using
// the _AsString() API.
unsigned int* dst;

#if PY3K
data = Py::Bytes(static_cast<const char*>(NULL), (int) newsize);
dst = reinterpret_cast<unsigned int*>(PyBytes_AsString(data.ptr()));
#else
data = Py::String(static_cast<const char*>(NULL), (int) newsize);
dst = reinterpret_cast<unsigned int*>(PyString_AsString(data.ptr()));
#endif

if (dst == NULL)
{
throw Py::MemoryError("RendererAgg::tostring_minimized could not allocate memory");
}

unsigned int* dst = (unsigned int*)buf;
unsigned int* src = (unsigned int*)pixBuffer;
for (int y = ymin; y < ymax; ++y)
{
Expand All @@ -2416,13 +2427,6 @@ RendererAgg::tostring_rgba_minimized(const Py::Tuple& args)
*dst = src[y * width + x];
}
}

// The Py::String will take over the buffer
#if PY3K
data = Py::Bytes((const char *)buf, (int) newsize);
#else
data = Py::String((const char *)buf, (int) newsize);
#endif
}

Py::Tuple bounds(4);
Expand Down