]> git.ipfire.org Git - thirdparty/valgrind.git/commitdiff
Merge revisions 14210 and 14626 from the BUF_REMOVAL branch to trunk.
authorFlorian Krohm <florian@eich-krohm.de>
Tue, 14 Oct 2014 21:01:33 +0000 (21:01 +0000)
committerFlorian Krohm <florian@eich-krohm.de>
Tue, 14 Oct 2014 21:01:33 +0000 (21:01 +0000)
Change VG_(resolve_filename) to not truncate the result which is returned
in a static buffer now. Fix callsites.
Simplify VG_(di_notify_pdb_debuginfo) to use VG_(resolve_filename).
Fix VG_(readlink) prototype.

git-svn-id: svn://svn.valgrind.org/valgrind/trunk@14628

coregrind/m_coredump/coredump-elf.c
coregrind/m_debuginfo/debuginfo.c
coregrind/m_libcfile.c
coregrind/m_syswrap/syswrap-generic.c
coregrind/pub_core_libcfile.h
include/pub_tool_libcfile.h

index 6f2b12c1c2996fad25e13489eba2711a466b210c..a40369b3c20603e90eaa2ae919a624820a42a699 100644 (file)
@@ -193,7 +193,7 @@ static void write_note(Int fd, const struct note *n)
 static void fill_prpsinfo(const ThreadState *tst,
                           struct vki_elf_prpsinfo *prpsinfo)
 {
-   static HChar name[VKI_PATH_MAX];
+   HChar *name;
 
    VG_(memset)(prpsinfo, 0, sizeof(*prpsinfo));
 
@@ -220,7 +220,7 @@ static void fill_prpsinfo(const ThreadState *tst,
    prpsinfo->pr_uid = 0;
    prpsinfo->pr_gid = 0;
    
-   if (VG_(resolve_filename)(VG_(cl_exec_fd), name, VKI_PATH_MAX)) {
+   if (VG_(resolve_filename)(VG_(cl_exec_fd), &name)) {
       HChar *n = name+VG_(strlen)(name)-1;
 
       while (n > name && *n != '/')
index c27179240f4dd2ef21552e784c4be8b57136127e..956822ed2c0f4625dfa7dc9b5f7b00cc74fbebf3 100644 (file)
@@ -1091,7 +1091,6 @@ void VG_(di_notify_pdb_debuginfo)( Int fd_obj, Addr avma_obj,
 {
    Int    i, r, sz_exename;
    ULong  obj_mtime, pdb_mtime;
-   HChar  exename[VKI_PATH_MAX];
    HChar* pdbname = NULL;
    HChar* dot;
    SysRes sres;
@@ -1112,21 +1111,17 @@ void VG_(di_notify_pdb_debuginfo)( Int fd_obj, Addr avma_obj,
       time into obj_mtime. */
    r = VG_(fstat)(fd_obj, &stat_buf);
    if (r == -1)
-      goto out; /* stat failed ?! */
+      return; /* stat failed ?! */
    vg_assert(r == 0);
    obj_mtime = stat_buf.mtime;
 
-   /* and get its name into exename[]. */
-   vg_assert(VKI_PATH_MAX > 100); /* to ensure /proc/self/fd/%d is safe */
-   VG_(memset)(exename, 0, sizeof(exename));
-   VG_(sprintf)(exename, "/proc/self/fd/%d", fd_obj);
-   /* convert exename from a symlink to real name .. overwrites the
-      old contents of the buffer.  Ick. */
-   sz_exename = VG_(readlink)(exename, exename, sizeof(exename)-2 );
-   if (sz_exename == -1)
-      goto out; /* readlink failed ?! */
-   vg_assert(sz_exename >= 0 && sz_exename < sizeof(exename));
-   vg_assert(exename[sizeof(exename)-1] == 0);
+   /* and get its name into exename. */
+   HChar *exe;
+   if (! VG_(resolve_filename)(fd_obj, &exe))
+      return; /*  failed */
+   sz_exename = VG_(strlen)(exe);
+   HChar exename[sz_exename + 1];
+   VG_(strcpy)(exename, exe);  // make a copy on the stack 
 
    if (VG_(clo_verbosity) > 0) {
       VG_(message)(Vg_UserMsg, "LOAD_PDB_DEBUGINFO: objname: %s\n", exename);
index 06a4461676995db9eae91e49fa6cd37f97b9d061..0e01206f6890db3312ad1dc1a5f32f4238459b33 100644 (file)
@@ -71,27 +71,58 @@ Int VG_(safe_fd)(Int oldfd)
 
 /* Given a file descriptor, attempt to deduce its filename.  To do
    this, we use /proc/self/fd/<FD>.  If this doesn't point to a file,
-   or if it doesn't exist, we return False. */
-Bool VG_(resolve_filename) ( Int fd, HChar* buf, Int n_buf )
+   or if it doesn't exist, we return False. 
+   Upon successful completion *result contains the filename. The
+   filename will be overwritten with the next invocation so callers
+   need to copy the filename if needed. *result is NULL if the filename
+   cannot be deduced. */
+Bool VG_(resolve_filename) ( Int fd, HChar** result )
 {
 #  if defined(VGO_linux)
-   HChar tmp[64];
+   static HChar *buf = NULL;
+   static SizeT  bufsiz = 0;
+
+   if (buf == NULL) {   // first time
+      bufsiz = 500;
+      buf = VG_(malloc)("resolve_filename", bufsiz);
+   }
+
+   HChar tmp[64];   // large enough
    VG_(sprintf)(tmp, "/proc/self/fd/%d", fd);
-   VG_(memset)(buf, 0, n_buf);
-   if (VG_(readlink)(tmp, buf, n_buf) > 0 && buf[0] == '/')
+
+   while (42) {
+      SSizeT res = VG_(readlink)(tmp, buf, bufsiz);
+      if (res < 0) break;
+      if (res == bufsiz) {  // buffer too small; increase and retry
+         bufsiz += 500;
+         buf = VG_(realloc)("resolve_filename", buf, bufsiz);
+         continue;
+      }
+      vg_assert(bufsiz > res);  // paranoia
+      if (buf[0] != '/') break;
+
+      buf[res] = '\0';
+      *result = buf;
       return True;
-   else
-      return False;
+   }
+   // Failure
+   *result = NULL;
+   return False;
 
 #  elif defined(VGO_darwin)
    HChar tmp[VKI_MAXPATHLEN+1];
    if (0 == VG_(fcntl)(fd, VKI_F_GETPATH, (UWord)tmp)) {
-      if (n_buf > 0) {
-         VG_(strncpy)( buf, tmp, n_buf < sizeof(tmp) ? n_buf : sizeof(tmp) );
-         buf[n_buf-1] = 0;
-      }
-      if (tmp[0] == '/') return True;
+      static HChar *buf = NULL;
+
+      if (buf == NULL) 
+         buf = VG_(malloc)("resolve_filename", VKI_MAXPATHLEN+1);
+      VG_(strcpy)( buf, tmp );
+
+      *result = buf;
+      if (buf[0] == '/') return True;
    }
+   // Failure
+   *result = NULL;
    return False;
 
 #  else
@@ -506,7 +537,11 @@ SysRes VG_(poll) (struct vki_pollfd *fds, Int nfds, Int timeout)
 }
 
 
-Int VG_(readlink) (const HChar* path, HChar* buf, UInt bufsiz)
+/* Performs the readlink operation and puts the result into 'buf'.
+   Note, that the string in 'buf' is *not* null-terminated. The function
+   returns the number of characters put into 'buf' or -1 if an error
+   occurred. */
+SSizeT VG_(readlink) (const HChar* path, HChar* buf, SizeT bufsiz)
 {
    SysRes res;
    /* res = readlink( path, buf, bufsiz ); */
index 8676e7c9a1cee1cc4248cd176e7383af8b87be0b..7e49124e169dbab0fb81fed3ead4ba757f4050f3 100644 (file)
@@ -621,9 +621,9 @@ void ML_(record_fd_open_with_given_name)(ThreadId tid, Int fd, char *pathname)
 // Record opening of an fd, and find its name.
 void ML_(record_fd_open_named)(ThreadId tid, Int fd)
 {
-   static HChar buf[VKI_PATH_MAX];
+   HChar* buf;
    HChar* name;
-   if (VG_(resolve_filename)(fd, buf, VKI_PATH_MAX))
+   if (VG_(resolve_filename)(fd, &buf))
       name = buf;
    else
       name = NULL;
index d3bd68069adbbac6eb4284dbed2d04e3216721f7..707399f8517797912af3e0e62d39f0348203edbb 100644 (file)
@@ -44,7 +44,7 @@ extern Int VG_(safe_fd) ( Int oldfd );
 extern Int VG_(fcntl)   ( Int fd, Int cmd, Addr arg );
 
 /* Convert an fd into a filename */
-extern Bool VG_(resolve_filename) ( Int fd, HChar* buf, Int n_buf );
+extern Bool VG_(resolve_filename) ( Int fd, HChar*buf );
 
 /* Return the size of a file, or -1 in case of error */
 extern Long VG_(fsize) ( Int fd );
index bb1a6e4f704575762e62eefaa1645dab2d4d92b4..3df5be56c62cc67aa6c82812e69346a399909859 100644 (file)
@@ -91,7 +91,7 @@ extern Int    VG_(unlink) ( const HChar* file_name );
 
 extern SysRes VG_(poll) (struct vki_pollfd *fds, Int nfds, Int timeout);
 
-extern Int    VG_(readlink)( const HChar* path, HChar* buf, UInt bufsize );
+extern SSizeT VG_(readlink)( const HChar* path, HChar* buf, SizeT bufsiz);
 
 #if defined(VGO_linux)
 extern Int    VG_(getdents64)( Int fd, struct vki_dirent64 *dirp, UInt count );