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 <rknall@gmail.com>
Tested-by: Petri Dish Buildbot
Reviewed-by: Roland Knall <rknall@gmail.com>
This commit is contained in:
Roland Knall 2019-08-20 07:56:44 +02:00
parent eabd1b0abb
commit d4ddf3d8bb
1 changed files with 19 additions and 47 deletions

View File

@ -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)
{
#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
}
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;
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));
}
unsigned long qDateToDosDate(QDateTime time)
{
time_t rawtime = time.toTime_t();
struct tm * timeinfo;
QDate ld = time.toLocalTime().date();
timeinfo = localtime(&rawtime);
timeinfo->tm_year = time.date().year() - 1900;
timeinfo->tm_mon = time.date().month() - 1;
timeinfo->tm_mday = time.date().day();
int year = ld.year() - 1900;
if ( year >= 1980 )
year -= 1980;
else if ( year >= 80 )
year -= 80;
else
year += 20;
mktime(timeinfo);
int month = ld.month() - 1;
int day = ld.day();
return tm_to_dosdate(timeinfo);
if ( year < 0 || year > 207 || month < 1 || month > 31 )
return 0;
QTime lt = time.toLocalTime().time();
unsigned int dosDate = static_cast<unsigned int>((day + (32 * (month + 1)) + (512 * year)));
unsigned int dosTime = static_cast<unsigned int>((lt.second() / 2) + (32 * lt.minute()) + (2048 * lt.hour()));
return dosDate << 16 | dosTime;
}
void WireSharkZipHelper::addFileToZip(zipFile zf, QString filepath, QString fileInZip)