From 4438f9defcd18bc1ded87f69d9ab8f6441218ec4 Mon Sep 17 00:00:00 2001 From: наб Date: Mon, 11 Nov 2024 15:43:56 +0100 Subject: Replace constant-size never-reallicated getservbyport_r() std::vector buffer with std::array --- apt-pkg/contrib/srvrec.cc | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'apt-pkg/contrib') diff --git a/apt-pkg/contrib/srvrec.cc b/apt-pkg/contrib/srvrec.cc index 4a68f3578..3ddb95abb 100644 --- a/apt-pkg/contrib/srvrec.cc +++ b/apt-pkg/contrib/srvrec.cc @@ -16,6 +16,7 @@ #include #include +#include #include #include #include @@ -47,7 +48,7 @@ bool GetSrvRecords(std::string host, int port, std::vector &Result) int res; struct servent s_ent_buf; struct servent *s_ent = nullptr; - std::vector buf(1024); + std::array buf; res = getservbyport_r(htons(port), "tcp", &s_ent_buf, buf.data(), buf.size(), &s_ent); if (res != 0 || s_ent == nullptr) -- cgit v1.2.3-70-g09d2 From 78e503bc1d79bc4fc29bc9cbe1dfb68ae4d16acf Mon Sep 17 00:00:00 2001 From: наб Date: Tue, 12 Nov 2024 13:28:04 +0100 Subject: Turn unique_ptr into real deleter types (warnings now, UB in C++20) --- apt-pkg/acquire-item.cc | 4 ++-- apt-pkg/acquire.cc | 2 +- apt-pkg/contrib/gpgv.cc | 21 ++++++++++++--------- apt-pkg/contrib/strutl.cc | 2 +- apt-pkg/contrib/strutl.h | 9 +++++++++ 5 files changed, 25 insertions(+), 13 deletions(-) (limited to 'apt-pkg/contrib') diff --git a/apt-pkg/acquire-item.cc b/apt-pkg/acquire-item.cc index b534d56a3..3016dad59 100644 --- a/apt-pkg/acquire-item.cc +++ b/apt-pkg/acquire-item.cc @@ -4065,7 +4065,7 @@ static std::string GetAuxFileNameFromURIInLists(std::string const &uri) auto const dirname = flCombine(_config->FindDir("Dir::State::lists"), "auxfiles/"); char const * const filetag = ".apt-acquire-privs-test.XXXXXX"; std::string const tmpfile_tpl = flCombine(dirname, filetag); - std::unique_ptr tmpfile { strdup(tmpfile_tpl.c_str()), std::free }; + std::unique_ptr tmpfile { strdup(tmpfile_tpl.c_str()) }; int const fd = mkstemp(tmpfile.get()); if (fd == -1) return ""; @@ -4081,7 +4081,7 @@ static std::string GetAuxFileNameFromURI(std::string const &uri) std::string tmpdir_tpl; strprintf(tmpdir_tpl, "%s/apt-auxfiles-XXXXXX", GetTempDir().c_str()); - std::unique_ptr tmpdir { strndup(tmpdir_tpl.data(), tmpdir_tpl.length()), std::free }; + std::unique_ptr tmpdir { strndup(tmpdir_tpl.data(), tmpdir_tpl.length()) }; if (mkdtemp(tmpdir.get()) == nullptr) { _error->Errno("GetAuxFileNameFromURI", "mkdtemp of %s failed", tmpdir.get()); diff --git a/apt-pkg/acquire.cc b/apt-pkg/acquire.cc index 7ebfa4ffe..f367e4310 100644 --- a/apt-pkg/acquire.cc +++ b/apt-pkg/acquire.cc @@ -605,7 +605,7 @@ static bool IsAccessibleBySandboxUser(std::string const &filename, bool const Re char const * const filetag = ".apt-acquire-privs-test.XXXXXX"; std::string const tmpfile_tpl = flCombine(dirname, filetag); - std::unique_ptr tmpfile { strdup(tmpfile_tpl.c_str()), std::free }; + std::unique_ptr tmpfile { strdup(tmpfile_tpl.c_str()) }; int const fd = mkstemp(tmpfile.get()); if (fd == -1 && errno == EACCES) return false; diff --git a/apt-pkg/contrib/gpgv.cc b/apt-pkg/contrib/gpgv.cc index 225acae88..9ab3e066e 100644 --- a/apt-pkg/contrib/gpgv.cc +++ b/apt-pkg/contrib/gpgv.cc @@ -28,14 +28,17 @@ #include /*}}}*/ -// syntactic sugar to wrap a raw pointer with a custom deleter in a std::unique_ptr -static std::unique_ptr make_unique_char(void *const str = nullptr) -{ - return {static_cast(str), &free}; +namespace { + struct FILEDeleter { + void operator()(FILE *p) { + fclose(p); + } + }; } -static std::unique_ptr make_unique_FILE(std::string const &filename, char const *const mode) + +static std::unique_ptr make_unique_FILE(std::string const &filename, char const *const mode) { - return {fopen(filename.c_str(), mode), &fclose}; + return {fopen(filename.c_str(), mode), {}}; } class LineBuffer /*{{{*/ @@ -207,9 +210,9 @@ void ExecGPGV(std::string const &File, std::string const &FileGPG, } enum { DETACHED, CLEARSIGNED } releaseSignature = (FileGPG != File) ? DETACHED : CLEARSIGNED; - auto sig = make_unique_char(); - auto data = make_unique_char(); - auto conf = make_unique_char(); + std::unique_ptr sig; + std::unique_ptr data; + std::unique_ptr conf; // Dump the configuration so apt-key picks up the correct Dir values { diff --git a/apt-pkg/contrib/strutl.cc b/apt-pkg/contrib/strutl.cc index 3689dc17a..ec61c7e2b 100644 --- a/apt-pkg/contrib/strutl.cc +++ b/apt-pkg/contrib/strutl.cc @@ -1421,7 +1421,7 @@ unsigned long RegexChoice(RxChoiceList *Rxs,const char **ListBegin, and to allow reordering of parameters */ bool iovprintf(std::ostream &out, const char *format, va_list &args, ssize_t &size) { - auto S = std::unique_ptr{static_cast(malloc(size)), &free}; + auto S = std::unique_ptr{static_cast(malloc(size))}; ssize_t const n = vsnprintf(S.get(), size, format, args); if (n > -1 && n < size) { out << S.get(); diff --git a/apt-pkg/contrib/strutl.h b/apt-pkg/contrib/strutl.h index 7cf9b456d..152477eee 100644 --- a/apt-pkg/contrib/strutl.h +++ b/apt-pkg/contrib/strutl.h @@ -31,6 +31,15 @@ #include "macros.h" +namespace { + struct FreeDeleter { + void operator()(void *p) { + free(p); + } + }; +} + + namespace APT { namespace String { APT_PUBLIC std::string Strip(const std::string &s); -- cgit v1.2.3-70-g09d2 From 9f81a8dd0ee585a360ae0ae7786959630a8d0be0 Mon Sep 17 00:00:00 2001 From: наб Date: Mon, 11 Nov 2024 16:17:35 +0100 Subject: dpkgpm: lift out CopyIndented() and make_unique_*() for FILEs; use getline() for reading lines instead of 1k buffer --- apt-pkg/contrib/fileutl.h | 29 +++++++++++++++++++ apt-pkg/contrib/gpgv.cc | 12 -------- apt-pkg/deb/dpkgpm.cc | 74 +++++++++++++++++------------------------------ 3 files changed, 55 insertions(+), 60 deletions(-) (limited to 'apt-pkg/contrib') diff --git a/apt-pkg/contrib/fileutl.h b/apt-pkg/contrib/fileutl.h index 11f4871f6..938d1d8bc 100644 --- a/apt-pkg/contrib/fileutl.h +++ b/apt-pkg/contrib/fileutl.h @@ -29,6 +29,7 @@ #include #include +#include #include #include #include @@ -284,4 +285,32 @@ APT_HIDDEN bool OpenConfigurationFileFd(std::string const &File, FileFd &Fd); APT_HIDDEN int Inhibit(const char *what, const char *who, const char *why, const char *mode); + +namespace { + struct FILEFcloseDeleter { + void operator()(FILE *p) { + fclose(p); + } + }; + struct FILEPcloseDeleter { + void operator()(FILE *p) { + pclose(p); + } + }; + + [[maybe_unused]] std::unique_ptr make_unique_FILE(const char *const filename, char const *const mode) + { + return {fopen(filename, mode), {}}; + } + [[maybe_unused]] std::unique_ptr make_unique_FILE(std::string const &filename, char const *const mode) + { + return make_unique_FILE(filename.c_str(), mode); + } + + [[maybe_unused]] std::unique_ptr make_unique_popen(const char *program, char const *const mode) + { + return {popen(program, mode), {}}; + } +} + #endif diff --git a/apt-pkg/contrib/gpgv.cc b/apt-pkg/contrib/gpgv.cc index 9ab3e066e..c5ea19aa8 100644 --- a/apt-pkg/contrib/gpgv.cc +++ b/apt-pkg/contrib/gpgv.cc @@ -28,18 +28,6 @@ #include /*}}}*/ -namespace { - struct FILEDeleter { - void operator()(FILE *p) { - fclose(p); - } - }; -} - -static std::unique_ptr make_unique_FILE(std::string const &filename, char const *const mode) -{ - return {fopen(filename.c_str(), mode), {}}; -} class LineBuffer /*{{{*/ { diff --git a/apt-pkg/deb/dpkgpm.cc b/apt-pkg/deb/dpkgpm.cc index 82035ee65..31f81af7c 100644 --- a/apt-pkg/deb/dpkgpm.cc +++ b/apt-pkg/deb/dpkgpm.cc @@ -33,6 +33,7 @@ #include #include #include +#include #include #include #include @@ -2233,6 +2234,27 @@ void pkgDPkgPM::Reset() { List.erase(List.begin(),List.end()); } + +template +struct AptScopeWrapper { + F func; + ~AptScopeWrapper() { func(); } +}; +template +AptScopeWrapper(F) -> AptScopeWrapper; + +static void CopyIndented(const char *header, FILE *from, FILE *to) +{ + if (!from) + return; + + fputs(header, to); + char *line{}; + size_t linelen; + AptScopeWrapper line_deleter{[&] { free(line); }}; + while (getline(&line, &linelen, from) != -1) + fprintf(to, " %s", line); +} /*}}}*/ // pkgDpkgPM::WriteApportReport - write out error report pkg failure /*{{{*/ // --------------------------------------------------------------------- @@ -2406,35 +2428,12 @@ void pkgDPkgPM::WriteApportReport(const char *pkgpath, const char *errormsg) // attach terminal log it if we have it string logfile_name = _config->FindFile("Dir::Log::Terminal", "/dev/null"); if (logfile_name != "/dev/null") - { - FILE *log = NULL; - - fprintf(report, "DpkgTerminalLog:\n"); - log = fopen(logfile_name.c_str(),"r"); - if(log != NULL) - { - char buf[1024]; - while( fgets(buf, sizeof(buf), log) != NULL) - fprintf(report, " %s", buf); - fprintf(report, " \n"); - fclose(log); - } - } + CopyIndented("DpkgTerminalLog:\n", make_unique_FILE(logfile_name, "r").get(), report); // attach history log it if we have it string histfile_name = _config->FindFile("Dir::Log::History", "/dev/null"); if (histfile_name != "/dev/null") - { - fprintf(report, "DpkgHistoryLog:\n"); - FILE* log = fopen(histfile_name.c_str(),"r"); - if(log != NULL) - { - char buf[1024]; - while( fgets(buf, sizeof(buf), log) != NULL) - fprintf(report, " %s", buf); - fclose(log); - } - } + CopyIndented("DpkgHistoryLog:\n", make_unique_FILE(histfile_name, "r").get(), report); // log the ordering, see dpkgpm.h and the "Ops" enum there fprintf(report, "AptOrdering:\n"); @@ -2458,32 +2457,11 @@ void pkgDPkgPM::WriteApportReport(const char *pkgpath, const char *errormsg) // attach dmesg log (to learn about segfaults) if (FileExists("/bin/dmesg")) - { - fprintf(report, "Dmesg:\n"); - FILE *log = popen("/bin/dmesg","r"); - if(log != NULL) - { - char buf[1024]; - while( fgets(buf, sizeof(buf), log) != NULL) - fprintf(report, " %s", buf); - pclose(log); - } - } + CopyIndented("Dmesg:\n", make_unique_popen("/bin/dmesg","r").get(), report); // attach df -l log (to learn about filesystem status) if (FileExists("/bin/df")) - { - - fprintf(report, "Df:\n"); - FILE *log = popen("/bin/df -l -x squashfs","r"); - if(log != NULL) - { - char buf[1024]; - while( fgets(buf, sizeof(buf), log) != NULL) - fprintf(report, " %s", buf); - pclose(log); - } - } + CopyIndented("Df:\n", make_unique_popen("/bin/df -l -x squashfs","r").get(), report); fclose(report); -- cgit v1.2.3-70-g09d2