clogger RubyGem user+dev discussion/patches/pulls/bugs/help
 help / color / mirror / code / Atom feed
From: Eric Wong <bofh@yhbt.net>
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	[thread overview]
Message-ID: <20221225112828.13701-1-bofh@yhbt.net> (raw)

`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);
 }

                 reply	other threads:[~2022-12-25 11:28 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://yhbt.net/clogger/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20221225112828.13701-1-bofh@yhbt.net \
    --to=bofh@yhbt.net \
    --cc=clogger-public@bogomips.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://yhbt.net/clogger.git/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).