From 46d24861153b5afffcecebfda822da8d2603c4ec Mon Sep 17 00:00:00 2001 From: William S Fulton Date: Wed, 14 Nov 2012 22:16:54 +0000 Subject: [PATCH] Resource leak fixes (or hiding them from Coverity static analysis tool by using String instead of char *) git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@13886 626c5289-ae23-0410-ae9c-e8d60b6d4f22 --- Source/Modules/allegrocl.cxx | 13 +++++------ Source/Modules/csharp.cxx | 4 +++- Source/Modules/d.cxx | 4 +++- Source/Modules/go.cxx | 1 + Source/Modules/guile.cxx | 22 +++++++++---------- Source/Modules/java.cxx | 4 +++- Source/Modules/mzscheme.cxx | 42 ++++++++++++++---------------------- Source/Modules/ocaml.cxx | 16 ++++++-------- Source/Modules/octave.cxx | 4 +++- Source/Modules/php.cxx | 1 + Source/Modules/r.cxx | 1 + Source/Modules/uffi.cxx | 13 +++++------ 12 files changed, 62 insertions(+), 63 deletions(-) diff --git a/Source/Modules/allegrocl.cxx b/Source/Modules/allegrocl.cxx index 56a3116fe..ffd96ca9c 100644 --- a/Source/Modules/allegrocl.cxx +++ b/Source/Modules/allegrocl.cxx @@ -48,7 +48,7 @@ static File *f_cxx_wrapper = 0; static String *module_name = 0; static String *swig_package = 0; -const char *identifier_converter = "identifier-convert-null"; +static String *identifier_converter = NewString("identifier-convert-null"); static bool CWrap = true; // generate wrapper file for C code by default. most correct. static bool Generate_Wrapper = false; @@ -1605,14 +1605,15 @@ void ALLEGROCL::main(int argc, char *argv[]) { /* check for built-ins */ if (!strcmp(conv, "lispify")) { - identifier_converter = "identifier-convert-lispify"; + Delete(identifier_converter); + identifier_converter = NewString("identifier-convert-lispify"); } else if (!strcmp(conv, "null")) { - identifier_converter = "identifier-convert-null"; + Delete(identifier_converter); + identifier_converter = NewString("identifier-convert-null"); } else { /* Must be user defined */ - char *idconv = new char[strlen(conv) + 1]; - strcpy(idconv, conv); - identifier_converter = idconv; + Delete(identifier_converter); + identifier_converter = NewString(conv); } } else if (!strcmp(argv[i], "-cwrap")) { CWrap = true; diff --git a/Source/Modules/csharp.cxx b/Source/Modules/csharp.cxx index 41c0da516..13c132a76 100644 --- a/Source/Modules/csharp.cxx +++ b/Source/Modules/csharp.cxx @@ -792,8 +792,10 @@ public: if (Getattr(n, "sym:overloaded")) { // Emit warnings for the few cases that can't be overloaded in C# and give up on generating wrapper Swig_overload_check(n); - if (Getattr(n, "overload:ignore")) + if (Getattr(n, "overload:ignore")) { + DelWrapper(f); return SWIG_OK; + } } Printv(imclass_class_code, "\n [DllImport(\"", dllimport, "\", EntryPoint=\"", wname, "\")]\n", NIL); diff --git a/Source/Modules/d.cxx b/Source/Modules/d.cxx index fc8e579b4..b21562c1d 100644 --- a/Source/Modules/d.cxx +++ b/Source/Modules/d.cxx @@ -1579,8 +1579,10 @@ public: if (Getattr(n, "sym:overloaded")) { // Emit warnings for the few cases that can't be overloaded in D and give up on generating wrapper Swig_overload_check(n); - if (Getattr(n, "overload:ignore")) + if (Getattr(n, "overload:ignore")) { + DelWrapper(f); return SWIG_OK; + } } // Collect the parameter list for the intermediary D module declaration of diff --git a/Source/Modules/go.cxx b/Source/Modules/go.cxx index 9f086f01c..7c34f8b9a 100644 --- a/Source/Modules/go.cxx +++ b/Source/Modules/go.cxx @@ -3640,6 +3640,7 @@ private: Delete(upcall_name); Delete(callback_wname); Delete(go_name); + DelWrapper(w); return SWIG_OK; } diff --git a/Source/Modules/guile.cxx b/Source/Modules/guile.cxx index 50c9e7e0d..c7e8fe7e4 100644 --- a/Source/Modules/guile.cxx +++ b/Source/Modules/guile.cxx @@ -59,9 +59,9 @@ static File *f_wrappers = 0; static File *f_init = 0; -static char *prefix = (char *) "gswig_"; +static String *prefix = NewString("gswig_"); static char *module = 0; -static char *package = 0; +static String *package = 0; static enum { GUILE_LSTYLE_SIMPLE, // call `SWIG_init()' GUILE_LSTYLE_PASSIVE, // passive linking (no module code) @@ -127,7 +127,7 @@ public: * ------------------------------------------------------------ */ virtual void main(int argc, char *argv[]) { - int i, orig_len; + int i; SWIG_library_directory("guile"); SWIG_typemap_lang("guile"); @@ -140,8 +140,7 @@ public: SWIG_exit(EXIT_SUCCESS); } else if (strcmp(argv[i], "-prefix") == 0) { if (argv[i + 1]) { - prefix = new char[strlen(argv[i + 1]) + 2]; - strcpy(prefix, argv[i + 1]); + prefix = NewString(argv[i + 1]); Swig_mark_arg(i); Swig_mark_arg(i + 1); i++; @@ -150,8 +149,7 @@ public: } } else if (strcmp(argv[i], "-package") == 0) { if (argv[i + 1]) { - package = new char[strlen(argv[i + 1]) + 2]; - strcpy(package, argv[i + 1]); + package = NewString(argv[i + 1]); Swig_mark_arg(i); Swig_mark_arg(i + 1); i++; @@ -278,12 +276,12 @@ public: // should use Swig_warning() ? Printf(stderr, "guile: Warning: -exportprimitive only makes sense with passive linkage without a scmstub.\n"); } - // Make sure `prefix' ends in an underscore - orig_len = strlen(prefix); - if (prefix[orig_len - 1] != '_') { - prefix[1 + orig_len] = 0; - prefix[orig_len] = '_'; + // Make sure `prefix' ends in an underscore + if (prefix) { + const char *px = Char(prefix); + if (px[Len(prefix)] != '_') + Printf(prefix, "_"); } /* Add a symbol for this module */ diff --git a/Source/Modules/java.cxx b/Source/Modules/java.cxx index b64626087..472740d48 100644 --- a/Source/Modules/java.cxx +++ b/Source/Modules/java.cxx @@ -876,8 +876,10 @@ public: if (Getattr(n, "sym:overloaded")) { // Emit warnings for the few cases that can't be overloaded in Java and give up on generating wrapper Swig_overload_check(n); - if (Getattr(n, "overload:ignore")) + if (Getattr(n, "overload:ignore")) { + DelWrapper(f); return SWIG_OK; + } } Printf(imclass_class_code, " public final static native %s %s(", im_return_type, overloaded_name); diff --git a/Source/Modules/mzscheme.cxx b/Source/Modules/mzscheme.cxx index 99513dae3..c2abd7ec4 100644 --- a/Source/Modules/mzscheme.cxx +++ b/Source/Modules/mzscheme.cxx @@ -33,12 +33,10 @@ static String *convert_proto_tab = 0; static String *struct_name = 0; static String *mangled_struct_name = 0; -static char *prefix = 0; +static String *prefix = 0; static bool declaremodule = false; static bool noinit = false; -//DLOPEN PATCH -static char *load_libraries = NULL; -//DLOPEN PATCH +static String *load_libraries = NULL; static String *module = 0; static char *mzscheme_path = (char *) "mzscheme"; static String *init_func_def = 0; @@ -75,8 +73,7 @@ public: SWIG_exit(0); } else if (strcmp(argv[i], "-prefix") == 0) { if (argv[i + 1]) { - prefix = new char[strlen(argv[i + 1]) + 2]; - strcpy(prefix, argv[i + 1]); + prefix = NewString(argv[i + 1]); Swig_mark_arg(i); Swig_mark_arg(i + 1); i++; @@ -90,26 +87,27 @@ public: noinit = true; Swig_mark_arg(i); } -// DLOPEN PATCH else if (strcmp(argv[i], "-dynamic-load") == 0) { - load_libraries = new char[strlen(argv[i + 1]) + 2]; - strcpy(load_libraries, argv[i + 1]); - Swig_mark_arg(i++); - Swig_mark_arg(i); + if (argv[i + 1]) { + Delete(load_libraries); + load_libraries = NewString(argv[i + 1]); + Swig_mark_arg(i++); + Swig_mark_arg(i); + } else { + Swig_arg_error(); + } } -// DLOPEN PATCH } } - // If a prefix has been specified make sure it ends in a '_' + // If a prefix has been specified make sure it ends in a '_' (not actually used!) if (prefix) { - if (prefix[strlen(prefix)] != '_') { - prefix[strlen(prefix) + 1] = 0; - prefix[strlen(prefix)] = '_'; - } + const char *px = Char(prefix); + if (px[Len(prefix)] != '_') + Printf(prefix, "_"); } else - prefix = (char *) "swig_"; + prefix = NewString("swig_"); // Add a symbol for this module @@ -177,11 +175,9 @@ public: Printf(f_init, "\treturn scheme_void;\n}\n"); Printf(f_init, "Scheme_Object *scheme_initialize(Scheme_Env *env) {\n"); - // DLOPEN PATCH if (load_libraries) { Printf(f_init, "mz_set_dlopen_libraries(\"%s\");\n", load_libraries); } - // DLOPEN PATCH Printf(f_init, "\treturn scheme_reload(env);\n"); Printf(f_init, "}\n"); @@ -244,14 +240,12 @@ public: int numreq; String *overname = 0; - // PATCH DLOPEN if (load_libraries) { ParmList *parms = Getattr(n, "parms"); SwigType *type = Getattr(n, "type"); String *name = NewString("caller"); Setattr(n, "wrap:action", Swig_cresult(type, Swig_cresult_name(), Swig_cfunction_call(name, parms))); } - // PATCH DLOPEN // Make a wrapper name for this String *wname = Swig_name_wrapper(iname); @@ -291,7 +285,6 @@ public: numargs = emit_num_arguments(l); numreq = emit_num_required(l); - // DLOPEN PATCH /* Add the holder for the pointer to the function to be opened */ if (load_libraries) { Wrapper_add_local(f, "_function_loaded", "static int _function_loaded=(1==0)"); @@ -302,19 +295,16 @@ public: Wrapper_add_local(f, "caller", SwigType_lstr(d, func)); /*"(*caller)()")); */ } } - // DLOPEN PATCH // adds local variables Wrapper_add_local(f, "lenv", "int lenv = 1"); Wrapper_add_local(f, "values", "Scheme_Object *values[MAXVALUES]"); - // DLOPEN PATCH if (load_libraries) { Printf(f->code, "if (!_function_loaded) { _the_function=mz_load_function(\"%s\");_function_loaded=(1==1); }\n", iname); Printf(f->code, "if (!_the_function) { scheme_signal_error(\"Cannot load C function '%s'\"); }\n", iname); Printf(f->code, "caller=_the_function;\n"); } - // DLOPEN PATCH // Now write code to extract the parameters (this is super ugly) diff --git a/Source/Modules/ocaml.cxx b/Source/Modules/ocaml.cxx index 6204fd2ec..135cc89dc 100644 --- a/Source/Modules/ocaml.cxx +++ b/Source/Modules/ocaml.cxx @@ -30,7 +30,7 @@ static int in_constructor = 0, in_destructor = 0, in_copyconst = 0; static int const_enum = 0; static int static_member_function = 0; static int generate_sizeof = 0; -static char *prefix = 0; +static String *prefix = 0; static char *ocaml_path = (char *) "ocaml"; static bool old_variable_names = false; static String *classname = 0; @@ -107,8 +107,7 @@ public: SWIG_exit(0); } else if (strcmp(argv[i], "-prefix") == 0) { if (argv[i + 1]) { - prefix = new char[strlen(argv[i + 1]) + 2]; - strcpy(prefix, argv[i + 1]); + prefix = NewString(argv[i + 1]); Swig_mark_arg(i); Swig_mark_arg(i + 1); i++; @@ -130,15 +129,14 @@ public: } } - // If a prefix has been specified make sure it ends in a '_' + // If a prefix has been specified make sure it ends in a '_' (not actually used!) if (prefix) { - if (prefix[strlen(prefix)] != '_') { - prefix[strlen(prefix) + 1] = 0; - prefix[strlen(prefix)] = '_'; - } + const char *px = Char(prefix); + if (px[Len(prefix)] != '_') + Printf(prefix, "_"); } else - prefix = (char *) "swig_"; + prefix = NewString("swig_"); // Add a symbol for this module diff --git a/Source/Modules/octave.cxx b/Source/Modules/octave.cxx index 4b42abae5..8a2ba7627 100644 --- a/Source/Modules/octave.cxx +++ b/Source/Modules/octave.cxx @@ -524,7 +524,6 @@ public: } virtual int functionWrapper(Node *n) { - Wrapper *f = NewWrapper(); Parm *p; String *tm; int j; @@ -551,6 +550,7 @@ public: if (!overloaded || last_overload) process_autodoc(n); + Wrapper *f = NewWrapper(); Octave_begin_function(n, f->def, iname, overname, !overloaded); emit_parameter_variables(l, f); @@ -859,6 +859,8 @@ public: Delete(getwname); Delete(setwname); + DelWrapper(setf); + DelWrapper(getf); return SWIG_OK; } diff --git a/Source/Modules/php.cxx b/Source/Modules/php.cxx index a0e8b3223..ab0d0e6b3 100644 --- a/Source/Modules/php.cxx +++ b/Source/Modules/php.cxx @@ -2330,6 +2330,7 @@ done: Printf(f->code, "}\n"); Wrapper_print(f, s_wrappers); + DelWrapper(f); return SWIG_OK; } diff --git a/Source/Modules/r.cxx b/Source/Modules/r.cxx index a8cdc1cc8..9e931a38d 100644 --- a/Source/Modules/r.cxx +++ b/Source/Modules/r.cxx @@ -726,6 +726,7 @@ String * R::createFunctionPointerHandler(SwigType *t, Node *n, int *numArgs) { Delete(rtype); Delete(rettype); Delete(funcparams); + DelWrapper(f); return funName; } diff --git a/Source/Modules/uffi.cxx b/Source/Modules/uffi.cxx index 9e317f8bc..3b9a0abaa 100644 --- a/Source/Modules/uffi.cxx +++ b/Source/Modules/uffi.cxx @@ -47,7 +47,7 @@ static struct { String **entries; } defined_foreign_types; -static const char *identifier_converter = "identifier-convert-null"; +static String *identifier_converter = NewString("identifier-convert-null"); static int any_varargs(ParmList *pl) { Parm *p; @@ -221,14 +221,15 @@ void UFFI::main(int argc, char *argv[]) { /* check for built-ins */ if (!strcmp(conv, "lispify")) { - identifier_converter = "identifier-convert-lispify"; + Delete(identifier_converter); + identifier_converter = NewString("identifier-convert-lispify"); } else if (!strcmp(conv, "null")) { - identifier_converter = "identifier-convert-null"; + Delete(identifier_converter); + identifier_converter = NewString("identifier-convert-null"); } else { /* Must be user defined */ - char *idconv = new char[strlen(conv) + 1]; - strcpy(idconv, conv); - identifier_converter = idconv; + Delete(identifier_converter); + identifier_converter = NewString(conv); } }