From d4ddf3d8bbd7531075d9d85e1839b63afb90dc01 Mon Sep 17 00:00:00 2001 From: Roland Knall Date: Tue, 20 Aug 2019 07:56:44 +0200 Subject: Qt: Fix null pointer dereference Rework existing code to avoid unsafe localtime Change-Id: I266ae7e006c6aeed42af39fea07c888874d0f588 Reviewed-on: https://code.wireshark.org/review/34329 Petri-Dish: Roland Knall Tested-by: Petri Dish Buildbot Reviewed-by: Roland Knall --- ui/qt/utils/wireshark_zip_helper.cpp | 62 ++++++++++-------------------------- 1 file changed, 17 insertions(+), 45 deletions(-) (limited to 'ui/qt/utils') diff --git a/ui/qt/utils/wireshark_zip_helper.cpp b/ui/qt/utils/wireshark_zip_helper.cpp index 88e13cf098..f2035dda53 100644 --- a/ui/qt/utils/wireshark_zip_helper.cpp +++ b/ui/qt/utils/wireshark_zip_helper.cpp @@ -148,58 +148,30 @@ bool WireSharkZipHelper::unzip(QString zipFile, QString directory, bool (*fileCh #define UINT32_MAX (0xffffffff) #endif -/* The following methods are being taken from https://github.com/nmoinvaz/minizip/blob/1.2/minishared.c */ -int invalid_date(const struct tm *ptm) +unsigned long qDateToDosDate(QDateTime time) { -#define datevalue_in_range(min, max, value) ((min) <= (value) && (value) <= (max)) - return (!datevalue_in_range(0, 207, ptm->tm_year) || - !datevalue_in_range(0, 11, ptm->tm_mon) || - !datevalue_in_range(1, 31, ptm->tm_mday) || - !datevalue_in_range(0, 23, ptm->tm_hour) || - !datevalue_in_range(0, 59, ptm->tm_min) || - !datevalue_in_range(0, 59, ptm->tm_sec)); -#undef datevalue_in_range -} + QDate ld = time.toLocalTime().date(); -uint32_t tm_to_dosdate(const struct tm *ptm) -{ - struct tm fixed_tm; - - /* Years supported: - * [00, 79] (assumed to be between 2000 and 2079) - * [80, 207] (assumed to be between 1980 and 2107, typical output of old - software that does 'year-1900' to get a double digit year) - * [1980, 2107] (due to the date format limitations, only years between 1980 and 2107 can be stored.) - */ - - memcpy(&fixed_tm, ptm, sizeof(struct tm)); - if (fixed_tm.tm_year >= 1980) /* range [1980, 2107] */ - fixed_tm.tm_year -= 1980; - else if (fixed_tm.tm_year >= 80) /* range [80, 99] */ - fixed_tm.tm_year -= 80; - else /* range [00, 79] */ - fixed_tm.tm_year += 20; - - if (invalid_date(ptm)) - return 0; + int year = ld.year() - 1900; + if ( year >= 1980 ) + year -= 1980; + else if ( year >= 80 ) + year -= 80; + else + year += 20; - return (uint32_t)(((fixed_tm.tm_mday) + (32 * (fixed_tm.tm_mon + 1)) + (512 * fixed_tm.tm_year)) << 16) | - ((fixed_tm.tm_sec / 2) + (32 * fixed_tm.tm_min) + (2048 * (uint32_t)fixed_tm.tm_hour)); -} + int month = ld.month() - 1; + int day = ld.day(); -unsigned long qDateToDosDate(QDateTime time) -{ - time_t rawtime = time.toTime_t(); - struct tm * timeinfo; + if ( year < 0 || year > 207 || month < 1 || month > 31 ) + return 0; - timeinfo = localtime(&rawtime); - timeinfo->tm_year = time.date().year() - 1900; - timeinfo->tm_mon = time.date().month() - 1; - timeinfo->tm_mday = time.date().day(); + QTime lt = time.toLocalTime().time(); - mktime(timeinfo); + unsigned int dosDate = static_cast((day + (32 * (month + 1)) + (512 * year))); + unsigned int dosTime = static_cast((lt.second() / 2) + (32 * lt.minute()) + (2048 * lt.hour())); - return tm_to_dosdate(timeinfo); + return dosDate << 16 | dosTime; } void WireSharkZipHelper::addFileToZip(zipFile zf, QString filepath, QString fileInZip) -- cgit v1.2.3