]> git.saurik.com Git - apt.git/commitdiff
ensure Cnf::FindFile doesn't return files below /dev/null
authorDavid Kalnischkies <david@kalnischkies.de>
Tue, 19 Jul 2016 16:05:13 +0000 (18:05 +0200)
committerDavid Kalnischkies <david@kalnischkies.de>
Tue, 19 Jul 2016 16:05:13 +0000 (18:05 +0200)
Very unlikely, but if the parent is /dev/null, the child empty and the
grandchild a value we returned /dev/null/value which doesn't exist, so
hardly a problem, but for best operability we should be consistent in
our work and return /dev/null always.

apt-pkg/contrib/configuration.cc
apt-pkg/contrib/fileutl.cc
apt-pkg/contrib/fileutl.h
test/libapt/configuration_test.cc

index 3cb7fde06507740ea8c8886ddd12444eb9cf5f76..9007bf9ec36a2f96139417ea5ceb4404801455cc 100644 (file)
@@ -225,15 +225,7 @@ string Configuration::FindFile(const char *Name,const char *Default) const
       }
       result.append(val);
    }
-
-   // do some normalisation by removing // and /./ from the path
-   size_t found = string::npos;
-   while ((found = result.find("/./")) != string::npos)
-      result.replace(found, 3, "/");
-   while ((found = result.find("//")) != string::npos)
-      result.replace(found, 2, "/");
-
-   return result;
+   return flNormalize(result);
 }
                                                                        /*}}}*/
 // Configuration::FindDir - Find a directory name                      /*{{{*/
index e410f52d6f892e3648e1655cb2b81d9c1ec3c12b..fa0930dff9e717a89a587590f304d06afbdab3bc 100644 (file)
@@ -694,6 +694,25 @@ string flAbsPath(string File)
    return AbsPath;
 }
                                                                        /*}}}*/
+std::string flNormalize(std::string file)                              /*{{{*/
+{
+   if (file.empty())
+      return file;
+   // do some normalisation by removing // and /./ from the path
+   size_t found = string::npos;
+   while ((found = file.find("/./")) != string::npos)
+      file.replace(found, 3, "/");
+   while ((found = file.find("//")) != string::npos)
+      file.replace(found, 2, "/");
+
+   if (APT::String::Startswith(file, "/dev/null"))
+   {
+      file.erase(strlen("/dev/null"));
+      return file;
+   }
+   return file;
+}
+                                                                       /*}}}*/
 // SetCloseExec - Set the close on exec flag                           /*{{{*/
 // ---------------------------------------------------------------------
 /* */
index f33f7804b0d65f1574f462c7bbbb4bffd5fe3233..c13613171e96e60fe52a059915609a1295d87543 100644 (file)
@@ -234,6 +234,8 @@ std::string flCombine(std::string Dir,std::string File);
 /** \brief Takes a file path and returns the absolute path
  */
 std::string flAbsPath(std::string File);
+/** \brief removes superfluous /./ and // from path */
+APT_HIDDEN std::string flNormalize(std::string file);
 
 // simple c++ glob
 std::vector<std::string> Glob(std::string const &pattern, int flags=0);
index 9fb580a011f7c79f68e7a316e3a876458816dbe3..bdc17cbf4ca0f1d32e2c8d19b78df8c5b7df725f 100644 (file)
@@ -112,6 +112,34 @@ TEST(ConfigurationTest,DirsAndFiles)
        EXPECT_EQ("/rootdir/dev/null", Cnf.FindDir("Dir::State"));
        EXPECT_EQ("/rootdir/dev/null", Cnf.FindDir("Dir::State::lists"));
 }
+TEST(ConfigurationTest,DevNullInPaths)
+{
+       Configuration Cnf;
+       EXPECT_EQ("", Cnf.FindFile("Dir"));
+       EXPECT_EQ("", Cnf.FindFile("Dir::State"));
+       EXPECT_EQ("", Cnf.FindFile("Dir::State::status"));
+       Cnf.Set("Dir::State", "/dev/null");
+       EXPECT_EQ("/dev/null", Cnf.FindFile("Dir::State"));
+       Cnf.Set("Dir", "/");
+       Cnf.Set("Dir::State::status", "status");
+       EXPECT_EQ("/dev/null", Cnf.FindFile("Dir::State"));
+       EXPECT_EQ("/dev/null", Cnf.FindFile("Dir::State::status"));
+       Cnf.Set("Dir::State", "");
+       EXPECT_EQ("", Cnf.FindFile("Dir::State"));
+       EXPECT_EQ("/status", Cnf.FindFile("Dir::State::status"));
+       Cnf.Set("Dir", "/dev/null");
+       EXPECT_EQ("/dev/null", Cnf.FindFile("Dir"));
+       EXPECT_EQ("", Cnf.FindFile("Dir::State"));
+       EXPECT_EQ("/dev/null", Cnf.FindFile("Dir::State::status"));
+       Cnf.Set("Dir", "/rootdir");
+       EXPECT_EQ("/rootdir", Cnf.FindFile("Dir"));
+       EXPECT_EQ("", Cnf.FindFile("Dir::State"));
+       EXPECT_EQ("/rootdir/status", Cnf.FindFile("Dir::State::status"));
+       Cnf.Set("Dir::State::status", "/foo/status");
+       EXPECT_EQ("/rootdir", Cnf.FindFile("Dir"));
+       EXPECT_EQ("", Cnf.FindFile("Dir::State"));
+       EXPECT_EQ("/foo/status", Cnf.FindFile("Dir::State::status"));
+}
 TEST(ConfigurationTest,Vector)
 {
        Configuration Cnf;