Angus Salkeld wrote:
On Thu, Jun 23, 2011 at 11:59:17AM +0200, Jim Meyering wrote:
> Angus Salkeld wrote:
> > char changes size on different arches.
>
> Hi Angus,
>
> Have you encountered an actual system on which "sizeof char" is not 1?
No, I get that sizeof(char) is 1 on pretty much all systems.
(I was more concerned with sizeof(char*) vs. sizeof(uint32_t*)
I was trying to solve a sparc 64 compile warning. But since re-checking
it seems I have failed :(
On sparc64 sizeof(char*) == 8 and sizeof(int*) is 4 (I expected uint8_t
to be 4 but it much be 8).
The warning is:
log_blackbox.c: In function 'qb_log_blackbox_print_from_file':
log_blackbox.c:171:13: warning: cast increases required alignment of target type
[-Wcast-align]
log_blackbox.c:175:14: warning: cast increases required alignment of target type
[-Wcast-align]
log_blackbox.c:182:16: warning: cast increases required alignment of target type
[-Wcast-align]
sparc64:
http://sparc.koji.fedoraproject.org/koji/getfile?taskID=112209&name=b...
sparcv9:
http://sparc.koji.fedoraproject.org/koji/getfile?taskID=112210&name=b...
The test fails on sparcv9 - but I am not sure how to get at the logs.
Any way I am very interested in the "correcct" way of fixing that
warning.
Hi Angus,
I took a look but was quickly distracted by something else.
An invalid timestamp could cause a NULL dereference.
Also, if this logging code may be run in a multi-threading
context, then it should be changed to use localtime_r rather
than the thread-unsafe localtime function.
From 7ce371b61a14e784a283ae4523b29d63fe3995bf Mon Sep 17 00:00:00 2001
From: Jim Meyering <meyering(a)redhat.com>
Date: Fri, 24 Jun 2011 19:19:10 +0200
Subject: [PATCH] don't let an invalid time stamp provoke a NULL dereference
* lib/log_blackbox.c (qb_log_blackbox_print_from_file): Handle
localtime failure by printing the long-int representation rather
than a strftime-formatted one.
---
lib/log_blackbox.c | 10 ++++++++--
1 files changed, 8 insertions(+), 2 deletions(-)
diff --git a/lib/log_blackbox.c b/lib/log_blackbox.c
index be26b79..1696614 100644
--- a/lib/log_blackbox.c
+++ b/lib/log_blackbox.c
@@ -167,6 +167,7 @@ qb_log_blackbox_print_from_file(const char *bb_filename)
bytes_read = qb_rb_chunk_read(instance, chunk, 512, 0);
ptr = chunk;
if (bytes_read > 0) {
+ struct tm *tm;
/* lineno */
lineno = (uint32_t *) ptr;
ptr += sizeof(uint32_t);
@@ -181,8 +182,13 @@ qb_log_blackbox_print_from_file(const char *bb_filename)
/* timestamp size & content */
timestamp = (time_t *) ptr;
ptr += sizeof(time_t);
- (void)strftime(time_buf, sizeof(time_buf), "%b %d %T",
- localtime(timestamp));
+ tm = localtime(timestamp);
+ if (tm)
+ strftime(time_buf, sizeof(time_buf), "%b %d %T",
+ tm);
+ else
+ snprintf(time_buf, sizeof(time_buf), "%ld",
+ (long int) timestamp);
/* message size & content */
/* log_size = (uint32_t *) ptr; */
--
1.7.6.rc2.302.gc2115