]> sourceware.org Git - systemtap.git/commitdiff
Fix canonicalize_file_name() leaks
authorJosh Stone <jistone@redhat.com>
Thu, 24 Oct 2013 21:56:06 +0000 (14:56 -0700)
committerJosh Stone <jistone@redhat.com>
Fri, 25 Oct 2013 00:20:36 +0000 (17:20 -0700)
This function allocates memory, but in a few places we weren't freeing
it.  We have a resolve_path() to do this correctly for std::string-happy
code, and this patch adds free() for the rest.

dwflpp.cxx
session.cxx
tapsets.cxx
translate.cxx
util.cxx
util.h

index 43508446c6c27f46ad7111201ac145428c1d9e7e..c4978657f5da7c12eedfbdd0372dc6ae3c50c538 100644 (file)
@@ -2194,7 +2194,7 @@ dwflpp::emit_address (struct obstack *pool, Dwarf_Addr address)
           obstack_printf (pool, "/* pragma:vma */");
           obstack_printf (pool, "({ unsigned long addr = 0; ");
           obstack_printf (pool, "addr = _stp_umodule_relocate (\"%s\",%#" PRIx64 ", current); ",
-                          canonicalize_file_name(module_name.c_str()), address);
+                          resolve_path(module_name.c_str()).c_str(), address);
           obstack_printf (pool, "addr; })");
         }
     }
index 8f2004a2dd671369aaa27a87a69160cc5c685b31..64173b6623055d5853626ee013a5df3a489f1fef 100644 (file)
@@ -701,11 +701,8 @@ systemtap_session::parse_cmdline (int argc, char * const argv [])
               return 1;
             }
             // At runtime user module names are resolved through their
-            // canonical (absolute) path.
-            const char *mpath = canonicalize_file_name (optarg);
-            if (mpath == NULL) // Must be a kernel module name
-              mpath = optarg;
-            unwindsym_modules.insert (string (mpath));
+            // canonical (absolute) path, or else it's a kernel module name.
+            unwindsym_modules.insert (resolve_path (optarg));
             // NB: we used to enable_vma_tracker() here for PR10228, but now
             // we'll leave that to pragma:vma functions which actually use it.
             break;
@@ -1224,13 +1221,14 @@ systemtap_session::parse_cmdline (int argc, char * const argv [])
              cerr << "ERROR: multiple --sysroot options not supported" << endl;
              return 1;
          } else {
-             const char *spath = canonicalize_file_name (optarg);
+             char *spath = canonicalize_file_name (optarg);
              if (spath == NULL) {
                  cerr << _F("ERROR: %s is an invalid directory for --sysroot", optarg) << endl;
                  return 1;
              }
 
              sysroot = string(spath);
+             free (spath);
              if (sysroot[sysroot.size() - 1] != '/')
                  sysroot.append("/");
 
index f729c1ca2b3a71d8ccd47f90ebd722840dbbf99a..5eac02eabc615c8133b1443a9bfca72e20329cb0 100644 (file)
@@ -6905,9 +6905,8 @@ dwarf_builder::build(systemtap_session & sess,
                   // patterns like process("stap*").  Otherwise it may go through
                   // to the next round of expansion as ("stap"), leading to a $PATH
                   // search that's not consistent with the glob search already done.
-
-                  char *cf = canonicalize_file_name (globbed);
-                  if (cf) globbed = cf;
+                  string canononicalized = resolve_path (globbed);
+                  globbed = canononicalized.c_str();
 
                   // synthesize a new probe_point, with the glob-expanded string
                   probe_point *pp = new probe_point (*location);
index 758d1b88083c27b640e903528422a6b57d461c18..1f9568946cf3edb96c8ad50804a808311d8fb044 100644 (file)
@@ -6155,8 +6155,8 @@ dump_unwindsym_cxt (Dwfl_Module *m,
 
   // For user space modules store canonical path and base name.
   // For kernel modules just the name itself.
-  const char *mainpath = canonicalize_file_name(mainfile);
-  const char *mainname = strrchr(mainpath, '/');
+  string mainpath = resolve_path(mainfile);
+  const char *mainname = mainpath.c_str() + mainpath.rfind('/');
   if (modname[0] == '/')
     mainname++;
   else
index 7acbf8d95eee2a7d24afa068a71c40beff5d813e..e84a710faf00863e0847b845f7aa13365254caf2 100644 (file)
--- a/util.cxx
+++ b/util.cxx
@@ -488,23 +488,11 @@ string find_executable(const string& name, const string& sysroot,
     retpath = sysroot + name;
 
   // Canonicalize the path name.
-  char *cf = canonicalize_file_name (retpath.c_str());
-  if (cf)
-    {
-      string scf = string(cf);
-      if (sysroot.empty())
-        retpath = scf;
-      else {
-        int pos = scf.find(sysroot);
-        if (pos == 0)
-          retpath = scf;
-        else
-          throw runtime_error(_F("find_executable(): file %s not in sysroot %s", cf, sysroot.c_str()));
-      }
-      free (cf);
-    }
-
-  return retpath;
+  string scf = resolve_path(retpath);
+  if (!startswith(scf, sysroot))
+    throw runtime_error(_F("find_executable(): file %s not in sysroot %s",
+                           scf.c_str(), sysroot.c_str()));
+  return scf;
 }
 
 
diff --git a/util.h b/util.h
index b7a7c955753598f6d33ae13a6763c64db5c20d4f..6561be7f79b5b8eaa92817e786057e108b3d83b6 100644 (file)
--- a/util.h
+++ b/util.h
@@ -251,6 +251,12 @@ startswith(const std::string & s, const char * prefix)
   return (s.compare(0, std::strlen(prefix), prefix) == 0);
 }
 
+inline bool
+startswith(const std::string & s, const std::string & prefix)
+{
+  return (s.compare(0, prefix.length(), prefix) == 0);
+}
+
 
 // Returns whether a string ends with the given suffix
 inline bool
This page took 0.068755 seconds and 5 git commands to generate.