Patchwork [libfortran] PR 51803 getcwd() failure

login
register
mail settings
Submitter Janne Blomqvist
Date Jan. 11, 2012, 1:08 p.m.
Message ID <CAO9iq9GwWahycSQ6dmcbiLjftWRqxyBO5o5y69ahnUQ-Uf7p5Q@mail.gmail.com>
Download mbox | patch
Permalink /patch/135383/
State New
Headers show

Comments

Janne Blomqvist - Jan. 11, 2012, 1:08 p.m.
On Wed, Jan 11, 2012 at 14:55, Tobias Burnus <burnus@net-b.de> wrote:
> Dear all,
>
> this is a follow up patch, which I think provides a better handling if
> either getcwd fails or is not availble - or if the pathname in argv[0]
> already is an absolute patch, in which case concatenating
> current-working-directory + '/' + argv[0] does not really make sense.

Checking for an absolute path is already done a few lines up. So if
you prefer the kind of approach that you have in your patch, IMHO a
more correct patch would be



Also, I removed the memset() call as superfluous; getcwd() makes sure
that the buffer is null terminated or it will return NULL instead of a
pointer to the string.

For my part this fixed patch would be Ok.

Patch

Index: main.c
===================================================================
--- main.c	(revision 183091)
+++ main.c	(working copy)
@@ -106,22 +106,26 @@ 
 #endif

   /* On the simulator argv is not set.  */
-  if (argv0 == NULL || argv0[0] == '/')
+  if (argv0 == NULL || argv0[0] == DIR_SEPARATOR)
     {
       exe_path = argv0;
       please_free_exe_path_when_done = 0;
       return;
     }

-  memset (buf, 0, sizeof (buf));
 #ifdef HAVE_GETCWD
   cwd = getcwd (buf, sizeof (buf));
-  if (!cwd)
-    cwd = ".";
 #else
-  cwd = ".";
+  cwd = NULL;
 #endif

+  if (!cwd)
+    {
+      exe_path = argv0;
+      please_free_exe_path_when_done = 0;
+      return;
+    }
+
   /* exe_path will be cwd + "/" + argv[0] + "\0".  This will not work
      if the executable is not in the cwd, but at this point we're out
      of better ideas.  */