clogger RubyGem user+dev discussion/patches/pulls/bugs/help
 help / color / mirror / code / Atom feed
* [PATCH] fix $request_time{9,0} for 32-bit platforms
@ 2022-12-25 11:28 Eric Wong
  0 siblings, 0 replies; only message in thread
From: Eric Wong @ 2022-12-25 11:28 UTC (permalink / raw)
  To: clogger-public

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

^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2022-12-25 11:28 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-12-25 11:28 [PATCH] fix $request_time{9,0} for 32-bit platforms Eric Wong

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).