[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[gnuastro-commits] master edc8c5e: Correctly add suffix when there wasn'

From: Mohammad Akhlaghi
Subject: [gnuastro-commits] master edc8c5e: Correctly add suffix when there wasn't any in input
Date: Thu, 25 Aug 2016 15:27:23 +0000 (UTC)

branch: master
commit edc8c5e20ba828dc25a3e2dcc69c7d0d517ec340
Author: Mohammad Akhlaghi <address@hidden>
Commit: Mohammad Akhlaghi <address@hidden>

    Correctly add suffix when there wasn't any in input
    The `gal_checkset_automatic_output' plays an important role in setting the
    output files names when the user hasn't explicitly set an output name. If
    the provided name didn't have a suffix, then this function would fail to
    add a suffix to it.
    The main cause of the problem was a `strcpy(out, inname)' call immediately
    before the loop searching for an existing suffix. However, the loop was
    assuming that the input and output strings are concatenated. In practice,
    `strcpy' was redundant, so by removing it (and doing the search from the
    end of the input string), we are able to achieve the same result while also
    removing this bug. The situation is fully explained in the comments during
    the loop.
    This fixes bug #48899.
 lib/checkset.c |   51 +++++++++++++++++++++++++++++++++------------------
 1 file changed, 33 insertions(+), 18 deletions(-)

diff --git a/lib/checkset.c b/lib/checkset.c
index 1ad53e3..33f725d 100644
--- a/lib/checkset.c
+++ b/lib/checkset.c
@@ -750,32 +750,47 @@ gal_checkset_dir_0_file_1(char *name, int dontdelete)
    be removed and the given suffix will be put in the end. */
 gal_checkset_automatic_output(char *inname, char *suffix, int removedirinfo,
-                int dontdelete, char **outname)
+                              int dontdelete, char **outname)
   char *out;
   size_t i, l, offset=0;
-  /* Note that we are just using malloccat here to allocate the right
-     space! The contents of the allocated space will be changed after
-     this.*/
+  /* Merge the contents of the input name and suffix name (while also
+     allocating the necessary space).*/
   out=gal_checkset_malloc_cat(inname, suffix);
-  /* Put the input in the space and remove all elements including and after
-     '.'.  Note that if there is no '.' in the name,
-     gal_checkset_malloc_cat has already appended inname and suffix.*/
-  l=strlen(inname);
-  strcpy(out, inname);
-  for(i=l;i!=0;--i)
+  /* If there is actually a suffix, replace it with the (possibly) existing
+     suffix. */
+  if(suffix[0]!='\0')
-      /* We don't want to remove any '.' in a directory name so if a
-         '/' is encountered in our search from the end of the file
-         name, we won't continue. */
-      if(out[i]=='/') break;
-      else if(out[i]=='.')
+      /* Start from the end of the input array*/
+      l=strlen(inname);
+      for(i=l-1;i!=0;--i)
-          out[i]='\0';
-          strcat(out, suffix);
-          break;
+          /* We don't want to touch anything before a `/' (directory
+             names). We are only concerned with file names here. */
+          if(out[i]=='/')
+            {
+              /* When `/' is the last input character, then the input is
+                 clearly not a filename, but a directory name. In this
+                 case, adding a suffix is meaningless (a suffix belongs to
+                 a filename for Gnuastro's tools). So close the string
+                 after the `/' and leave the loop. However, if the `/'
+                 isn't the last input name charector, there is probably a
+                 filename (without a "." suffix), so break from the
+                 loop. No further action is required, since we initially
+                 allocated the necessary space and concatenated the input
+                 and suffix arrays. */
+              if(i==l-1)
+                out[i+1]='\0';
+              break;
+            }
+          else if(out[i]=='.')
+            {
+              out[i]='\0';
+              strcat(out, suffix);
+              break;
+            }

reply via email to

[Prev in Thread] Current Thread [Next in Thread]