changeset 21610:eff26982a19b

avoid passing temp string to ::set_program_name (bug #47372) * oct-env.cc (octave_env::do_set_program_name): Allocate string to pass to ::set_program_name.
author John W. Eaton <jwe@octave.org>
date Tue, 12 Apr 2016 00:17:49 -0400
parents 46f059de4dce
children edd0ce03f548
files liboctave/system/oct-env.cc
diffstat 1 files changed, 20 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/liboctave/system/oct-env.cc	Mon Apr 11 14:39:02 2016 -0400
+++ b/liboctave/system/oct-env.cc	Tue Apr 12 00:17:49 2016 -0400
@@ -261,18 +261,29 @@
 void
 octave_env::do_set_program_name (const std::string& s) const
 {
-  // For gnulib.
-  ::set_program_name (s.c_str ());
+  bool initialized = false;
 
-  // Let gnulib strip off things like the "lt-" prefix from libtool.
-  prog_invocation_name = program_name;
+  if (! initialized)
+    {
+      // The string passed to gnulib's ::set_program_name function must
+      // exist for the duration of the program so allocate a copy here
+      // instead of passing S.c_str () which only exists as long as the
+      // string object S.
+
+      // For gnulib.
+      ::set_program_name (strsave (s.c_str ()));
 
-  size_t pos
-    = prog_invocation_name.find_last_of (file_ops::dir_sep_chars ());
+      // Let gnulib strip off things like the "lt-" prefix from libtool.
+      prog_invocation_name = program_name;
+
+      size_t pos
+        = prog_invocation_name.find_last_of (file_ops::dir_sep_chars ());
 
-  // Also keep a shortened version of the program name.
-  prog_name = (pos == std::string::npos)
-              ? prog_invocation_name : prog_invocation_name.substr (pos+1);
+      // Also keep a shortened version of the program name.
+      prog_name = (pos == std::string::npos
+                   ? prog_invocation_name
+                   : prog_invocation_name.substr (pos+1));
+    }
 }
 
 // Return a pretty pathname.  If the first part of the pathname is the