# HG changeset patch # User John W. Eaton # Date 1449609774 18000 # Node ID b3f985199f3f74ffeb10a80fdfde0a70c397612b # Parent 779898f2a02a51213ef0bef9d9374d61eb09e878 eliminate memory leaks in error functions * error.cc (pr_where_2, error_1, handle_message, error): Use std::string object to manage memory for temporary string copies. diff -r 779898f2a02a -r b3f985199f3f libinterp/corefcn/error.cc --- a/libinterp/corefcn/error.cc Tue Dec 08 16:34:57 2015 -0500 +++ b/libinterp/corefcn/error.cc Tue Dec 08 16:22:54 2015 -0500 @@ -229,10 +229,8 @@ { if (len > 1) { - char *tmp_fmt = strsave (fmt); - tmp_fmt[len - 1] = '\0'; - verror (false, os, 0, "", tmp_fmt, args); - delete [] tmp_fmt; + std::string tmp_fmt (fmt, len - 1); + verror (false, os, 0, "", tmp_fmt.c_str (), args); } } else @@ -475,11 +473,9 @@ { if (len > 1) { - // FIXME: This is a memory leak. - char *tmp_fmt = strsave (fmt); - tmp_fmt[len - 1] = '\0'; - verror (true, os, name, id, tmp_fmt, args, with_cfn); - delete [] tmp_fmt; + std::string tmp_fmt (fmt, len - 1); + verror (true, os, name, id, tmp_fmt.c_str (), + args, with_cfn); } // If format ends with newline, suppress stack trace. @@ -881,11 +877,9 @@ { if (len > 1) { - char *tmp_msg = strsave (msg); - tmp_msg[len - 1] = '\0'; - f (id, "%s\n", tmp_msg); + std::string tmp_msg (msg, len - 1); + f (id, "%s\n", tmp_msg.c_str ()); retval = tmp_msg; - delete [] tmp_msg; } } else @@ -951,18 +945,17 @@ } // Ugh. - char *tmp_msg = strsave (msg.c_str ()); + std::string tmp_msg (msg); if (tmp_msg[len-1] == '\n') { if (len > 1) { - tmp_msg[len - 1] = '\0'; + tmp_msg.erase (len - 1); rethrow_error (id.c_str (), "%s\n", tmp_msg); } } else rethrow_error (id.c_str (), "%s", tmp_msg); - delete [] tmp_msg; // FIXME: is this the right thing to do for Vlast_error_stack? // Should it be saved and restored with unwind_protect?