aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorflorian <florian@a5019735-40e9-0310-863c-91ae7b9d1cf9>2014-10-14 21:01:33 +0000
committerflorian <florian@a5019735-40e9-0310-863c-91ae7b9d1cf9>2014-10-14 21:01:33 +0000
commita175ffb9ffa4fd250611582a611333d11c835de2 (patch)
tree720b1aadfb578609d385d45826a4f6345ba35b33
parentcb26caefec9abdb5ac84f1c485d2189510a354a9 (diff)
downloadvalgrind-a175ffb9ffa4fd250611582a611333d11c835de2.tar.gz
Merge revisions 14210 and 14626 from the BUF_REMOVAL branch to trunk.
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 a5019735-40e9-0310-863c-91ae7b9d1cf9
-rw-r--r--coregrind/m_coredump/coredump-elf.c4
-rw-r--r--coregrind/m_debuginfo/debuginfo.c21
-rw-r--r--coregrind/m_libcfile.c61
-rw-r--r--coregrind/m_syswrap/syswrap-generic.c4
-rw-r--r--coregrind/pub_core_libcfile.h2
-rw-r--r--include/pub_tool_libcfile.h2
6 files changed, 62 insertions, 32 deletions
diff --git a/coregrind/m_coredump/coredump-elf.c b/coregrind/m_coredump/coredump-elf.c
index 6f2b12c1c..a40369b3c 100644
--- a/coregrind/m_coredump/coredump-elf.c
+++ b/coregrind/m_coredump/coredump-elf.c
@@ -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 != '/')
diff --git a/coregrind/m_debuginfo/debuginfo.c b/coregrind/m_debuginfo/debuginfo.c
index c27179240..956822ed2 100644
--- a/coregrind/m_debuginfo/debuginfo.c
+++ b/coregrind/m_debuginfo/debuginfo.c
@@ -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);
diff --git a/coregrind/m_libcfile.c b/coregrind/m_libcfile.c
index 06a446167..0e01206f6 100644
--- a/coregrind/m_libcfile.c
+++ b/coregrind/m_libcfile.c
@@ -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 ); */
diff --git a/coregrind/m_syswrap/syswrap-generic.c b/coregrind/m_syswrap/syswrap-generic.c
index 8676e7c9a..7e49124e1 100644
--- a/coregrind/m_syswrap/syswrap-generic.c
+++ b/coregrind/m_syswrap/syswrap-generic.c
@@ -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;
diff --git a/coregrind/pub_core_libcfile.h b/coregrind/pub_core_libcfile.h
index d3bd68069..707399f85 100644
--- a/coregrind/pub_core_libcfile.h
+++ b/coregrind/pub_core_libcfile.h
@@ -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 );
diff --git a/include/pub_tool_libcfile.h b/include/pub_tool_libcfile.h
index bb1a6e4f7..3df5be56c 100644
--- a/include/pub_tool_libcfile.h
+++ b/include/pub_tool_libcfile.h
@@ -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 );