From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: X-Spam-Status: No, score=-3.9 required=3.0 tests=ALL_TRUSTED,AWL,BAYES_00, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF shortcircuit=no autolearn=ham autolearn_force=no version=3.4.2 Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id 8E2AB1F953 for ; Sun, 25 Dec 2022 11:28:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=yhbt.net; s=selector1; t=1671967708; bh=b3rdyXN+Wl0Jd391v+izGz8wYgyM8fCgzpkGPpv6gYI=; h=From:To:Subject:Date:From; b=hzZLtrNzwgTP3ESwrjGIYzD6Grii2QiS8YwrUy1DT9dxdt+VkWOl+RckseUXlKs17 P3IoMmt8N8YPdPGD0IyfomUx9HkjBV6X/LWR0gcZG7hfhMVyZg+brwZlURDpFJUrzY Ri61tr5Qf98qOCMG8ABPfPbB3XXZE7NAe2ybQ+IE= From: Eric Wong To: clogger-public@bogomips.org Subject: [PATCH] fix $request_time{9,0} for 32-bit platforms Date: Sun, 25 Dec 2022 11:28:28 +0000 Message-Id: <20221225112828.13701-1-bofh@yhbt.net> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: `struct timespec' has 32-bit tv_sec and tv_nsec on 32-bit x86 GNU/Linux system, causing excessive overflow and test failures. --- ext/clogger_ext/clogger.c | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/ext/clogger_ext/clogger.c b/ext/clogger_ext/clogger.c index 2ec9510..8cd40be 100644 --- a/ext/clogger_ext/clogger.c +++ b/ext/clogger_ext/clogger.c @@ -386,30 +386,29 @@ static void append_request_time_fmt(struct clogger *c, VALUE op) clock_gettime(hopefully_CLOCK_MONOTONIC, &now); clock_diff(&now, &c->ts_start); if (ipow) { - struct timespec prev; unsigned long adj = 1; - /* - * n.b. timespec.tv_sec may not be time_t on some platforms, - * so we use a full timespec struct instead of time_t: - */ - prev.tv_sec = now.tv_sec; + int64_t now_sec = now.tv_sec, now_nsec = now.tv_nsec, + prev_sec = now.tv_sec; + do { adj *= 10; } while (--ipow); - now.tv_sec *= adj; - now.tv_nsec *= adj; - if (now.tv_nsec >= NANO_PER_SEC) { - int64_t add = now.tv_nsec / NANO_PER_SEC; - now.tv_sec += add; - now.tv_nsec %= NANO_PER_SEC; + now_sec *= adj; + now_nsec *= adj; + if (now_nsec >= NANO_PER_SEC) { + int64_t add = now_nsec / NANO_PER_SEC; + now_sec += add; + now_nsec %= NANO_PER_SEC; } - if (now.tv_sec < prev.tv_sec) { /* overflowed */ - now.tv_nsec = NANO_PER_SEC - 1; + if (now_sec < prev_sec) { /* overflowed */ + now_nsec = NANO_PER_SEC - 1; /* * some platforms may use unsigned .tv_sec, but * they're not worth supporting, so keep unsigned: */ - now.tv_sec = (time_t)(sizeof(now.tv_sec) == 4 ? + now_sec = (time_t)(sizeof(now.tv_sec) == 4 ? INT_MAX : LONG_MAX); } + now.tv_sec = now_sec; + now.tv_nsec = now_nsec; } append_ts(c, op, &now); }