Author: florian
Date: Tue Jul 29 22:42:30 2014
New Revision: 14206
Log:
Avoid fixed size buffer startup_wd for VG_(record_startup_wd).
Change VG_(get_startup_wd) to simply return the string.
Fix call sites.
We could even fold both functions into a single one with the
benefit of giving the two associated global variables function
scope.
Modified:
branches/BUF_REMOVAL/coregrind/m_commandline.c
branches/BUF_REMOVAL/coregrind/m_libcfile.c
branches/BUF_REMOVAL/coregrind/m_main.c
branches/BUF_REMOVAL/coregrind/m_options.c
branches/BUF_REMOVAL/include/pub_tool_libcfile.h
Modified: branches/BUF_REMOVAL/coregrind/m_commandline.c
==============================================================================
--- branches/BUF_REMOVAL/coregrind/m_commandline.c (original)
+++ branches/BUF_REMOVAL/coregrind/m_commandline.c Tue Jul 29 22:42:30 2014
@@ -222,9 +222,8 @@
// Don't read ./.valgrindrc if "." is the same as "$HOME", else its
// contents will be applied twice. (bug #142488)
if (home) {
- HChar cwd[VKI_PATH_MAX+1];
- Bool cwd_ok = VG_(get_startup_wd)(cwd, VKI_PATH_MAX);
- f2_clo = ( (cwd_ok && VG_STREQ(home, cwd))
+ const HChar *cwd = VG_(get_startup_wd)();
+ f2_clo = ( VG_STREQ(home, cwd)
? NULL : read_dot_valgrindrc(".") );
}
Modified: branches/BUF_REMOVAL/coregrind/m_libcfile.c
==============================================================================
--- branches/BUF_REMOVAL/coregrind/m_libcfile.c (original)
+++ branches/BUF_REMOVAL/coregrind/m_libcfile.c Tue Jul 29 22:42:30 2014
@@ -417,14 +417,13 @@
return sr_isError(res) ? (-1) : 0;
}
-/* The working directory at startup. AIX doesn't provide an easy
- system call to do getcwd, but fortunately we don't need arbitrary
- getcwd support. All that is really needed is to note the cwd at
+/* The working directory at startup.
+ All that is really needed is to note the cwd at
process startup. Hence VG_(record_startup_wd) notes it (in a
platform dependent way) and VG_(get_startup_wd) produces the noted
value. Hence: */
-static HChar startup_wd[VKI_PATH_MAX];
-static Bool startup_wd_acquired = False;
+static HChar *startup_wd;
+static Bool startup_wd_acquired = False;
/* Record the process' working directory at startup. Is intended to
be called exactly once, at startup, before the working directory
@@ -433,37 +432,40 @@
there is a problem. */
Bool VG_(record_startup_wd) ( void )
{
- const Int szB = sizeof(startup_wd);
+ Int szB = VKI_PATH_MAX;
vg_assert(!startup_wd_acquired);
- vg_assert(szB >= 512 && szB <= 16384/*let's say*/); /* stay sane */
- VG_(memset)(startup_wd, 0, szB);
+
# if defined(VGO_linux)
/* Simple: just ask the kernel */
- { SysRes res
- = VG_(do_syscall2)(__NR_getcwd, (UWord)startup_wd, szB-1);
- vg_assert(startup_wd[szB-1] == 0);
- if (sr_isError(res)) {
- return False;
- } else {
- startup_wd_acquired = True;
- return True;
- }
- }
+ SysRes res;
+ do {
+ szB += VKI_PATH_MAX;
+ startup_wd =
+ VG_(arena_realloc)(VG_AR_CORE, "startup_wd", startup_wd, szB);
+ VG_(memset)(startup_wd, 0, szB);
+ res = VG_(do_syscall2)(__NR_getcwd, (UWord)startup_wd, szB-1);
+ } while (sr_isError(res));
+
+ vg_assert(startup_wd[szB-1] == 0);
+ startup_wd_acquired = True;
+ return True;
+
# elif defined(VGO_darwin)
/* We can't ask the kernel, so instead rely on launcher-*.c to
tell us the startup path. Note the env var is keyed to the
parent's PID, not ours, since our parent is the launcher
process. */
- { HChar envvar[100];
+ { HChar envvar[100]; // large enough
HChar* wd = NULL;
VG_(memset)(envvar, 0, sizeof(envvar));
VG_(sprintf)(envvar, "VALGRIND_STARTUP_PWD_%d_XYZZY",
(Int)VG_(getppid)());
wd = VG_(getenv)( envvar );
- if (wd == NULL || (1+VG_(strlen)(wd) >= szB))
+ if (wd == NULL)
return False;
- VG_(strncpy_safely)(startup_wd, wd, szB);
- vg_assert(startup_wd[szB-1] == 0);
+ SizeT need = VG_(strlen)(wd) + 1;
+ startup_wd = VG_(arena_malloc)(VG_AR_CORE, "startup_wd", need);
+ VG_(strcpy)(startup_wd, wd);
startup_wd_acquired = True;
return True;
}
@@ -472,16 +474,12 @@
# endif
}
-/* Copy the previously acquired startup_wd into buf[0 .. size-1],
- or return False if buf isn't big enough. */
-Bool VG_(get_startup_wd) ( HChar* buf, SizeT size )
+/* Return the previously acquired startup_wd. */
+const HChar *VG_(get_startup_wd) ( void )
{
vg_assert(startup_wd_acquired);
- vg_assert(startup_wd[ sizeof(startup_wd)-1 ] == 0);
- if (1+VG_(strlen)(startup_wd) >= size)
- return False;
- VG_(strncpy_safely)(buf, startup_wd, size);
- return True;
+
+ return startup_wd;
}
SysRes VG_(poll) (struct vki_pollfd *fds, Int nfds, Int timeout)
Modified: branches/BUF_REMOVAL/coregrind/m_main.c
==============================================================================
--- branches/BUF_REMOVAL/coregrind/m_main.c (original)
+++ branches/BUF_REMOVAL/coregrind/m_main.c Tue Jul 29 22:42:30 2014
@@ -1743,12 +1743,7 @@
VG_(err_config_error)( "Can't establish current working "
"directory at startup\n");
}
- { HChar buf[VKI_PATH_MAX+1];
- Bool ok = VG_(get_startup_wd)( buf, sizeof(buf) );
- vg_assert(ok);
- buf[VKI_PATH_MAX] = 0;
- VG_(debugLog)(1, "main", "... %s\n", buf );
- }
+ VG_(debugLog)(1, "main", "... %s\n", VG_(get_startup_wd)() );
//============================================================
// Command line argument handling order:
Modified: branches/BUF_REMOVAL/coregrind/m_options.c
==============================================================================
--- branches/BUF_REMOVAL/coregrind/m_options.c (original)
+++ branches/BUF_REMOVAL/coregrind/m_options.c Tue Jul 29 22:42:30 2014
@@ -142,12 +142,11 @@
// expanding %p and %q entries. Returns a new, malloc'd string.
HChar* VG_(expand_file_name)(const HChar* option_name, const HChar* format)
{
- static HChar base_dir[VKI_PATH_MAX];
+ const HChar *base_dir;
Int len, i = 0, j = 0;
HChar* out;
- Bool ok = VG_(get_startup_wd)(base_dir, VKI_PATH_MAX);
- tl_assert(ok);
+ base_dir = VG_(get_startup_wd)();
if (VG_STREQ(format, "")) {
// Empty name, bad.
Modified: branches/BUF_REMOVAL/include/pub_tool_libcfile.h
==============================================================================
--- branches/BUF_REMOVAL/include/pub_tool_libcfile.h (original)
+++ branches/BUF_REMOVAL/include/pub_tool_libcfile.h Tue Jul 29 22:42:30 2014
@@ -102,7 +102,7 @@
/* Copy the working directory at startup into buf[0 .. size-1], or return
False if buf is too small. */
-extern Bool VG_(get_startup_wd) ( HChar* buf, SizeT size );
+extern const HChar *VG_(get_startup_wd) ( void );
#endif // __PUB_TOOL_LIBCFILE_H
|