# HG changeset patch # User John W. Eaton # Date 1460434669 14400 # Node ID eff26982a19b2e9ee0b0eca6c5ffa14265014111 # Parent 46f059de4dce1a62efd3b3f4bf7c273b49608e26 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. diff -r 46f059de4dce -r eff26982a19b liboctave/system/oct-env.cc --- 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