From e21d907ede24d42ce68abe332f9e49490813d37e Mon Sep 17 00:00:00 2001 From: AdamPlenty <58278560+AdamPlenty@users.noreply.github.com> Date: Mon, 29 Jul 2024 03:45:32 +0100 Subject: [PATCH] Fixed year 2038 crash Fixes #3379 --- src/bflib_basics.c | 38 +++++++++++++++++++++++--------------- src/bflib_basics.h | 3 +++ src/bflib_datetm.cpp | 26 +++++++++++++++----------- 3 files changed, 41 insertions(+), 26 deletions(-) diff --git a/src/bflib_basics.c b/src/bflib_basics.c index 39126b5dd9..8ee6edf8c3 100644 --- a/src/bflib_basics.c +++ b/src/bflib_basics.c @@ -441,36 +441,44 @@ int LbLog(struct TbLog *log, const char *fmt_str, va_list arg) if ((log->flags & LbLog_TimeInHeader) != 0) { struct TbTime curr_time; - LbTime(&curr_time); - fprintf(file, " @ %02d:%02d:%02d", - curr_time.Hour,curr_time.Minute,curr_time.Second); + if (LbTime(&curr_time) == Lb_SUCCESS) + { + fprintf(file, " @ %02u:%02u:%02u", + curr_time.Hour,curr_time.Minute,curr_time.Second); + } at_used = 1; } if ((log->flags & LbLog_DateInHeader) != 0) { struct TbDate curr_date; - LbDate(&curr_date); - const char *sep; - if ( at_used ) - sep = " "; - else - sep = " @ "; - fprintf(file," %s%02d-%02d-%d",sep,curr_date.Day,curr_date.Month,curr_date.Year); + if (LbDate(&curr_date) == Lb_SUCCESS) + { + const char *sep; + if ( at_used ) + sep = " "; + else + sep = " @ "; + fprintf(file," %s%02u-%02u-%u",sep,curr_date.Day,curr_date.Month,curr_date.Year); + } } fprintf(file, "\n\n"); } if ((log->flags & LbLog_DateInLines) != 0) { struct TbDate curr_date; - LbDate(&curr_date); - fprintf(file,"%02d-%02d-%d ",curr_date.Day,curr_date.Month,curr_date.Year); + if (LbDate(&curr_date) == Lb_SUCCESS) + { + fprintf(file,"%02u-%02u-%u ",curr_date.Day,curr_date.Month,curr_date.Year); + } } if ((log->flags & LbLog_TimeInLines) != 0) { struct TbTime curr_time; - LbTime(&curr_time); - fprintf(file, "%02d:%02d:%02d ", - curr_time.Hour,curr_time.Minute,curr_time.Second); + if (LbTime(&curr_time) == Lb_SUCCESS) + { + fprintf(file, "%02u:%02u:%02u ", + curr_time.Hour,curr_time.Minute,curr_time.Second); + } } if (log->prefix[0] != '\0') { fputs(log->prefix, file); diff --git a/src/bflib_basics.h b/src/bflib_basics.h index 0c6f5c882b..32fb125ca8 100644 --- a/src/bflib_basics.h +++ b/src/bflib_basics.h @@ -17,6 +17,9 @@ * (at your option) any later version. */ /******************************************************************************/ +#define _FILE_OFFSET_BITS 64 +#define _TIME_BITS 64 + #ifndef BFLIB_BASICS_H #define BFLIB_BASICS_H diff --git a/src/bflib_datetm.cpp b/src/bflib_datetm.cpp index 286f342983..14ae9e0692 100644 --- a/src/bflib_datetm.cpp +++ b/src/bflib_datetm.cpp @@ -197,20 +197,24 @@ TbResult LbDateTime(struct TbDate *curr_date, struct TbTime *curr_time) TbResult LbDateTimeDecode(const time_t *datetime,struct TbDate *curr_date,struct TbTime *curr_time) { - struct tm *ltime=localtime(datetime); - if (curr_date!=NULL) + struct tm *ltime = localtime(datetime); + if (ltime == NULL) { - curr_date->Day=ltime->tm_mday; - curr_date->Month=ltime->tm_mon+1; - curr_date->Year=1900+ltime->tm_year; - curr_date->DayOfWeek=ltime->tm_wday; + return Lb_FAIL; } - if (curr_time!=NULL) + if (curr_date != NULL) { - curr_time->Hour=ltime->tm_hour; - curr_time->Minute=ltime->tm_min; - curr_time->Second=ltime->tm_sec; - curr_time->HSecond=0; + curr_date->Day = ltime->tm_mday; + curr_date->Month = ltime->tm_mon+1; + curr_date->Year = 1900+ltime->tm_year; + curr_date->DayOfWeek = ltime->tm_wday; + } + if (curr_time != NULL) + { + curr_time->Hour = ltime->tm_hour; + curr_time->Minute = ltime->tm_min; + curr_time->Second = ltime->tm_sec; + curr_time->HSecond = 0; } return Lb_SUCCESS; }