mmomtchev / swig

This is SWIG JavaScript Evolution, a fork of the SWIG project with modern JavaScript/TypeScript support including WASM and async
http://www.swig.org
Other
7 stars 0 forks source link

With Node-API, typemaps that accept `const char *` strings leak memory on exception #16

Closed mmomtchev closed 5 months ago

mmomtchev commented 5 months ago

The code produced for a function that expects a const char * string leaks memory if the called function throws:

(buf1 is not freed on the exception path)

template <typename SWIG_OBJ_WRAP>
Napi::Value _Magick_Color_templ<SWIG_OBJ_WRAP>::_wrap_new_Magick_Color__SWIG_4(const Napi::CallbackInfo &info) {
  Napi::Env env = info.Env();
  Napi::Object self;
  char *arg1 = (char *) 0 ;
  int res1 ;
  char *buf1 = 0 ;
  int alloc1 = 0 ;
  Magick::Color *result;
  NAPI_CHECK_RESULT(info.This().ToObject(), self);
  this->owned = true;
  if(static_cast<int>(info.Length()) < 1 || static_cast<int>(info.Length()) > 1) {
    SWIG_Error(SWIG_ERROR, "Illegal number of arguments for _wrap_new_Magick_Color__SWIG_4.");
  }
  res1 = SWIG_AsCharPtrAndSize(info[0], &buf1, NULL, &alloc1);
  if (!SWIG_IsOK(res1)) {
    SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "new_Magick_Color" "', argument " "1"" of type '" "char const *""'");
  }
  arg1 = reinterpret_cast< char * >(buf1);{
    try {
      result = (Magick::Color *)new Magick::Color((char const *)arg1);
    } catch (const Magick::Exception &e) {
      do {
        SWIG_NAPI_Raise(env, e.what()); SWIG_fail; 
      } while (0);
      SWIG_fail;
    }
  }

  if (alloc1 == SWIG_NEWOBJ) delete[] buf1;

  this->self = result;

#ifndef NAPI_CPP_EXCEPTIONS
  goto fail;
fail:
#endif
  return Napi::Value();
}
mmomtchev commented 5 months ago

This is the continuation of swig#2563 and happens only in constructors