[devel] [APT PATCH] rpmSingle{Pkg, Src}Index::ArchiveURI(): avoid cases with undefined behavior

Ivan Zakharyaschev imz на altlinux.org
Вс Фев 16 04:09:14 MSK 2020


Two cases of UB are avoided with such a rewrite:

* getcwd(2) returned NULL. Constructing a string from NULL is UB.
  (Such string was passed as an argument to flCombine().)
  Now, SafeGetCwd() (in fileutl.cc) returns "/" in such cases;
  if you consider SafeGetCwd()'s implementation not to be reasonable,
  rewrite it (just at a single place).

* File.length() < 2. Since File was a non-const string,
  File[File.length()] might be UB before C++11. Now, File is const, and
  it is guaranteed that File[File.length()] == 0.

This change was inspired by:

commit c138c48501381d26872a6b18aa1fb7af9f0300b2
Author: Aleksei Nikiforov <darktemplar на altlinux.org>
Date:   Mon Jun 10 18:10:30 2019 +0300

    RPM ArchiveURI: check file length before using it
---
 apt/apt-pkg/rpm/rpmindexfile.cc | 24 +++++++++---------------
 1 file changed, 9 insertions(+), 15 deletions(-)

diff --git a/apt/apt-pkg/rpm/rpmindexfile.cc b/apt/apt-pkg/rpm/rpmindexfile.cc
index 901ba77dd..ad6899a08 100644
--- a/apt/apt-pkg/rpm/rpmindexfile.cc
+++ b/apt/apt-pkg/rpm/rpmindexfile.cc
@@ -511,26 +511,20 @@ unsigned long rpmSrcDirIndex::Size() const
 
 // SinglePkgIndex::ArchiveURI - URI for the archive       	        /*{{{*/
 // ---------------------------------------------------------------------
-string rpmSinglePkgIndex::ArchiveURI(string File) const
+string rpmSinglePkgIndex::ArchiveURI(const string File) const
 {
-   char *cwd = getcwd(NULL,0);
-   if (File[0] == '.' && File[1] == '/')
-      File = string(File, 2);
-   string URI = "file://"+flCombine(cwd, File);
-   free(cwd);
-   return URI;
+   return "file://"+
+      flCombine(SafeGetCWD(),
+                (File[0] == '.' && File[1] == '/') ? string(File, 2) : File);
 }
 									/*}}}*/
 // SinglePkgIndex::ArchiveURI - URI for the archive       	        /*{{{*/
 // ---------------------------------------------------------------------
-string rpmSingleSrcIndex::ArchiveURI(string File) const
-{
-   char *cwd = getcwd(NULL,0);
-   if (File[0] == '.' && File[1] == '/')
-      File = string(File, 2);
-   string URI = "file://"+flCombine(cwd, File);
-   free(cwd);
-   return URI;
+string rpmSingleSrcIndex::ArchiveURI(const string File) const
+{
+   return "file://"+
+      flCombine(SafeGetCWD(),
+                (File[0] == '.' && File[1] == '/') ? string(File, 2) : File);
 }
 									/*}}}*/
 
-- 
2.21.0


Подробная информация о списке рассылки Devel