Skip to contentSkip to conversation

alfred: Fix printing of timespec

PATCH New Sven Eckelmann 1 participant 1 message Mar 8

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 alfred: Fix printing of timespec

musl on a 32 bit system still uses a 64-bit value (long long) for the storage of tv_sec and tv_nsec. But the printf was evaluating these arguments always only as long.

During the print of the nsec value, range of 0-999_999_999 is only possible. 30 bit is therefore enough to store this range. For simplicity, just use an unsigned int.

The second value on the other hand can get up to 64 bit on a 64-bit-unix timestamp system. Just use long long for it to be on the safe side.

---
 main.c   | 4 +++-
 server.c | 4 ++--
 2 files changed, 5 insertions(+), 3 deletions(-)
main.c +3 -1
index 0f242e8..71a6260 100644
@@ -285,7 +285,9 @@ static struct globals *alfred_init(int argc, char *argv[])
 			sync_period = strtod(optarg, NULL);
 			globals->sync_period.tv_sec = (int)sync_period;
 			globals->sync_period.tv_nsec = (double)(sync_period - (int)sync_period) * 1e9;
-			printf(" ** Setting sync interval to: %.9f seconds (%ld.%09ld)\n", sync_period, globals->sync_period.tv_sec, globals->sync_period.tv_nsec);
+			printf(" ** Setting sync interval to: %.9f seconds (%lld.%09u)\n", sync_period,
+			       (long long)globals->sync_period.tv_sec,
+			       (unsigned int)globals->sync_period.tv_nsec);
 			break;
 		case '4':
 			globals->ipv4mode = true;
server.c +2 -2
index 0d792b0..c3d32a0 100644
@@ -404,8 +404,8 @@ static void sync_period_timer(struct globals *globals,
 
 	if (globals->opmode == OPMODE_PRIMARY) {
 		/* we are a primary */
-		printf("[%ld.%09ld] announce primary ...\n",
-		       now.tv_sec, now.tv_nsec);
+		printf("[%lld.%09u] announce primary ...\n",
+		       (long long)now.tv_sec, (unsigned int)now.tv_nsec);
 		announce_primary(globals);
 		sync_data(globals);
 	} else {

---
base-commit: bdab2a4dd79716edd6c5ecdbda7bf38c8495083c
change-id: 20260308-t64-41cd12362bdc

Best regards,

Signature · 1 line
Sven Eckelmann <[email protected]>