HEAP CORRUPTION | Cant find the reason

Hello. I've been debugging and searching for some time now, but I cant find the bug!

I'd be very gratefull if someone checks the following

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <protocol.h>
#include <stdio.h>
#include <time.h>
#include <stdarg.h>
using namespace network;

int connection; FILE* lf;//Main connection and log file

wchar_t* getTime( long ms){//Vrne lepo oblikovan podan čas
	wchar_t* rets;  unsigned h,m,s; const  long ct=ms; 
	h=ms/3600000;
	ms-=h*3600000;
	m=ms/60000;
	ms-=m*60000;
	s=ms/1000;
	ms-=s*1000;
	rets=(wchar_t*)malloc(18*sizeof(wchar_t));
	if(ct<1000){//Only miliseconds
		//rets=(wchar_t*)malloc(4*sizeof(wchar_t));
		//wsprintfW(rets, L"%03u", ms);
		wsprintfW(rets, L"[%03u:%02u:%02u:%03u]:\t", h,m,s,ms);
	}else
	if(ct<60000){//Up to seconds
		rets=(wchar_t*)malloc(7*sizeof(wchar_t));
		//wsprintfW(rets,L"%02u:%03u", s,ms);
		wsprintfW(rets, L"[%03u:%02u:%02u:%03u]:\t", h,m,s,ms);
	}else
	if(ct<3600000){//Up to minutes
		//rets=(wchar_t*)malloc(10*sizeof(wchar_t));
		//wsprintfW(rets, L"%02u:%02u:%03u", m,s,ms);
		wsprintfW(rets, L"[%03u:%02u:%02u:%03u]:\t", h,m,s,ms);
	}
	else{//Hours included
		//rets=(wchar_t*)malloc(14*sizeof(wchar_t));	
		wsprintfW(rets, L"[%03u:%02u:%02u:%03u]:\t", h,m,s,ms);
		//wsprintfW(rets, L"%03uh %02um %02us %03ums", h,m,s,ms);
	};
	return rets;
};
#define  getTime() getTime(clock())

int _log( wchar_t* s, va_list* va ){
	if(!lf){//Not initialized
		SYSTEMTIME	t;
		GetLocalTime(&t);
		char fname[32];
		sprintf_s(fname, 32, "%02d%02d%02d%03d%04d%02d%02d.log",  t.wHour, t.wMinute, t.wSecond, t.wMilliseconds, t.wYear, t.wDay, t.wMonth);
		lf		= fopen( fname, "wb" );
		if(!lf)
			return 0;
	}
	int written=0;
	LPWSTR tmp =(LPWSTR) malloc(sizeof(wchar_t)*1024);
	LPWSTR time=getTime(); 
	tmp[0]=L'\0';
	written=_vsnwprintf(tmp+17, 1024-19, s,(*va) );
	if(written<0)
		return 0;
	memcpy(tmp, time, 17*sizeof(wchar_t));
	written+=17;
	memcpy(tmp+written, L"\r\n", sizeof(wchar_t)*2);
	fwrite(tmp,sizeof(wchar_t),written+2, lf);
	fflush(lf);
	WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), tmp, written+2, (LPDWORD)&written, NULL);//?
	free(time);
	free(tmp);
	return 0;
}

inline int log( wchar_t* s, ... ){
	va_list va;
	va_start(va, s);
	int ret = _log(s, &va);
	va_end(va);
	return ret;
};


protocol.h inclides windows.h


thanks
Line 24 - you are reallocating/messing the buffer pointer already allocated at line 17.
tyvm :)
Topic archived. No new replies allowed.