]> git.ipfire.org Git - thirdparty/valgrind.git/commitdiff
Merge 14206,14207,14261,14577,14578 from BUF_REMOVAL branch to trunk.
authorFlorian Krohm <florian@eich-krohm.de>
Sat, 27 Sep 2014 17:42:07 +0000 (17:42 +0000)
committerFlorian Krohm <florian@eich-krohm.de>
Sat, 27 Sep 2014 17:42:07 +0000 (17:42 +0000)
This changes VG_(record_startup_wd) to dynamically allocate a large
enough buffer for the directory name. As the dynamic memory manager has
started up a while ago, this is quite safe. Also rewrite VG_(get_startup_wd)
to simply return the directory name. No more messing with copying it
around. Adapt call sites.

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

coregrind/m_commandline.c
coregrind/m_libcfile.c
coregrind/m_main.c
coregrind/m_options.c
include/pub_tool_libcfile.h

index 9f020fd5fcad33b5c55b2c18c7a9144ec400b8df..3a4c271429e72bd55e0fe7d88e2750223613c898 100644 (file)
@@ -218,9 +218,8 @@ void VG_(split_up_argv)( Int argc, HChar** argv )
       // 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(".") );
       }
 
index 9459f7a4f6e011b72f25c1059ec9c5606f088612..ceb6bdc1efa72a7a63de69f57ad00cb91e09f0a9 100644 (file)
@@ -418,14 +418,12 @@ Int VG_(unlink) ( const HChar* file_name )
    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
-   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;
+/* 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. */
+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
@@ -434,37 +432,39 @@ static Bool  startup_wd_acquired = False;
    there is a problem. */
 Bool VG_(record_startup_wd) ( void )
 {
-   const Int szB = sizeof(startup_wd);
    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;
+   SizeT szB = 0;
+   do { 
+      szB += 500;
+      startup_wd = VG_(realloc)("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* wd = NULL;
+   { HChar  envvar[100];   // large enough
+     HChar* wd;
      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_(malloc)("startup_wd", need);
+     VG_(strcpy)(startup_wd, wd);
      startup_wd_acquired = True;
      return True;
    }
@@ -473,16 +473,12 @@ Bool VG_(record_startup_wd) ( void )
 #  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)
index 94dc4158a82efb25158700d4f28cb1e2f04ce1d1..1499c8ab355542225ddb94d5a2cd7bc9bbc0d510 100644 (file)
@@ -1763,12 +1763,7 @@ Int valgrind_main ( Int argc, HChar **argv, HChar **envp )
         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:
index 0710c8d6733c383c358176de01c0a9721d921e49..3140b810aa98a22369b6acbf20dcbe0b3414f2a4 100644 (file)
@@ -142,12 +142,11 @@ UInt   VG_(clo_unw_stack_scan_frames) = 5;
 // 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);
-   vg_assert(ok);
+   base_dir = VG_(get_startup_wd)();
 
    if (VG_STREQ(format, "")) {
       // Empty name, bad.
index ea50289b2de04413e445eb89846fef11b2a1ae77..bb1a6e4f704575762e62eefaa1645dab2d4d92b4 100644 (file)
@@ -103,9 +103,9 @@ extern const HChar* VG_(dirname) ( const HChar* path );
 /* Return the name of a directory for temporary files. */
 extern const HChar* VG_(tmpdir)(void);
 
-/* 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 );
+/* Return the working directory at startup. The returned string is
+   persistent. */
+extern const HChar *VG_(get_startup_wd) ( void );
 
 #endif   // __PUB_TOOL_LIBCFILE_H