ObjFW  Check-in [a2b4238850]

Overview
Comment:Future-proof some code using freeWhenDone

While -[OFData initWithDataNoCopy:count:itemSize:freeWhenDone:] can
currently never throw, it might in the future, and when it does so in
the future, these would be memory leaks then.

Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: a2b42388504c320dab245ce47c256e4eb5af1d7fcc92e07b17607d58e0537daa
User & Date: js on 2020-11-06 02:24:14
Other Links: manifest | tags
Context
2020-11-06
02:37
OFString: Make freeWhenDone behave like in OFData check-in: 0f260e0fd5 user: js tags: trunk
02:24
Future-proof some code using freeWhenDone check-in: a2b4238850 user: js tags: trunk
02:12
OFData: Make parameter order more consistent check-in: 9d74d1b74e user: js tags: trunk
Changes

Modified src/OFArray.m from [84ddc75d7b] to [a1c992248d].

242
243
244
245
246
247
248





249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
{
	size_t count = self.count;
	id *buffer = of_malloc(count, sizeof(id));

	@try {
		[self getObjects: buffer
			 inRange: of_range(0, count)];





	} @catch (id e) {
		free(buffer);
		@throw e;
	}

	return [OFData dataWithItemsNoCopy: buffer
				     count: count
				  itemSize: sizeof(id)
			      freeWhenDone: true].items;
}

- (id)copy
{
	return [self retain];
}








>
>
>
>
>




<
<
<
<
<







242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257





258
259
260
261
262
263
264
{
	size_t count = self.count;
	id *buffer = of_malloc(count, sizeof(id));

	@try {
		[self getObjects: buffer
			 inRange: of_range(0, count)];

		return [OFData dataWithItemsNoCopy: buffer
					     count: count
					  itemSize: sizeof(id)
				      freeWhenDone: true].items;
	} @catch (id e) {
		free(buffer);
		@throw e;
	}





}

- (id)copy
{
	return [self retain];
}

Modified src/OFHTTPServer.m from [59a786282c] to [ac3250a22a].

143
144
145
146
147
148
149

150
151




152
153
154
155
156
157
158
		    ? of_ascii_toupper(*tmp)
		    : of_ascii_tolower(*tmp));

		firstLetter = false;
		tmp++;
	}


	return [OFString stringWithUTF8StringNoCopy: cString
				       freeWhenDone: true];




}

@implementation OFHTTPServerResponse
- (instancetype)initWithSocket: (OFStreamSocket *)sock
			server: (OFHTTPServer *)server
		       request: (OFHTTPRequest *)request
{







>
|
|
>
>
>
>







143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
		    ? of_ascii_toupper(*tmp)
		    : of_ascii_tolower(*tmp));

		firstLetter = false;
		tmp++;
	}

	@try {
		return [OFString stringWithUTF8StringNoCopy: cString
					       freeWhenDone: true];
	} @catch (id e) {
		free(cString);
		@throw e;
	}
}

@implementation OFHTTPServerResponse
- (instancetype)initWithSocket: (OFStreamSocket *)sock
			server: (OFHTTPServer *)server
		       request: (OFHTTPRequest *)request
{

Modified src/OFString+URLEncoding.m from [4c3b80addf] to [329955234f].

136
137
138
139
140
141
142

143
144
145



146

147

	@try {
		retCString = of_realloc(retCString, 1, i + 1);
	} @catch (OFOutOfMemoryException *e) {
		/* We don't care if it fails, as we only made it smaller. */
	}


	return [OFString stringWithUTF8StringNoCopy: retCString
					     length: i
				       freeWhenDone: true];



}

@end







>
|
|
|
>
>
>
|
>

136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152

	@try {
		retCString = of_realloc(retCString, 1, i + 1);
	} @catch (OFOutOfMemoryException *e) {
		/* We don't care if it fails, as we only made it smaller. */
	}

	@try {
		return [OFString stringWithUTF8StringNoCopy: retCString
						     length: i
					       freeWhenDone: true];
	} @catch (id e) {
		free(retCString);
		@throw e;
	}
}
@end

Modified src/OFString.m from [75be1c56bd] to [ab9d9164f8].

1045
1046
1047
1048
1049
1050
1051
1052

1053
1054
1055




1056
1057
1058
1059
1060
1061
1062
1063

		tmp[(size_t)fileSize] = '\0';
	} @catch (id e) {
		[self release];
		@throw e;
	}

	if (encoding == OF_STRING_ENCODING_UTF_8)

		self = [self initWithUTF8StringNoCopy: tmp
					       length: (size_t)fileSize
					 freeWhenDone: true];




	else {
		@try {
			self = [self initWithCString: tmp
					    encoding: encoding
					      length: (size_t)fileSize];
		} @finally {
			free(tmp);
		}







|
>
|
|
|
>
>
>
>
|







1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068

		tmp[(size_t)fileSize] = '\0';
	} @catch (id e) {
		[self release];
		@throw e;
	}

	if (encoding == OF_STRING_ENCODING_UTF_8) {
		@try {
			self = [self initWithUTF8StringNoCopy: tmp
						       length: (size_t)fileSize
						 freeWhenDone: true];
		} @catch (id e) {
			free(tmp);
			@throw e;
		}
	} else {
		@try {
			self = [self initWithCString: tmp
					    encoding: encoding
					      length: (size_t)fileSize];
		} @finally {
			free(tmp);
		}
1431
1432
1433
1434
1435
1436
1437

1438
1439
1440




1441
1442
1443
1444
1445
1446
1447
		}

		break;
	default:
		@throw [OFInvalidEncodingException exception];
	}


	return [OFData dataWithItemsNoCopy: cString
				     count: cStringLength + 1
			      freeWhenDone: true].items;




}

