The bug happens, for example, in the following:

  printf("%s%s\n", (efi_time_t_to_tm(buf), efi_time_t_to_tm(buf)));

where efi_time_t_to_tm() returns a pointer to buf, the address of the
buffer passed to in the first argument. Obviously, the second call
overwrites the result of the first call.

It's dull to prepare a buffer for each function call, so I avoid this
simply by calling printf() for each efi_time_to_tm() call:

  printf("%s", efi_time_t_to_tm(buf));
  printf("%s\n", efi_time_t_to_tm(buf));

When the second call overwrites the result of the first call, printf()
has already finished displaying it.

Also I change efi_time_t_to_tm() so that it uses local buffer. It's
safe if following the above use.

Signed-off-by: HATAYAMA Daisuke <[email protected]>
---

 sadump.c |   76 +++++++++++++++++++++++++++++++-------------------------------
 1 files changed, 38 insertions(+), 38 deletions(-)
diff --git a/sadump.c b/sadump.c
index 490eb6c..92573bf 100644
--- a/sadump.c
+++ b/sadump.c
@@ -37,7 +37,7 @@ static inline int is_set_bit(char *bitmap, ulong pfn);
 static inline int page_is_ram(unsigned int nr);
 static inline int page_is_dumpable(unsigned int nr);
 static int lookup_diskset(ulong whole_offset, int *diskid, ulong *disk_offset);
-static struct tm *efi_time_t_to_tm(const efi_time_t *e, struct tm *t);
+static struct tm *efi_time_t_to_tm(const efi_time_t *e);
 static char * guid_to_str(efi_guid_t *guid, char *buf, size_t buflen);
 static int verify_magic_number(uint32_t 
magicnum[DUMP_PART_HEADER_MAGICNUM_SIZE]);
 static ulong per_cpu_ptr(ulong ptr, int cpu);
@@ -279,19 +279,17 @@ restart:
 
                if (memcmp(&sph->time_stamp, &smh->time_stamp,
                           sizeof(efi_time_t)) != 0) {
-                       struct tm tm;
-                       if (CRASHDEBUG(1))
-                               error(INFO, "sadump: time stamp mismatch\n"
-                                     "  partition header: %s\n"
-                                     "  media header: %s\n",
+                       if (CRASHDEBUG(1)) {
+                               error(INFO, "sadump: time stamp mismatch\n");
+                               error(INFO, "sadump:   partition header: %s\n",
                                      strip_linefeeds(asctime
                                                      (efi_time_t_to_tm
-                                                      (&sph->time_stamp,
-                                                       &tm))),
+                                                      (&sph->time_stamp))));
+                               error(INFO, "sadump:   media header: %s\n",
                                      strip_linefeeds(asctime
                                                      (efi_time_t_to_tm
-                                                      (&smh->time_stamp,
-                                                       &tm))));
+                                                      (&smh->time_stamp))));
+                       }
                        goto err;
                }
 
@@ -538,18 +536,20 @@ add_disk(char *file)
 
        if (memcmp(&sd->header->time_stamp, &ph->time_stamp,
                   sizeof(efi_time_t)) != 0) {
-               struct tm tm;
-               if (CRASHDEBUG(1))
-                       error(INFO, "sadump: time stamp mismatch\n"
-                             "  partition header on disk #1: %s\n"
-                             "  partition header on disk #%d: %s\n",
-                             strip_linefeeds(asctime(efi_time_t_to_tm
-                                                     (&sd->header->time_stamp,
-                                                      &tm))),
+               if (CRASHDEBUG(1)) {
+                       error(INFO, "sadump: time stamp mismatch\n");
+                       error(INFO,
+                             "sadump:   partition header on disk #1: %s\n",
+                             strip_linefeeds(asctime
+                                             (efi_time_t_to_tm
+                                              (&sd->header->time_stamp))));
+                       error(INFO,
+                             "sadump:   partition header on disk #%d: %s\n",
                              diskid+1,
-                             strip_linefeeds(asctime(efi_time_t_to_tm
-                                                     (&ph->time_stamp,
-                                                      &tm))));
+                             strip_linefeeds(asctime
+                                             (efi_time_t_to_tm
+                                              (&ph->time_stamp))));
+               }
                free(ph);
                return FALSE;
        }
@@ -794,30 +794,31 @@ ulong get_sadump_switch_stack(ulong task)
 }
 
 static struct tm *
