File: inouealc.c

package info (click to toggle)
psqlodbc 1%3A08.03.0200-1.2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 3,744 kB
  • ctags: 3,456
  • sloc: ansic: 42,699; sh: 8,715; cpp: 1,461; sql: 115; makefile: 70
file content (269 lines) | stat: -rw-r--r-- 5,237 bytes parent folder | download | duplicates (3)
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#undef	_MEMORY_DEBUG_
#include	"psqlodbc.h"

#ifdef	WIN32
#ifdef	_DEBUG
/* #include	<stdlib.h> */
#define	_CRTDBG_MAP_ALLOC
#include <crtdbg.h>
#else
#include	<malloc.h>
#endif /* _DEBUG */
#endif /* WIN32 */
#include	<string.h>

#include	"misc.h"
typedef struct {
	size_t	len;
	void	*aladr;
} ALADR;

static int	alsize = 0;
static int	tbsize = 0;
static ALADR	*altbl = NULL;

CSTR	ALCERR	= "alcerr";
void * debug_alloc(size_t size)
{
	void * alloced;
	alloced = malloc(size);
inolog(" alloced=%p(%d)\n", alloced, size);
	if (alloced)
	{
		if (!alsize)
		{
			alsize = 100;
			altbl = (ALADR *) malloc(alsize * sizeof(ALADR));
		} 
		else if (tbsize >= alsize)
		{
			ALADR *al;
			alsize *= 2;
			if (al = (ALADR *) realloc(altbl, alsize * sizeof(ALADR)), NULL == al)
				return alloced;
			altbl = al;
		}
		altbl[tbsize].aladr = alloced;
		altbl[tbsize].len = size;
		tbsize++; 
	}
	else
		mylog("%s:alloc %dbyte\n", ALCERR, size);
	return alloced;
}
void * debug_calloc(size_t n, size_t size)
{
	void * alloced = calloc(n, size);

	if (alloced)
	{
		if (!alsize)
		{
			alsize = 100;
			altbl = (ALADR *) malloc(alsize * sizeof(ALADR));
		} 
		else if (tbsize >= alsize)
		{
			ALADR *al;
			alsize *= 2;
			if (al = (ALADR *) realloc(altbl, alsize * sizeof(ALADR)), NULL == al)
				return alloced;
			altbl = al;
		}
		altbl[tbsize].aladr = alloced;
		altbl[tbsize].len = n * size;
		tbsize++; 
	}
	else
		mylog("%s:calloc %dbyte\n", ALCERR, size);
inolog("calloced = %p\n", alloced);
	return alloced;
}
void * debug_realloc(void * ptr, size_t size)
{
	void * alloced = realloc(ptr, size);
	if (!alloced)
	{
		mylog("%s:debug_realloc %p error\n", ALCERR, ptr);
	}
	else if (!ptr)
	{
		altbl[tbsize].aladr = alloced;
		altbl[tbsize].len = size;
		tbsize++;
	}
	else /* if (alloced != ptr) */
	{
		int	i;
		for (i = 0; i < tbsize; i++)
		{
			if (altbl[i].aladr == ptr)
			{
				altbl[i].aladr = alloced;
				altbl[i].len = size;
				break;
			}	
		}
	}
		
	inolog("debug_realloc %p->%p\n", ptr, alloced);
	return alloced;
}
char * debug_strdup(const char * ptr)
{
	char * alloced = strdup(ptr);
	if (!alloced)
	{
		mylog("%s:debug_strdup %p error\n", ALCERR, ptr);
	}
	else
	{
		if (!alsize)
		{
			alsize = 100;
			altbl = (ALADR *) malloc(alsize * sizeof(ALADR));
		} 
		else if (tbsize >= alsize)
		{
			ALADR *al;
			alsize *= 2;
			if (al = (ALADR *) realloc(altbl, alsize * sizeof(ALADR)), NULL == al)
				return alloced;
			altbl = al;
		}
		altbl[tbsize].aladr = alloced;
		altbl[tbsize].len = strlen(ptr) + 1;
		tbsize++; 
	}
	inolog("debug_strdup %p->%p(%s)\n", ptr, alloced, alloced);
	return alloced;
}

