[AD] Crash in make_relative_filename under Unix

[ Thread Index | Date Index | More lists.liballeg.org/allegro-developers Archives ]


The following test case makes make_relative_filename() crash under
Unix during a call to ustrzcat() because a previous get_filename()
doesn't work on backslashes and some NULL pointers are later not
set correctly.

  #include <allegro.h>
  
  int main(void)
  {
      char buf[255];
      char path1[] = "\\long\\absolute\\path\\program.exe";
      char path2[] = "\\nice\\and\\short.txt";
      
      allegro_init();
      make_relative_filename(buf, path1, path2, sizeof(buf));
      return 0;
  }

The attached patch fixes the problem bailing out of
make_relative_filename() with a NULL return value.
Index: src/file.c
===================================================================
RCS file: /cvsroot/alleg/allegro/src/file.c,v
retrieving revision 1.60
diff -u -p -r1.60 file.c
--- src/file.c	17 Jul 2005 15:18:09 -0000	1.60
+++ src/file.c	15 Aug 2005 14:11:27 -0000
@@ -417,6 +417,13 @@ char *make_relative_filename(char *dest,
       }
    }
    else {
+      /* Bail out if previously something went wrong (ie. user supplied
+       * paths are not canonical and we can't understand them. */
+      if (!reduced_path) {
+	 free(my_path);
+	 free(my_filename);
+	 return NULL;
+      }
       /* Otherwise, we are in the latter case and need to count the number
        * of remaining directories in the reduced path and prepend the same
        * number of '../' to the reduced filename.
@@ -433,6 +440,14 @@ char *make_relative_filename(char *dest,
       usetc(dest+pos, 0);
    }
 
+   /* Bail out if previously something went wrong (ie. user supplied
+    * paths are not canonical and we can't understand them. */
+   if (!reduced_filename) {
+      free(my_path);
+      free(my_filename);
+      return NULL;
+   }
+
    ustrzcat(dest, size, reduced_filename);
    ustrzcat(dest, size, get_filename(filename));
 



Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/