-efi_time_t_to_tm(const efi_time_t *e, struct tm *t)
+efi_time_t_to_tm(const efi_time_t *e)
 {
+       static struct tm t;
        time_t ti;
 
-       memset(t, 0, sizeof(*t));
+       memset(&t, 0, sizeof(t));
 
-       t->tm_sec  = e->second;
-       t->tm_min  = e->minute;
-       t->tm_hour = e->hour;
-       t->tm_mday = e->day;
-       t->tm_mon  = e->month - 1;
-       t->tm_year = e->year - 1900;
+       t.tm_sec  = e->second;
+       t.tm_min  = e->minute;
+       t.tm_hour = e->hour;
+       t.tm_mday = e->day;
+       t.tm_mon  = e->month - 1;
+       t.tm_year = e->year - 1900;
 
        if (e->timezone != EFI_UNSPECIFIED_TIMEZONE)
-               t->tm_hour += e->timezone;
+               t.tm_hour += e->timezone;
 
        else if (CRASHDEBUG(1))
                error(INFO, "sadump: timezone information is missing\n");
 
-       ti = mktime(t);
+       ti = mktime(&t);
        if (ti == (time_t)-1)
-               return t;
+               return &t;
 
-       return localtime_r(&ti, t);
+       return localtime_r(&ti, &t);
 }
 
 static char *
@@ -867,7 +868,6 @@ int sadump_memory_dump(FILE *fp)
        struct sadump_header *sh;
        struct sadump_media_header *smh;
        int i, others;
-       struct tm tm;
        char guid[33];
 
        fprintf(fp, "sadump_data: \n");
@@ -908,7 +908,7 @@ int sadump_memory_dump(FILE *fp)
        fprintf(fp, "         disk_set_id: %s\n", 
guid_to_str(&sph->disk_set_id, guid, sizeof(guid)));
        fprintf(fp, "              vol_id: %s\n", guid_to_str(&sph->vol_id, 
guid, sizeof(guid)));
        fprintf(fp, "          time_stamp: %s\n",
-               strip_linefeeds(asctime(efi_time_t_to_tm(&sph->time_stamp, 
&tm))));
+               strip_linefeeds(asctime(efi_time_t_to_tm(&sph->time_stamp))));
        fprintf(fp, "        set_disk_set: %u\n", sph->set_disk_set);
        fprintf(fp, "             reserve: %u\n", sph->reserve);
        fprintf(fp, "         used_device: %llu\n", 
(ulonglong)sph->used_device);
@@ -922,7 +922,7 @@ int sadump_memory_dump(FILE *fp)
        fprintf(fp, "      header_version: %u\n", sh->header_version);
        fprintf(fp, "             reserve: %u\n", sh->reserve);
        fprintf(fp, "           timestamp: %s\n",
-               strip_linefeeds(asctime(efi_time_t_to_tm(&sh->timestamp, 
&tm))));
+               strip_linefeeds(asctime(efi_time_t_to_tm(&sh->timestamp))));
        fprintf(fp, "              status: %u\n", sh->status);
        fprintf(fp, "            compress: %u\n", sh->compress);
        fprintf(fp, "          block_size: %u\n", sh->block_size);
@@ -998,7 +998,7 @@ int sadump_memory_dump(FILE *fp)
                fprintf(fp, "\n           sadump_id: %s\n", 
guid_to_str(&smh->sadump_id, guid, sizeof(guid)));
                fprintf(fp, "         disk_set_id: %s\n", 
guid_to_str(&smh->disk_set_id, guid, sizeof(guid)));
                fprintf(fp, "          time_stamp: %s\n",
-                       
strip_linefeeds(asctime(efi_time_t_to_tm(&smh->time_stamp, &tm))));
+                       
strip_linefeeds(asctime(efi_time_t_to_tm(&smh->time_stamp))));
                fprintf(fp, "      sequential_num: %d\n", smh->sequential_num);
                fprintf(fp, "           term_cord: %d\n", smh->term_cord);
                fprintf(fp, "disk_set_header_size: %d\n", 
smh->disk_set_header_size);
@@ -1039,7 +1039,7 @@ int sadump_memory_dump(FILE *fp)
                fprintf(fp, "           disk_set_id: %s\n", 
guid_to_str(&sph->disk_set_id, guid, sizeof(guid)));
                fprintf(fp, "                vol_id: %s\n", 
guid_to_str(&sph->vol_id, guid, sizeof(guid)));
                fprintf(fp, "            time_stamp: %s\n",
-                       
strip_linefeeds(asctime(efi_time_t_to_tm(&sph->time_stamp, &tm))));
+                       
strip_linefeeds(asctime(efi_time_t_to_tm(&sph->time_stamp))));
                fprintf(fp, "          set_disk_set: %u\n", sph->set_disk_set);
                fprintf(fp, "               reserve: %u\n", sph->reserve);
                fprintf(fp, "           used_device: %llu\n", 
(ulonglong)sph->used_device);
--
Crash-utility mailing list
[email protected]
https://www.redhat.com/mailman/listinfo/crash-utility

Reply via email to