ObjFW  Check-in [4073a31454]

Overview
Comment:OFXMLFactory: Fix FIXMEs, TODOs and possible off-by-one.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 4073a31454ade2622f6a124466930cc6bb2808baf3df1cfacc82d6d5a5a0fea6
User & Date: js on 2008-12-07 12:41:35
Other Links: manifest | tags
Context
2008-12-07
21:58
Server support for OFTCPSocket. check-in: 0d5b08e43e user: js tags: trunk
12:41
OFXMLFactory: Fix FIXMEs, TODOs and possible off-by-one. check-in: 4073a31454 user: js tags: trunk
10:48
Very small API change. check-in: 88053589dc user: js tags: trunk
Changes

Modified src/OFXMLFactory.m from [d880516120] to [b1d2c0931c].

30
31
32
33
34
35
36
37
38


39
40
41
42
43
44
45
30
31
32
33
34
35
36


37
38
39
40
41
42
43
44
45







-
-
+
+








static inline BOOL
xf_resize_chars(char **str, size_t *len, size_t add)
{
	char *str2;
	size_t len2;

	/* FIXME: Check for overflows on add */

	if (add > SIZE_MAX - *len)
		[[OFOutOfRangeException newWithObject: nil] raise];
	len2 = *len + add;
	
	if ((str2 = realloc(*str, len2)) == NULL) {
		if (*str)
			free(*str);
		*str = NULL;
		return NO;
53
54
55
56
57
58
59
60
61


62



63
64
65
66
67
68
69
53
54
55
56
57
58
59


60
61
62
63
64
65
66
67
68
69
70
71
72







-
-
+
+

+
+
+








static inline BOOL
xf_resize_wchars(wchar_t **str, size_t *len, size_t add)
{
	wchar_t *str2;
	size_t len2;

	/* FIXME: Check for overflows on add and multiply */

	if (add > SIZE_MAX - *len)
		[[OFOutOfRangeException newWithObject: nil] raise];
	len2 = *len + add;

	if (len2 > SIZE_MAX / sizeof(wchar_t))
		[[OFOutOfRangeException newWithObject: nil] raise];
	
	if ((str2 = realloc(*str, len2 * sizeof(wchar_t))) == NULL) {
		if (*str)
			free(*str);
		*str = NULL;
		return NO;
	}
109
110
111
112
113
114
115



116
117

118
119

120
121
122
123
124
125
126
112
113
114
115
116
117
118
119
120
121
122

123
124

125
126
127
128
129
130
131
132







+
+
+

-
+

-
+







@implementation OFXMLFactory
+ (char*)escapeCString: (const char*)s
{
	char *ret;
	size_t i, j, len, nlen;

	len = nlen = strlen(s);
	if (SIZE_MAX - len < 1)
		[[OFOutOfRangeException newWithObject: nil] raise];
	nlen++;

	if ((ret = malloc(len + 1)) == NULL)
	if ((ret = malloc(nlen)) == NULL)
		[[OFNoMemException newWithObject: nil
					 andSize: len + 1] raise];
					 andSize: nlen] raise];

	for (i = j = 0; i < len; i++) {
		switch (s[i]) {
		case '<':
			if (OF_UNLIKELY(!xf_add2chars(&ret, &nlen, &j, "&lt;")))
				[[OFNoMemException newWithObject: nil
							 andSize: nlen + 4]
165
166
167
168
169
170
171



172


173
174


175
176

177
178
179
180
181
182
183
171
172
173
174
175
176
177
178
179
180
181
182
183


184
185
186

187
188
189
190
191
192
193
194







+
+
+

+
+
-
-
+
+

-
+








+ (wchar_t*)escapeWideCString: (const wchar_t*)s
{
	wchar_t *ret;
	size_t i, j, len, nlen;

	len = nlen = wcslen(s);
	if (SIZE_MAX - len < 1)
		[[OFOutOfRangeException newWithObject: nil] raise];
	nlen++;

	if (nlen > SIZE_MAX / sizeof(wchar_t))
		[[OFOutOfRangeException newWithObject: nil] raise];
	/* FIXME: Check for overflow in multiply */
	if ((ret = malloc((len + 1) * sizeof(wchar_t))) == NULL)

	if ((ret = malloc(nlen * sizeof(wchar_t))) == NULL)
		[[OFNoMemException newWithObject: nil
					 andSize: (len + 1) * sizeof(wchar_t)]
					 andSize: nlen * sizeof(wchar_t)]
		     raise];

	for (i = j = 0; i < len; i++) {
		switch (s[i]) {
		case L'<':
			if (OF_UNLIKELY(!xf_add2wchars(&ret, &nlen, &j,
			    L"&lt;")))
238
239
240
241
242
243
244
245





246
247
248
249
250
251
252
249
250
251
252
253
254
255

256
257
258
259
260
261
262
263
264
265
266
267







-
+
+
+
+
+







	      andData: (const char*)data, ...
{
	char *arg, *val, *xml;
	size_t i, len;
	va_list args;

	/* Start of tag */
	len = strlen(name) + 3;
	len = strlen(name);
	if (SIZE_MAX - len < 3)
		[[OFOutOfRangeException newWithObject: nil] raise];
	len += 3;

	if ((xml = malloc(len)) == NULL)
		[[OFNoMemException newWithObject: nil
					 andSize: len] raise];

	i = 0;
	xml[i++] = '<';
	memcpy(xml + i, name, strlen(name));
333
334
335
336
337
338
339
340
341
342









343
344
345
346
347
348
349
348
349
350
351
352
353
354



355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370







-
-
-
+
+
+
+
+
+
+
+
+







		     andData: (const wchar_t*)data, ...
{
	wchar_t *arg, *val, *xml;
	size_t i, len;
	va_list args;

	/* Start of tag */
	len = wcslen(name) + 3;
	/* TODO: Check for multiply overflow */
	if ((xml = malloc(len * sizeof(wchar_t*))) == NULL)
	len = wcslen(name);
	if (SIZE_MAX - len < 3)
		[[OFOutOfRangeException newWithObject: nil] raise];
	len += 3;

	if (len > SIZE_MAX / sizeof(wchar_t))
		[[OFOutOfRangeException newWithObject: nil] raise];

	if ((xml = malloc(len * sizeof(wchar_t))) == NULL)
		[[OFNoMemException newWithObject: nil
					 andSize: len * sizeof(wchar_t)] raise];

	i = 0;
	xml[i++] = L'<';
	wmemcpy(xml + i, name, wcslen(name));
	i += wcslen(name);
433
434
435
436
437
438
439
440




441
442
443
444
445
446
447
454
455
456
457
458
459
460

461
462
463
464
465
466
467
468
469
470
471







-
+
+
+
+







{
	char *ret;
	size_t i, len, pos;

	if (strs[0] == NULL)
		return NULL;

	len = strlen(*strs) + 1;
	len = strlen(*strs);
	if (SIZE_MAX - len < 1)
		[[OFOutOfRangeException newWithObject: nil] raise];
	len++;
	
	if ((ret = malloc(len)) == NULL)
		[[OFNoMemException newWithObject: nil
					 andSize: len] raise];

	memcpy(ret, strs[0], len - 1);
	pos = len - 1;
466
467
468
469
470
471
472
473
474
475








476
477
478
479
480
481
482
490
491
492
493
494
495
496



497
498
499
500
501
502
503
504
505
506
507
508
509
510
511







-
-
-
+
+
+
+
+
+
+
+







{
	wchar_t *ret;
	size_t i, len, pos;

	if (strs[0] == NULL)
		return NULL;

	len = wcslen(*strs) + 1;
	
	/* FIXME: Check for overflow on multiply */
	len = wcslen(*strs);
	if (SIZE_MAX - len < 1)
		[[OFOutOfRangeException newWithObject: nil] raise];
	len++;

	if (len > SIZE_MAX - sizeof(wchar_t))
		[[OFOutOfRangeException newWithObject: nil] raise];

	if ((ret = malloc(len * sizeof(wchar_t))) == NULL)
		[[OFNoMemException newWithObject: nil
					 andSize: len * sizeof(wchar_t)] raise];

	wmemcpy(ret, strs[0], len - 1);
	pos = len - 1;