diff options
author | David Kalnischkies <david@kalnischkies.de> | 2016-08-23 15:11:20 +0200 |
---|---|---|
committer | Julian Andres Klode <jak@debian.org> | 2016-11-14 15:10:03 +0100 |
commit | 1e352b95182da20b1c27360b6270fcdebbe24207 (patch) | |
tree | 975caa3addea4b59ff6ad9ccf2dcc9c6138c8f6e | |
parent | 5afa62c02b03161aff2a983647a5894b309ddd6f (diff) |
prevent C++ locale number formatting in text APIs (try 3)
This time it is the formatting of floating numbers in progress
reporting with a radix charater potentially not being dot.
Followup of 7303e11ff28f920a6277c159aa46f80c007350bb. Regression of
b58e2c7c56b1416a343e81f9f80cb1f02c128e25 in so far as it exchanging
very effected with slightly less effected code.
LP: 1611010
(cherry picked from commit 0919f1df552ddf022ce4508cbf40e04eae5ef896)
-rw-r--r-- | apt-pkg/acquire.cc | 7 | ||||
-rw-r--r-- | apt-pkg/install-progress.cc | 73 |
2 files changed, 44 insertions, 36 deletions
diff --git a/apt-pkg/acquire.cc b/apt-pkg/acquire.cc index 2bc2287a1..3ce2e0d5a 100644 --- a/apt-pkg/acquire.cc +++ b/apt-pkg/acquire.cc @@ -1247,8 +1247,11 @@ bool pkgAcquireStatus::Pulse(pkgAcquire *Owner) snprintf(msg,sizeof(msg), _("Retrieving file %li of %li"), i, TotalItems); // build the status str - std::string dlstatus; - strprintf(dlstatus, "dlstatus:%ld:%.4f:%s\n", i, Percent, msg); + std::ostringstream str; + str.imbue(std::locale("C.UTF-8")); + str.precision(4); + str << "dlstatus" << ':' << std::fixed << i << ':' << Percent << ':' << msg << '\n'; + auto const dlstatus = str.str(); FileFd::Write(fd, dlstatus.data(), dlstatus.size()); } diff --git a/apt-pkg/install-progress.cc b/apt-pkg/install-progress.cc index c77c240c3..7acd61f65 100644 --- a/apt-pkg/install-progress.cc +++ b/apt-pkg/install-progress.cc @@ -77,6 +77,18 @@ void PackageManagerProgressFd::WriteToStatusFd(std::string s) FileFd::Write(OutStatusFd, s.c_str(), s.size()); } +static std::string GetProgressFdString(char const * const status, + char const * const pkg, unsigned long long Done, + unsigned long long Total, char const * const msg) +{ + float const progress{Done / static_cast<float>(Total) * 100}; + std::ostringstream str; + str.imbue(std::locale("C.UTF-8")); + str.precision(4); + str << status << ':' << pkg << ':' << std::fixed << progress << ':' << msg << '\n'; + return str.str(); +} + void PackageManagerProgressFd::StartDpkg() { if(OutStatusFd <= 0) @@ -87,10 +99,7 @@ void PackageManagerProgressFd::StartDpkg() fcntl(OutStatusFd,F_SETFD,FD_CLOEXEC); // send status information that we are about to fork dpkg - std::string status; - strprintf(status, "pmstatus:dpkg-exec:%.4f:%s\n", - (StepsDone/float(StepsTotal)*100.0), _("Running dpkg")); - WriteToStatusFd(std::move(status)); + WriteToStatusFd(GetProgressFdString("pmstatus", "dpkg-exec", StepsDone, StepsTotal, _("Running dpkg"))); } APT_CONST void PackageManagerProgressFd::Stop() @@ -102,10 +111,8 @@ void PackageManagerProgressFd::Error(std::string PackageName, unsigned int TotalSteps, std::string ErrorMessage) { - std::string status; - strprintf(status, "pmerror:%s:%.4f:%s\n", PackageName.c_str(), - (StepsDone/float(TotalSteps)*100.0), ErrorMessage.c_str()); - WriteToStatusFd(std::move(status)); + WriteToStatusFd(GetProgressFdString("pmerror", PackageName.c_str(), + StepsDone, TotalSteps, ErrorMessage.c_str())); } void PackageManagerProgressFd::ConffilePrompt(std::string PackageName, @@ -113,10 +120,8 @@ void PackageManagerProgressFd::ConffilePrompt(std::string PackageName, unsigned int TotalSteps, std::string ConfMessage) { - std::string status; - strprintf(status, "pmconffile:%s:%.4f:%s\n", PackageName.c_str(), - (StepsDone/float(TotalSteps)*100.0), ConfMessage.c_str()); - WriteToStatusFd(std::move(status)); + WriteToStatusFd(GetProgressFdString("pmconffile", PackageName.c_str(), + StepsDone, TotalSteps, ConfMessage.c_str())); } @@ -128,11 +133,8 @@ bool PackageManagerProgressFd::StatusChanged(std::string PackageName, StepsDone = xStepsDone; StepsTotal = xTotalSteps; - // build the status str - std::string status; - strprintf(status, "pmstatus:%s:%.4f:%s\n", StringSplit(PackageName, ":")[0].c_str(), - (StepsDone/float(StepsTotal)*100.0), pkg_action.c_str()); - WriteToStatusFd(std::move(status)); + WriteToStatusFd(GetProgressFdString("pmstatus", StringSplit(PackageName, ":")[0].c_str(), + StepsDone, StepsTotal, pkg_action.c_str())); if(_config->FindB("Debug::APT::Progress::PackageManagerFd", false) == true) std::cerr << "progress: " << PackageName << " " << xStepsDone @@ -156,17 +158,29 @@ void PackageManagerProgressDeb822Fd::WriteToStatusFd(std::string s) FileFd::Write(OutStatusFd, s.c_str(), s.size()); } +static std::string GetProgressDeb822String(char const * const status, + char const * const pkg, unsigned long long Done, + unsigned long long Total, char const * const msg) +{ + float const progress{Done / static_cast<float>(Total) * 100}; + std::ostringstream str; + str.imbue(std::locale("C.UTF-8")); + str.precision(4); + str << "Status: " << status << '\n'; + if (pkg != nullptr) + str << "Package: " << pkg << '\n'; + str << "Percent: " << std::fixed << progress << '\n' + << "Message: " << msg << "\n\n"; + return str.str(); +} + void PackageManagerProgressDeb822Fd::StartDpkg() { // FIXME: use SetCloseExec here once it taught about throwing // exceptions instead of doing _exit(100) on failure fcntl(OutStatusFd,F_SETFD,FD_CLOEXEC); - // send status information that we are about to fork dpkg - std::string status; - strprintf(status, "Status: %s\nPercent: %.4f\nMessage: %s\n\n", "progress", - (StepsDone/float(StepsTotal)*100.0), _("Running dpkg")); - WriteToStatusFd(std::move(status)); + WriteToStatusFd(GetProgressDeb822String("progress", nullptr, StepsDone, StepsTotal, _("Running dpkg"))); } APT_CONST void PackageManagerProgressDeb822Fd::Stop() @@ -178,10 +192,7 @@ void PackageManagerProgressDeb822Fd::Error(std::string PackageName, unsigned int TotalSteps, std::string ErrorMessage) { - std::string status; - strprintf(status, "Status: %s\nPackage: %s\nPercent: %.4f\nMessage: %s\n\n", "Error", - PackageName.c_str(), (StepsDone/float(TotalSteps)*100.0), ErrorMessage.c_str()); - WriteToStatusFd(std::move(status)); + WriteToStatusFd(GetProgressDeb822String("Error", PackageName.c_str(), StepsDone, TotalSteps, ErrorMessage.c_str())); } void PackageManagerProgressDeb822Fd::ConffilePrompt(std::string PackageName, @@ -189,10 +200,7 @@ void PackageManagerProgressDeb822Fd::ConffilePrompt(std::string PackageName, unsigned int TotalSteps, std::string ConfMessage) { - std::string status; - strprintf(status, "Status: %s\nPackage: %s\nPercent: %.4f\nMessage: %s\n\n", "ConfFile", - PackageName.c_str(), (StepsDone/float(TotalSteps)*100.0), ConfMessage.c_str()); - WriteToStatusFd(std::move(status)); + WriteToStatusFd(GetProgressDeb822String("ConfFile", PackageName.c_str(), StepsDone, TotalSteps, ConfMessage.c_str())); } @@ -204,10 +212,7 @@ bool PackageManagerProgressDeb822Fd::StatusChanged(std::string PackageName, StepsDone = xStepsDone; StepsTotal = xTotalSteps; - std::string status; - strprintf(status, "Status: %s\nPackage: %s\nPercent: %.4f\nMessage: %s\n\n", "progress", - PackageName.c_str(), (StepsDone/float(StepsTotal)*100.0), message.c_str()); - WriteToStatusFd(std::move(status)); + WriteToStatusFd(GetProgressDeb822String("progress", PackageName.c_str(), StepsDone, StepsTotal, message.c_str())); return true; } |