* [PATCH] log: Avoid time_t/__syscall_slong_t format mismatch with long int on X32 ABI
@ 2023-03-13 12:07 Stefano Brivio
2023-03-14 10:25 ` David Gibson
0 siblings, 1 reply; 2+ messages in thread
From: Stefano Brivio @ 2023-03-13 12:07 UTC (permalink / raw)
To: passt-dev
On X32 (ILP32 using AMD64 system call ABI) and glibc,
struct timespec::tv_nsec is __syscall_slong_t and not a long int, see
also https://sourceware.org/bugzilla/show_bug.cgi?id=16437 and
timespec(3type). Fine, we could cast that down to long and be done
with it.
But it turns out that also time_t (not guaranteed to be equivalent to
any type) is a long long int, and there we can't downcast.
To keep it simple, cast both to long long int, and change formats to
%lli, to avoid format warnings from gcc.
Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
---
log.c | 22 ++++++++++++----------
1 file changed, 12 insertions(+), 10 deletions(-)
diff --git a/log.c b/log.c
index d2f08fa..d7411ba 100644
--- a/log.c
+++ b/log.c
@@ -53,9 +53,9 @@ void name(const char *format, ...) { \
\
if (setlogmask(0) & LOG_MASK(LOG_DEBUG) && log_file == -1) { \
clock_gettime(CLOCK_REALTIME, &tp); \
- fprintf(stderr, "%li.%04li: ", \
- tp.tv_sec - log_start, \
- tp.tv_nsec / (100L * 1000)); \
+ fprintf(stderr, "%lli.%04lli: ", \
+ (long long int)tp.tv_sec - log_start, \
+ (long long int)tp.tv_nsec / (100L * 1000)); \
} \
\
if ((LOG_MASK(LOG_PRI(level)) & log_mask) || BEFORE_DAEMON) { \
@@ -225,8 +225,9 @@ static void logfile_rotate_fallocate(int fd, struct timespec *ts)
return;
n = snprintf(buf, BUFSIZ,
- "%s - log truncated at %li.%04li", log_header,
- ts->tv_sec - log_start, ts->tv_nsec / (100L * 1000));
+ "%s - log truncated at %lli.%04lli", log_header,
+ (long long int)(ts->tv_sec - log_start),
+ (long long int)(ts->tv_nsec / (100L * 1000)));
/* Avoid partial lines by padding the header with spaces */
nl = memchr(buf + n + 1, '\n', BUFSIZ - n - 1);
@@ -256,9 +257,9 @@ static void logfile_rotate_move(int fd, struct timespec *ts)
char buf[BUFSIZ], *nl;
header_len = snprintf(buf, BUFSIZ,
- "%s - log truncated at %li.%04li\n", log_header,
- ts->tv_sec - log_start,
- ts->tv_nsec / (100L * 1000));
+ "%s - log truncated at %lli.%04lli\n", log_header,
+ (long long int)(ts->tv_sec - log_start),
+ (long long int)(ts->tv_nsec / (100L * 1000)));
if (lseek(fd, 0, SEEK_SET) == -1)
return;
if (write(fd, buf, header_len) == -1)
@@ -349,8 +350,9 @@ void logfile_write(int pri, const char *format, va_list ap)
if (clock_gettime(CLOCK_REALTIME, &ts))
return;
- n = snprintf(buf, BUFSIZ, "%li.%04li: %s",
- ts.tv_sec - log_start, ts.tv_nsec / (100L * 1000),
+ n = snprintf(buf, BUFSIZ, "%lli.%04lli: %s",
+ (long long int)(ts.tv_sec - log_start),
+ (long long int)(ts.tv_nsec / (100L * 1000)),
logfile_prefix[pri]);
n += vsnprintf(buf + n, BUFSIZ - n, format, ap);
--
@@ -53,9 +53,9 @@ void name(const char *format, ...) { \
\
if (setlogmask(0) & LOG_MASK(LOG_DEBUG) && log_file == -1) { \
clock_gettime(CLOCK_REALTIME, &tp); \
- fprintf(stderr, "%li.%04li: ", \
- tp.tv_sec - log_start, \
- tp.tv_nsec / (100L * 1000)); \
+ fprintf(stderr, "%lli.%04lli: ", \
+ (long long int)tp.tv_sec - log_start, \
+ (long long int)tp.tv_nsec / (100L * 1000)); \
} \
\
if ((LOG_MASK(LOG_PRI(level)) & log_mask) || BEFORE_DAEMON) { \
@@ -225,8 +225,9 @@ static void logfile_rotate_fallocate(int fd, struct timespec *ts)
return;
n = snprintf(buf, BUFSIZ,
- "%s - log truncated at %li.%04li", log_header,
- ts->tv_sec - log_start, ts->tv_nsec / (100L * 1000));
+ "%s - log truncated at %lli.%04lli", log_header,
+ (long long int)(ts->tv_sec - log_start),
+ (long long int)(ts->tv_nsec / (100L * 1000)));
/* Avoid partial lines by padding the header with spaces */
nl = memchr(buf + n + 1, '\n', BUFSIZ - n - 1);
@@ -256,9 +257,9 @@ static void logfile_rotate_move(int fd, struct timespec *ts)
char buf[BUFSIZ], *nl;
header_len = snprintf(buf, BUFSIZ,
- "%s - log truncated at %li.%04li\n", log_header,
- ts->tv_sec - log_start,
- ts->tv_nsec / (100L * 1000));
+ "%s - log truncated at %lli.%04lli\n", log_header,
+ (long long int)(ts->tv_sec - log_start),
+ (long long int)(ts->tv_nsec / (100L * 1000)));
if (lseek(fd, 0, SEEK_SET) == -1)
return;
if (write(fd, buf, header_len) == -1)
@@ -349,8 +350,9 @@ void logfile_write(int pri, const char *format, va_list ap)
if (clock_gettime(CLOCK_REALTIME, &ts))
return;
- n = snprintf(buf, BUFSIZ, "%li.%04li: %s",
- ts.tv_sec - log_start, ts.tv_nsec / (100L * 1000),
+ n = snprintf(buf, BUFSIZ, "%lli.%04lli: %s",
+ (long long int)(ts.tv_sec - log_start),
+ (long long int)(ts.tv_nsec / (100L * 1000)),
logfile_prefix[pri]);
n += vsnprintf(buf + n, BUFSIZ - n, format, ap);
--
2.39.2
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH] log: Avoid time_t/__syscall_slong_t format mismatch with long int on X32 ABI
2023-03-13 12:07 [PATCH] log: Avoid time_t/__syscall_slong_t format mismatch with long int on X32 ABI Stefano Brivio
@ 2023-03-14 10:25 ` David Gibson
0 siblings, 0 replies; 2+ messages in thread
From: David Gibson @ 2023-03-14 10:25 UTC (permalink / raw)
To: Stefano Brivio; +Cc: passt-dev
[-- Attachment #1: Type: text/plain, Size: 3301 bytes --]
On Mon, Mar 13, 2023 at 01:07:15PM +0100, Stefano Brivio wrote:
> On X32 (ILP32 using AMD64 system call ABI) and glibc,
> struct timespec::tv_nsec is __syscall_slong_t and not a long int, see
> also https://sourceware.org/bugzilla/show_bug.cgi?id=16437 and
> timespec(3type). Fine, we could cast that down to long and be done
> with it.
>
> But it turns out that also time_t (not guaranteed to be equivalent to
> any type) is a long long int, and there we can't downcast.
>
> To keep it simple, cast both to long long int, and change formats to
> %lli, to avoid format warnings from gcc.
>
> Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
Reviewed-by: David Gibson <david@gibson.dropbear.id.au>
> ---
> log.c | 22 ++++++++++++----------
> 1 file changed, 12 insertions(+), 10 deletions(-)
>
> diff --git a/log.c b/log.c
> index d2f08fa..d7411ba 100644
> --- a/log.c
> +++ b/log.c
> @@ -53,9 +53,9 @@ void name(const char *format, ...) { \
> \
> if (setlogmask(0) & LOG_MASK(LOG_DEBUG) && log_file == -1) { \
> clock_gettime(CLOCK_REALTIME, &tp); \
> - fprintf(stderr, "%li.%04li: ", \
> - tp.tv_sec - log_start, \
> - tp.tv_nsec / (100L * 1000)); \
> + fprintf(stderr, "%lli.%04lli: ", \
> + (long long int)tp.tv_sec - log_start, \
> + (long long int)tp.tv_nsec / (100L * 1000)); \
> } \
> \
> if ((LOG_MASK(LOG_PRI(level)) & log_mask) || BEFORE_DAEMON) { \
> @@ -225,8 +225,9 @@ static void logfile_rotate_fallocate(int fd, struct timespec *ts)
> return;
>
> n = snprintf(buf, BUFSIZ,
> - "%s - log truncated at %li.%04li", log_header,
> - ts->tv_sec - log_start, ts->tv_nsec / (100L * 1000));
> + "%s - log truncated at %lli.%04lli", log_header,
> + (long long int)(ts->tv_sec - log_start),
> + (long long int)(ts->tv_nsec / (100L * 1000)));
>
> /* Avoid partial lines by padding the header with spaces */
> nl = memchr(buf + n + 1, '\n', BUFSIZ - n - 1);
> @@ -256,9 +257,9 @@ static void logfile_rotate_move(int fd, struct timespec *ts)
> char buf[BUFSIZ], *nl;
>
> header_len = snprintf(buf, BUFSIZ,
> - "%s - log truncated at %li.%04li\n", log_header,
> - ts->tv_sec - log_start,
> - ts->tv_nsec / (100L * 1000));
> + "%s - log truncated at %lli.%04lli\n", log_header,
> + (long long int)(ts->tv_sec - log_start),
> + (long long int)(ts->tv_nsec / (100L * 1000)));
> if (lseek(fd, 0, SEEK_SET) == -1)
> return;
> if (write(fd, buf, header_len) == -1)
> @@ -349,8 +350,9 @@ void logfile_write(int pri, const char *format, va_list ap)
> if (clock_gettime(CLOCK_REALTIME, &ts))
> return;
>
> - n = snprintf(buf, BUFSIZ, "%li.%04li: %s",
> - ts.tv_sec - log_start, ts.tv_nsec / (100L * 1000),
> + n = snprintf(buf, BUFSIZ, "%lli.%04lli: %s",
> + (long long int)(ts.tv_sec - log_start),
> + (long long int)(ts.tv_nsec / (100L * 1000)),
> logfile_prefix[pri]);
>
> n += vsnprintf(buf + n, BUFSIZ - n, format, ap);
--
David Gibson | I'll have my music baroque, and my code
david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_
| _way_ _around_!
http://www.ozlabs.org/~dgibson
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2023-03-14 10:36 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-03-13 12:07 [PATCH] log: Avoid time_t/__syscall_slong_t format mismatch with long int on X32 ABI Stefano Brivio
2023-03-14 10:25 ` David Gibson
Code repositories for project(s) associated with this public inbox
https://passt.top/passt
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 IMAP folder(s).