summaryrefslogtreecommitdiff
path: root/src/lib
diff options
context:
space:
mode:
authorCarl Hetherington <cth@carlh.net>2014-02-09 22:21:31 +0000
committerCarl Hetherington <cth@carlh.net>2014-02-09 22:21:31 +0000
commite4b65ba64c319281d6bff15e950ebce697c9621a (patch)
tree978bf1f27d4cb65496f336c23cfb412c083a45c5 /src/lib
parent8322da2c2ff305103ab1b180e79f470d30366699 (diff)
Fix 1 crash on low memory.
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/ffmpeg.cc3
-rw-r--r--src/lib/image.cc8
-rw-r--r--src/lib/util.cc13
-rw-r--r--src/lib/util.h1
4 files changed, 20 insertions, 5 deletions
diff --git a/src/lib/ffmpeg.cc b/src/lib/ffmpeg.cc
index b7ae04b06..fae9baa2b 100644
--- a/src/lib/ffmpeg.cc
+++ b/src/lib/ffmpeg.cc
@@ -26,6 +26,7 @@ extern "C" {
#include "ffmpeg.h"
#include "ffmpeg_content.h"
#include "exceptions.h"
+#include "util.h"
#include "i18n.h"
@@ -85,7 +86,7 @@ FFmpeg::setup_general ()
av_register_all ();
_file_group.set_paths (_ffmpeg_content->paths ());
- _avio_buffer = static_cast<uint8_t*> (av_malloc (_avio_buffer_size));
+ _avio_buffer = static_cast<uint8_t*> (wrapped_av_malloc (_avio_buffer_size));
_avio_context = avio_alloc_context (_avio_buffer, _avio_buffer_size, 0, this, avio_read_wrapper, 0, avio_seek_wrapper);
_format_context = avformat_alloc_context ();
_format_context->pb = _avio_context;
diff --git a/src/lib/image.cc b/src/lib/image.cc
index 95bf2b04d..4722563c4 100644
--- a/src/lib/image.cc
+++ b/src/lib/image.cc
@@ -498,13 +498,13 @@ Image::Image (AVPixelFormat p, libdcp::Size s, bool aligned)
void
Image::allocate ()
{
- _data = (uint8_t **) av_malloc (4 * sizeof (uint8_t *));
+ _data = (uint8_t **) wrapped_av_malloc (4 * sizeof (uint8_t *));
_data[0] = _data[1] = _data[2] = _data[3] = 0;
- _line_size = (int *) av_malloc (4 * sizeof (int));
+ _line_size = (int *) wrapped_av_malloc (4 * sizeof (int));
_line_size[0] = _line_size[1] = _line_size[2] = _line_size[3] = 0;
- _stride = (int *) av_malloc (4 * sizeof (int));
+ _stride = (int *) wrapped_av_malloc (4 * sizeof (int));
_stride[0] = _stride[1] = _stride[2] = _stride[3] = 0;
for (int i = 0; i < components(); ++i) {
@@ -520,7 +520,7 @@ Image::allocate ()
seem to mind. The nasty + 1 in this malloc makes sure there is always a byte
for that instruction to read safely.
*/
- _data[i] = (uint8_t *) av_malloc (_stride[i] * lines (i) + 1);
+ _data[i] = (uint8_t *) wrapped_av_malloc (_stride[i] * lines (i) + 1);
}
}
diff --git a/src/lib/util.cc b/src/lib/util.cc
index edb202df2..190bb42b1 100644
--- a/src/lib/util.cc
+++ b/src/lib/util.cc
@@ -27,6 +27,7 @@
#include <iostream>
#include <fstream>
#include <climits>
+#include <stdexcept>
#ifdef DCPOMATIC_POSIX
#include <execinfo.h>
#include <cxxabi.h>
@@ -92,6 +93,7 @@ using std::istream;
using std::numeric_limits;
using std::pair;
using std::cout;
+using std::bad_alloc;
using std::streampos;
using boost::shared_ptr;
using boost::thread;
@@ -916,3 +918,14 @@ fit_ratio_within (float ratio, libdcp::Size full_frame)
return libdcp::Size (full_frame.width, rint (full_frame.width / ratio));
}
+
+void *
+wrapped_av_malloc (size_t s)
+{
+ void* p = av_malloc (s);
+ if (!p) {
+ throw bad_alloc ();
+ }
+ return p;
+}
+
diff --git a/src/lib/util.h b/src/lib/util.h
index 7dcd920b7..a229bbfc9 100644
--- a/src/lib/util.h
+++ b/src/lib/util.h
@@ -120,6 +120,7 @@ extern float get_required_float (std::multimap<std::string, std::string> const &
extern std::string get_required_string (std::multimap<std::string, std::string> const & kv, std::string k);
extern int get_optional_int (std::multimap<std::string, std::string> const & kv, std::string k);
extern std::string get_optional_string (std::multimap<std::string, std::string> const & kv, std::string k);
+extern void* wrapped_av_malloc (size_t);
/** @class Socket
* @brief A class to wrap a boost::asio::ip::tcp::socket with some things