Skip to content

Commit

Permalink
Fix number of bugs in log.cpp
Browse files Browse the repository at this point in the history
Fix null pointer dereference, memory leak in log_print, fix heap corruption in _stprintf_s call in the print_time routine.
  • Loading branch information
hfiref0x committed Jan 22, 2019
1 parent 19e7fe1 commit 0b57ee3
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions al-khaser/Shared/log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ TCHAR* print_time()
}

//Getting rid of \n
timestr[_tcsclen(timestr) - 1] = 0;
timestr[_tcsclen(timestr) - 1] = 0;

//Additional +2 for square braces
size = (_tcsclen(timestr) + 1 + 2) * sizeof(TCHAR);
size = (_tcsclen(timestr) + 1 + 2) * sizeof(TCHAR);
buf = (TCHAR*)malloc(size);

memset(buf, 0x0, size);
_stprintf_s(buf, size,_T("[%s]"), timestr);

if (buf) {
memset(buf, 0x0, size);
_stprintf_s(buf, size / sizeof(TCHAR), _T("[%s]"), timestr);
}
return buf;
}
void log_print(const TCHAR* filename, const TCHAR *fmt, ...)
Expand All @@ -47,12 +47,18 @@ void log_print(const TCHAR* filename, const TCHAR *fmt, ...)
const TCHAR *p, *r;
int e;

TCHAR *pszTime;

if (SESSION_TRACKER > 0)
_tfopen_s(&fp, _T("log.txt"), _T("a+"));
else
_tfopen_s(&fp, _T("log.txt"), _T("w"));

_ftprintf(fp, _T("%s "), print_time());
pszTime = print_time();
if (pszTime) {
_ftprintf(fp, _T("%s "), pszTime);
free(pszTime);
}
va_start(list, fmt);

for (p = fmt; *p; ++p)
Expand Down

0 comments on commit 0b57ee3

Please sign in to comment.