void debug_free(void * ptr)
{
	int i, j;
	int	freed = 0;

	if (!ptr)
	{
		mylog("%s:debug_freeing null ptr\n", ALCERR);
		return;
	}
	for (i = 0; i < tbsize; i++)
	{
		if (altbl[i].aladr == ptr)
		{
			for (j = i; j < tbsize - 1; j++)
			{
				altbl[j].aladr = altbl[j + 1].aladr;
				altbl[j].len = altbl[j + 1].len;
			}
			tbsize--;
			freed =  1;
			break;	
		}
	}
	if (! freed)
		mylog("%s:debug_freeing not found ptr %p\n", ALCERR, ptr);
	else
		inolog("debug_freeing ptr=%p\n", ptr);
	free(ptr);
}

static BOOL out_check(void *out, size_t len, const char *name)
{
	BOOL	ret = TRUE;
	int	i;

	for (i = 0; i < tbsize; i++)
	{
		if ((UInt4)out < (UInt4)(altbl[i].aladr))
			continue;
		if ((UInt4)out < (UInt4)(altbl[i].aladr) + altbl[i].len)
		{
			if ((UInt4)out + len > (UInt4)(altbl[i].aladr) + altbl[i].len)
			{
				ret = FALSE;
				mylog("%s:%s:out_check found memory buffer overrun %p(%d)>=%p(%d)\n", ALCERR, name, out, len, altbl[i].aladr, altbl[i].len);
			}
			break;
		}
	}
	return ret;
}
char *debug_strcpy(char *out, const char *in)
{
	if (!out || !in)
	{
		mylog("%s:debug_strcpy null pointer out=%p,in=%p\n", ALCERR, out, in);
		return NULL;
	}
	out_check(out, strlen(in) + 1, "debug_strcpy");
	return strcpy(out, in);
}
char *debug_strncpy(char *out, const char *in, size_t len)
{
	CSTR	func = "debug_strncpy";

	if (!out || !in)
	{
		mylog("%s:%s null pointer out=%p,in=%p\n", ALCERR, func, out, in);
		return NULL;
	}
	out_check(out, len, func);
	return strncpy(out, in, len);
}
char *debug_strncpy_null(char *out, const char *in, size_t len)
{
	CSTR	func = "debug_strncpy_null";

	if (!out || !in)
	{
		mylog("%s:%s null pointer out=%p,in=%p\n", ALCERR, func, out, in);
		return NULL;
	}
	out_check(out, len, func);
	return strncpy_null(out, in, len);
}

void *debug_memcpy(void *out, const void *in, size_t len)
{
	if (!out || !in)
	{
		mylog("%s:debug_memcpy null pointer out=%p,in=%p\n", ALCERR, out, in);
		return NULL;
	}
	out_check(out, len, "debug_memcpy");
	return memcpy(out, in, len);
}

void *debug_memset(void *out, int c, size_t len)
{
	if (!out)
	{
		mylog("%s:debug_memcpy null pointer out=%p\n", ALCERR, out);
		return NULL;
	}
	out_check(out, len, "debug_memset");
	return memset(out, c, len);
}

void debug_memory_check(void)
{
	int i;

	if (0 == tbsize)
	{
		mylog("no memry leak found and max count allocated so far is %d\n", alsize);
		free(altbl);
		alsize = 0;
	}
	else
	{
		mylog("%s:memory leak found check count=%d alloc=%d\n", ALCERR, tbsize, alsize);
		for (i = 0; i < tbsize; i++)
		{
			mylog("%s:leak = %p(%d)\n", ALCERR, altbl[i].aladr, altbl[i].len);
		}
	}
}