Correct connection timing computation

Handle microsecond rollover when measuring connection attempts.
Calculate seconds and microseconds directly, adjusting when the
microsecond field underflows.

Fixes https://github.com/rsyslog/rsyslog/issues/5831.

AI-Agent: Codex
This commit is contained in:
Rainer Gerhards 2025-07-18 21:31:01 +02:00
parent d19086dd7e
commit 54d8e273d2
No known key found for this signature in database
GPG Key ID: 0CB6B2A8BE80B499

View File

@ -977,7 +977,15 @@ static rsRetVal Connect(nsd_t *pNsd, int family, uchar *port, uchar *host, char
gettimeofday(&end, NULL);
seconds = end.tv_sec - start.tv_sec;
useconds = end.tv_usec - start.tv_usec;
LogError(errno, RS_RET_IO_ERROR, "cannot connect to %s:%s (took %ld.%ld seconds)", host, port, seconds,
if (useconds < 0) {
useconds += 1000000;
seconds -= 1;
}
if (seconds < 0) {
seconds = 0;
useconds = 0;
}
LogError(errno, RS_RET_IO_ERROR, "cannot connect to %s:%s (took %ld.%02ld seconds)", host, port, seconds,
useconds / 10000);
ABORT_FINALIZE(RS_RET_IO_ERROR);
}