Skip to content

Commit

Permalink
On realloc error, release previously allocated memory
Browse files Browse the repository at this point in the history
  • Loading branch information
fdmanana committed Mar 27, 2011
1 parent 722f4ae commit 97d1aa2
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
5 changes: 5 additions & 0 deletions c_src/erl_nif_compat.h
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ extern "C" {
#define enif_release_resource_compat enif_release_resource #define enif_release_resource_compat enif_release_resource
#define enif_alloc_binary_compat enif_alloc_binary #define enif_alloc_binary_compat enif_alloc_binary
#define enif_alloc_compat enif_alloc #define enif_alloc_compat enif_alloc
#define enif_release_binary_compat enif_release_binary
#define enif_free_compat enif_free #define enif_free_compat enif_free
#define enif_get_atom_compat enif_get_atom #define enif_get_atom_compat enif_get_atom
#define enif_priv_data_compat enif_get_data #define enif_priv_data_compat enif_get_data
Expand All @@ -59,6 +60,7 @@ extern "C" {
#define enif_release_resource_compat enif_release_resource #define enif_release_resource_compat enif_release_resource
#define enif_alloc_binary_compat enif_alloc_binary #define enif_alloc_binary_compat enif_alloc_binary
#define enif_realloc_binary_compat enif_realloc_binary #define enif_realloc_binary_compat enif_realloc_binary
#define enif_release_binary_compat enif_release_binary
#define enif_alloc_compat enif_alloc #define enif_alloc_compat enif_alloc
#define enif_free_compat enif_free #define enif_free_compat enif_free
#define enif_get_atom_compat enif_get_atom #define enif_get_atom_compat enif_get_atom
Expand Down Expand Up @@ -87,6 +89,9 @@ extern "C" {
#define enif_realloc_binary_compat(E, S, B) \ #define enif_realloc_binary_compat(E, S, B) \
enif_realloc_binary(S, B) enif_realloc_binary(S, B)


#define enif_release_binary_compat(E, B) \
enif_release_binary(B)

#define enif_alloc_compat(E, S) \ #define enif_alloc_compat(E, S) \
enif_alloc(S) enif_alloc(S)


Expand Down
3 changes: 3 additions & 0 deletions c_src/snappy.cc
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class SnappyNifSink : public snappy::Sink {
public: public:
SnappyNifSink(ErlNifEnv* e) : env(e), length(0) { SnappyNifSink(ErlNifEnv* e) : env(e), length(0) {
if (!enif_alloc_binary_compat(env, 0, &bin)) { if (!enif_alloc_binary_compat(env, 0, &bin)) {
enif_release_binary_compat(env, &bin);
throw OutOfMem(); throw OutOfMem();
} }
} }
Expand All @@ -52,6 +53,7 @@ class SnappyNifSink : public snappy::Sink {
size_t sz = len > ALLOC_SIZE ? len + ALLOC_SIZE - (len % ALLOC_SIZE) : ALLOC_SIZE; size_t sz = len > ALLOC_SIZE ? len + ALLOC_SIZE - (len % ALLOC_SIZE) : ALLOC_SIZE;


if (!enif_realloc_binary_compat(env, &bin, bin.size + sz)) { if (!enif_realloc_binary_compat(env, &bin, bin.size + sz)) {
enif_release_binary_compat(env, &bin);
throw OutOfMem(); throw OutOfMem();
} }
} }
Expand All @@ -63,6 +65,7 @@ class SnappyNifSink : public snappy::Sink {
if (bin.size > length) { if (bin.size > length) {
if (!enif_realloc_binary_compat(env, &bin, length)) { if (!enif_realloc_binary_compat(env, &bin, length)) {
// shouldn't happen // shouldn't happen
enif_release_binary_compat(env, &bin);
throw OutOfMem(); throw OutOfMem();
} }
} }
Expand Down

0 comments on commit 97d1aa2

Please sign in to comment.