Fixed bug #606: Empty path in yaz_filepath_resolve reads from / .
authorAdam Dickmeiss <adam@indexdata.dk>
Thu, 8 Jun 2006 10:26:10 +0000 (10:26 +0000)
committerAdam Dickmeiss <adam@indexdata.dk>
Thu, 8 Jun 2006 10:26:10 +0000 (10:26 +0000)
Added a test for it : tst_tpath.

src/tpath.c
test/.cvsignore
test/Makefile.am
test/tst_tpath.c [new file with mode: 0644]

index 8c33904..0fdf529 100644 (file)
@@ -2,7 +2,7 @@
  * Copyright (C) 1995-2006, Index Data ApS
  * See the file LICENSE for details.
  *
- * $Id: tpath.c,v 1.9 2006-04-27 08:39:05 adam Exp $
+ * $Id: tpath.c,v 1.10 2006-06-08 10:26:10 adam Exp $
  */
 /**
  * \file tpath.c
@@ -62,15 +62,17 @@ char *yaz_filepath_resolve(const char *fname, const char *path,
                 len = path_sep - path;
             else
                 len = strlen(path);
+            /* is path is relative and base is to be used */
             if (!strchr ("/\\", *path) && base)
             {
+                /* yes: make base the first part */
                 strcpy (fullpath, base);
                 slen = strlen(fullpath);
                 fullpath[slen++] = '/';
             }
             memcpy (fullpath+slen, path, len);
             slen += len;
-            if (!strchr("/\\", fullpath[slen-1]))
+            if (slen > 0 && !strchr("/\\", fullpath[slen-1]))
                 fullpath[slen++] = '/';
         }
         strcpy (fullpath+slen, fname);
index e630501..0746d53 100644 (file)
@@ -20,4 +20,5 @@ tstpquery
 tst_filepath
 tst_record_conv
 tst_retrieval
+tst_tpath
 nfatest1
index 205e189..7059259 100644 (file)
@@ -1,10 +1,10 @@
 ## Copyright (C) 1994-2006, Index Data ApS
 ## All rights reserved.
-## $Id: Makefile.am,v 1.18 2006-05-04 20:00:45 adam Exp $
+## $Id: Makefile.am,v 1.19 2006-06-08 10:26:10 adam Exp $
 
 check_PROGRAMS = tsticonv tstnmem tstmatchstr tstwrbuf tstodr tstccl tstlog \
  tstsoap1 tstsoap2 tstodrstack tstlogthread tstxmlquery tstpquery nfatest1 \
- tst_filepath tst_record_conv tst_retrieval
+ tst_filepath tst_record_conv tst_retrieval tst_tpath
 check_SCRIPTS = tstcql.sh tstmarciso.sh tstmarcxml.sh
 
 TESTS = $(check_PROGRAMS) $(check_SCRIPTS)
@@ -54,3 +54,4 @@ nfatest1_SOURCES = nfatest1.c
 tst_filepath_SOURCES = tst_filepath.c
 tst_record_conv_SOURCES = tst_record_conv.c
 tst_retrieval_SOURCES = tst_retrieval.c
+tst_tpath_SOURCES = tst_tpath.c
diff --git a/test/tst_tpath.c b/test/tst_tpath.c
new file mode 100644 (file)
index 0000000..eca34a1
--- /dev/null
@@ -0,0 +1,51 @@
+/*
+ * Copyright (C) 2005-2006, Index Data ApS
+ * See the file LICENSE for details.
+ *
+ * $Id: tst_tpath.c,v 1.1 2006-06-08 10:26:10 adam Exp $
+ *
+ */
+#include <yaz/tpath.h>
+#include <yaz/test.h>
+#include <string.h>
+#include <yaz/log.h>
+
+#if HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+
+static void tst_tpath(void)
+{
+    char fullpath[1024];
+
+    YAZ_CHECK(!yaz_filepath_resolve("etc", 0, 0, fullpath));
+    YAZ_CHECK(!yaz_filepath_resolve("etc", "", 0, fullpath)); /* bug #606 */
+    YAZ_CHECK(!yaz_filepath_resolve("etc", ".", 0, fullpath));
+    YAZ_CHECK(!yaz_filepath_resolve("does_not_exist", "", 0, fullpath));
+    YAZ_CHECK(!yaz_filepath_resolve("does_not_exist", ".", 0, fullpath));
+    YAZ_CHECK(yaz_filepath_resolve("tst_tpath", 0, 0, fullpath));
+    YAZ_CHECK(yaz_filepath_resolve("tst_tpath", "", 0, fullpath));
+    YAZ_CHECK(yaz_filepath_resolve("tst_tpath", ".", 0, fullpath));
+    YAZ_CHECK(!yaz_filepath_resolve("tst_tpath", "unknown_dir", 0, fullpath));
+    YAZ_CHECK(yaz_filepath_resolve("tst_tpath", "unknown_dir:.", 0, fullpath));
+    YAZ_CHECK(yaz_filepath_resolve("tst_tpath", "unknown_dir:", 0, fullpath));
+    YAZ_CHECK(!yaz_filepath_resolve("tst_tpath", "unknown_dir:c:", 0, fullpath));
+    YAZ_CHECK(!yaz_filepath_resolve("tst_tpath", "unknown_dir:c:\\other", 0, fullpath));
+}
+
+int main(int argc, char **argv)
+{
+    YAZ_CHECK_INIT(argc, argv);
+    tst_tpath();
+    YAZ_CHECK_TERM;
+}
+
+/*
+ * Local variables:
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * End:
+ * vim: shiftwidth=4 tabstop=8 expandtab
+ */
+