- (const char *)cStringWithEncoding: (of_string_encoding_t)encoding
{
	return [self of_cStringWithEncoding: encoding
				      lossy: false];
}







>
|
|
|
>
>
>
>







1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
		}

		break;
	default:
		@throw [OFInvalidEncodingException exception];
	}

	@try {
		return [OFData dataWithItemsNoCopy: cString
					     count: cStringLength + 1
				      freeWhenDone: true].items;
	} @catch (id e) {
		free(cString);
		@throw e;
	}
}

- (const char *)cStringWithEncoding: (of_string_encoding_t)encoding
{
	return [self of_cStringWithEncoding: encoding
				      lossy: false];
}
2501
2502
2503
2504
2505
2506
2507





2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
	size_t length = self.length;
	of_unichar_t *buffer;

	buffer = of_malloc(length, sizeof(of_unichar_t));
	@try {
		[self getCharacters: buffer
			    inRange: of_range(0, length)];





	} @catch (id e) {
		free(buffer);
		@throw e;
	}

	return [OFData dataWithItemsNoCopy: buffer
				     count: length
				  itemSize: sizeof(of_unichar_t)
			      freeWhenDone: true].items;
}

- (const of_char16_t *)UTF16String
{
	return [self UTF16StringWithByteOrder: OF_BYTE_ORDER_NATIVE];
}








>
>
>
>
>




<
<
<
<
<







2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526





2527
2528
2529
2530
2531
2532
2533
	size_t length = self.length;
	of_unichar_t *buffer;

	buffer = of_malloc(length, sizeof(of_unichar_t));
	@try {
		[self getCharacters: buffer
			    inRange: of_range(0, length)];

		return [OFData dataWithItemsNoCopy: buffer
					     count: length
					  itemSize: sizeof(of_unichar_t)
				      freeWhenDone: true].items;
	} @catch (id e) {
		free(buffer);
		@throw e;
	}





}

- (const of_char16_t *)UTF16String
{
	return [self UTF16StringWithByteOrder: OF_BYTE_ORDER_NATIVE];
}

2564
2565
2566
2567
2568
2569
2570

2571
2572
2573
2574




2575
2576
2577
2578
2579
2580
2581
		buffer = of_realloc(buffer, j + 1, sizeof(of_char16_t));
	} @catch (OFOutOfMemoryException *e) {
		/* We don't care, as we only tried to make it smaller */
	}

	objc_autoreleasePoolPop(pool);


	return [OFData dataWithItemsNoCopy: buffer
				     count: j + 1
				  itemSize: sizeof(of_char16_t)
			      freeWhenDone: true].items;




}

- (size_t)UTF16StringLength
{
	const of_unichar_t *characters = self.characters;
	size_t length, UTF16StringLength;








>
|
|
|
|
>
>
>
>







2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
		buffer = of_realloc(buffer, j + 1, sizeof(of_char16_t));
	} @catch (OFOutOfMemoryException *e) {
		/* We don't care, as we only tried to make it smaller */
	}

	objc_autoreleasePoolPop(pool);

	@try {
		return [OFData dataWithItemsNoCopy: buffer
					     count: j + 1
					  itemSize: sizeof(of_char16_t)
				      freeWhenDone: true].items;
	} @catch (id e) {
		free(buffer);
		@throw e;
	}
}

- (size_t)UTF16StringLength
{
	const of_unichar_t *characters = self.characters;
	size_t length, UTF16StringLength;

2599
2600
2601
2602
2603
2604
2605









2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
	of_char32_t *buffer;

	buffer = of_malloc(length + 1, sizeof(of_char32_t));
	@try {
		[self getCharacters: buffer
			    inRange: of_range(0, length)];
		buffer[length] = 0;









	} @catch (id e) {
		free(buffer);
		@throw e;
	}

	if (byteOrder != OF_BYTE_ORDER_NATIVE)
		for (size_t i = 0; i < length; i++)
			buffer[i] = OF_BSWAP32(buffer[i]);

	return [OFData dataWithItemsNoCopy: buffer
				     count: length + 1
				  itemSize: sizeof(of_char32_t)
			      freeWhenDone: true].items;
}

- (OFData *)dataWithEncoding: (of_string_encoding_t)encoding
{
	void *pool = objc_autoreleasePoolPush();
	OFData *data =
	    [OFData dataWithItems: [self cStringWithEncoding: encoding]







>
>
>
>
>
>
>
>
>




<
<
<
<
<
<
<
<
<







2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633









2634
2635
2636
2637
2638
2639
2640
	of_char32_t *buffer;

	buffer = of_malloc(length + 1, sizeof(of_char32_t));
	@try {
		[self getCharacters: buffer
			    inRange: of_range(0, length)];
		buffer[length] = 0;

		if (byteOrder != OF_BYTE_ORDER_NATIVE)
			for (size_t i = 0; i < length; i++)
				buffer[i] = OF_BSWAP32(buffer[i]);

		return [OFData dataWithItemsNoCopy: buffer
					     count: length + 1
					  itemSize: sizeof(of_char32_t)
				      freeWhenDone: true].items;
	} @catch (id e) {
		free(buffer);
		@throw e;
	}









}

- (OFData *)dataWithEncoding: (of_string_encoding_t)encoding
{
	void *pool = objc_autoreleasePoolPush();
	OFData *data =
	    [OFData dataWithItems: [self cStringWithEncoding: encoding]