1
0
mirror of https://github.com/libssh2/libssh2.git synced 2026-10-24 03:48:58 +03:00

misc: replace gettimeofday() with ssh2_now() in ssh2_deb_low()

To de-duplicate current-time methods, to avoid relying on deprecated
`gettimeofday()` in core code, and to avoid an additional
Windows-specific retrieval method.

Also:
- add and use `SSH2_TIME_T_FORMAT` macro.

Follow-up to c97959e9b6 #2489
Cherry-picked from #2485

Closes #2490
This commit is contained in:
Viktor Szakats
2026-08-02 19:32:05 +02:00
parent 04665fedff
commit a43dc6d504
2 changed files with 13 additions and 9 deletions

View File

@@ -308,6 +308,7 @@ typedef enum {
#define ssh2_time_t libssh2_int64_t /* us */
#define ssh2_timediff_t libssh2_int64_t /* us */
#define SSH2_TIME_T_FORMAT SSH2_INT64_T_FORMAT
#define ssh2_sec_to_timediff(sec) ((ssh2_time_t)(sec) * 1000000)
#define ssh2_timediff_to_sec(td) ((td) / 1000000)
#define ssh2_timediff_to_usec(td) ((td) % 1000000)

View File

@@ -554,12 +554,12 @@ void ssh2_deb_low(LIBSSH2_SESSION *session, int context,
"Publickey",
"Socket",
};
static long firstsec;
static ssh2_time_t firstnow;
char buffer[1536];
int len, msglen, buflen = sizeof(buffer);
va_list vargs;
struct timeval now;
ssh2_time_t now;
const char *contexttext = contexts[0];
unsigned int contextindex;
@@ -575,14 +575,17 @@ void ssh2_deb_low(LIBSSH2_SESSION *session, int context,
}
}
ssh2_gettimeofday(&now, NULL);
if(!firstsec)
firstsec = now.tv_sec;
now.tv_sec -= firstsec;
now = ssh2_now();
if(!firstnow)
firstnow = now;
now -= firstnow;
/* '[libssh2] 9999999999.9999999999 Failure Event: ' */
len = ssh2_snprintf(buffer, buflen, "[libssh2] %d.%06d %s: ",
(int)now.tv_sec, (int)now.tv_usec, contexttext);
/* '[libssh2] 99999999999999999999.99999999999999999999 Failure Event: ' */
len = ssh2_snprintf(buffer, buflen, "[libssh2]"
" %" SSH2_TIME_T_FORMAT ".%06" SSH2_TIME_T_FORMAT
" %s: ",
ssh2_timediff_to_sec(now),
ssh2_timediff_to_usec(now), contexttext);
if(len < 0 || len >= buflen) {
msglen = len < 0 ? 0 : (buflen - 1);
buffer[msglen] = '\0';