Skip to content

Commit

Permalink
Auto merge of zcash#1615 - arithmetric:1612.fix-floating-point-except…
Browse files Browse the repository at this point in the history
…ion, r=daira

Fixing floating point exception in non-TTY environments

As reported in zcash#1612, a floating point exception occurs when zcashd is started with `showmetrics` enabled in environments without a TTY, such as when started as a service or piped to a file.

The root cause is that the metrics code attempts to get the screen width and uses this as a divisor in calculations. For non-TTY environments, this value is 0, leading to a division by zero error.

This PR adds a default screen width of 80 and uses the actual screen width only if the width can be fetched (and in a TTY environment).
  • Loading branch information
zkbot committed Oct 25, 2016
2 parents 11ce636 + 1da44b3 commit 511c5ec
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/metrics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <boost/thread/synchronized_value.hpp>
#include <string>
#include <sys/ioctl.h>
#include <unistd.h>

AtomicCounter transactionsValidated;
AtomicCounter ehSolverRuns;
Expand Down Expand Up @@ -192,16 +193,22 @@ void ThreadShowMetricsScreen()
while (true) {
// Number of lines that are always displayed
int lines = 1;
int cols = 80;

// Get current window size
struct winsize w;
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
if (isatty(STDOUT_FILENO)) {
struct winsize w;
w.ws_col = 0;
if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &w) != -1 && w.ws_col != 0) {
cols = w.ws_col;
}
}

// Erase below current position
std::cout << "\e[J";

lines += printMetrics(w.ws_col, nStart, mining);
lines += printMessageBox(w.ws_col);
lines += printMetrics(cols, nStart, mining);
lines += printMessageBox(cols);
lines += printInitMessage();

// Explain how to exit
Expand Down

0 comments on commit 511c5ec

Please sign in to comment.