From 37da965d8ae37bb9f00dcced97d44bd446896849 Mon Sep 17 00:00:00 2001 From: Elliott Sales de Andrade Date: Wed, 5 Sep 2018 14:27:46 -0400 Subject: [PATCH 3/3] Use safer comparison for process start time. Comparing two doubles for equality can be troublesome, especially on 32-bit systems (possibly due to 80-bit registers). Checking for some epsilon is safer. Signed-off-by: Elliott Sales de Andrade --- src/api-linux.c | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/src/api-linux.c b/src/api-linux.c index d2b6193..8162f1d 100644 --- a/src/api-linux.c +++ b/src/api-linux.c @@ -28,12 +28,16 @@ typedef struct { #define PS__TV2DOUBLE(t) ((t).tv_sec + (t).tv_usec / 1000000.0) -#define PS__CHECK_STAT(stat, handle) \ - if (psll_linux_boot_time + (stat).starttime * psll_linux_clock_period != \ - handle->create_time) { \ - ps__no_such_process(handle->pid, 0); \ - ps__throw_error(); \ - } +#define PS__CHECK_STAT(stat, handle) \ + do { \ + double starttime = psll_linux_boot_time + \ + (stat).starttime * psll_linux_clock_period; \ + double diff = starttime - (handle)->create_time; \ + if (fabs(diff) > psll_linux_clock_period) { \ + ps__no_such_process((handle)->pid, 0); \ + ps__throw_error(); \ + } \ + } while (0) #define PS__CHECK_HANDLE(handle) \ do { \ @@ -218,6 +222,7 @@ int psll__parse_stat_file(long pid, psl_stat_t *stat, char **name) { void ps__check_for_zombie(ps_handle_t *handle, int err) { psl_stat_t stat; + double diff; if (!handle) error("Process pointer cleaned up already"); @@ -226,8 +231,9 @@ void ps__check_for_zombie(ps_handle_t *handle, int err) { ps__throw_error(); } - if (psll_linux_boot_time + stat.starttime * psll_linux_clock_period != - handle->create_time) { + diff = (psll_linux_boot_time + stat.starttime * psll_linux_clock_period) - + handle->create_time; + if (fabs(diff) > psll_linux_clock_period) { ps__no_such_process(handle->pid, 0); err = 1; } else if (stat.state == 'Z') { -- 2.17.1