ObjFW  Check-in [81156ee75a]

Overview
Comment:No self = [self init…] with self-releasing @catch

When using self = [self init…], this already releases self on an
exception, so using it inside of a @try / @catch block that releases
self would lead to a double-release.

Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 81156ee75a9043e67e57fc8ee37c5a886e1bf0578ba127c43d656eeb2208d6c5
User & Date: js on 2018-02-25 17:16:23
Other Links: manifest | tags
Context
2018-02-25
18:10
OFURLHandler_HTTP: Require sockets and threads check-in: f4b11d1e1b user: js tags: trunk
17:16
No self = [self init…] with self-releasing @catch check-in: 81156ee75a user: js tags: trunk
16:06
OFURLHandler: Add a handler for HTTP(S) check-in: 5613565c63 user: js tags: trunk
Changes

Modified src/OFData.m from [f8e15d9a7d] to [8d40ffbf81].

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

	return self;
}

#ifdef OF_HAVE_FILES
- (instancetype)initWithContentsOfFile: (OFString *)path
{



	@try {


		uintmax_t size = [[[OFFileManager defaultManager]
		    attributesOfItemAtPath: path] fileSize];
		char *buffer;

# if UINTMAX_MAX > SIZE_MAX
		if (size > SIZE_MAX)
			@throw [OFOutOfRangeException exception];
# endif

		buffer = malloc((size_t)size);
		if (buffer == NULL)
			@throw [OFOutOfMemoryException
			    exceptionWithRequestedSize: (size_t)size];

		@try {
			OFFile *file = [[OFFile alloc] initWithPath: path
							       mode: @"r"];
			@try {
				[file readIntoBuffer: buffer
					 exactLength: (size_t)size];
			} @finally {
				[file release];
			}








			self = [self initWithItemsNoCopy: buffer
						   count: (size_t)size
					    freeWhenDone: true];
		} @catch (id e) {
			free(buffer);
			@throw e;
		}
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}
#endif








>
>
>

>
>
|

<






|
<



<
|
|
|
|
|
|
|
|
>
>
>

>
>
>
>
|
|
|
|
|
<
<
<
<







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

	return self;
}

#ifdef OF_HAVE_FILES
- (instancetype)initWithContentsOfFile: (OFString *)path
{
	char *buffer = NULL;
	uintmax_t size;

	@try {
		OFFile *file;

		size = [[[OFFileManager defaultManager]
		    attributesOfItemAtPath: path] fileSize];


# if UINTMAX_MAX > SIZE_MAX
		if (size > SIZE_MAX)
			@throw [OFOutOfRangeException exception];
# endif

		if ((buffer = malloc((size_t)size)) == NULL)

			@throw [OFOutOfMemoryException
			    exceptionWithRequestedSize: (size_t)size];


		file = [[OFFile alloc] initWithPath: path
					       mode: @"r"];
		@try {
			[file readIntoBuffer: buffer
				 exactLength: (size_t)size];
		} @finally {
			[file release];
		}
	} @catch (id e) {
		free(buffer);
		[self release];

		@throw e;
	}

	@try {
		self = [self initWithItemsNoCopy: buffer
					   count: (size_t)size
				    freeWhenDone: true];
	} @catch (id e) {
		free(buffer);




		@throw e;
	}

	return self;
}
#endif

349
350
351
352
353
354
355
356
357
358
359

360
361
362
363
364
365
366
367
368
369
370
371
372




373
374
375
376
377
378
379
		[(OFMutableData *)self makeImmutable];

	return self;
}

- (instancetype)initWithSerialization: (OFXMLElement *)element
{
	@try {
		void *pool = objc_autoreleasePoolPush();
		OFString *stringValue;


		if (![[element name] isEqual: [self className]] ||
		    ![[element namespace] isEqual: OF_SERIALIZATION_NS])
			@throw [OFInvalidArgumentException exception];

		stringValue = [element stringValue];

		self = [self initWithBase64EncodedString: stringValue];

		objc_autoreleasePoolPop(pool);
	} @catch (id e) {
		[self release];
		@throw e;
	}





	return self;
}

- (void)dealloc
{
	if (_freeWhenDone)







<
|
|

>





<
<
<
<




>
>
>
>







354
355
356
357
358
359
360

361
362
363
364
365
366
367
368
369




370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
		[(OFMutableData *)self makeImmutable];

	return self;
}

- (instancetype)initWithSerialization: (OFXMLElement *)element
{

	void *pool = objc_autoreleasePoolPush();
	OFString *stringValue;

	@try {
		if (![[element name] isEqual: [self className]] ||
		    ![[element namespace] isEqual: OF_SERIALIZATION_NS])
			@throw [OFInvalidArgumentException exception];

		stringValue = [element stringValue];




	} @catch (id e) {
		[self release];
		@throw e;
	}

	self = [self initWithBase64EncodedString: stringValue];

	objc_autoreleasePoolPop(pool);

	return self;
}

- (void)dealloc
{
	if (_freeWhenDone)

Modified src/OFFile.m from [0526f78e96] to [1cab20f522].

310
311
312
313
314
315
316
317
318
319
320
321
322
323


324
325
326
327





328
329
330
331
332
333
334

	return self;
}

- (instancetype)initWithURL: (OFURL *)URL
		       mode: (OFString *)mode
{
	@try {
		void *pool = objc_autoreleasePoolPush();

		self = [self initWithPath: [URL fileSystemRepresentation]
				     mode: mode];

		objc_autoreleasePoolPop(pool);


	} @catch (id e) {
		[self release];
		@throw e;
	}






	return self;
}

- (instancetype)initWithHandle: (of_file_handle_t)handle
{
	self = [super init];







<
|
<
|
<

<
>
>




>
>
>
>
>







310
311
312
313
314
315
316

317

318

319

320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337

	return self;
}

- (instancetype)initWithURL: (OFURL *)URL
		       mode: (OFString *)mode
{

	void *pool = objc_autoreleasePoolPush();

	OFString *fileSystemRepresentation;



	@try {
		fileSystemRepresentation = [URL fileSystemRepresentation];
	} @catch (id e) {
		[self release];
		@throw e;
	}

	self = [self initWithPath: fileSystemRepresentation
			     mode: mode];

	objc_autoreleasePoolPop(pool);

	return self;
}

- (instancetype)initWithHandle: (of_file_handle_t)handle
{
	self = [super init];

Modified src/OFString.m from [8f9360fade] to [b00db1bc09].

901
902
903
904
905
906
907
908
909
910
911
912
913
914
915




916
917
918
919
920
921
922

- (instancetype)initWithData: (OFData *)data
		    encoding: (of_string_encoding_t)encoding
{
	@try {
		if ([data itemSize] != 1)
			@throw [OFInvalidArgumentException exception];

		self = [self initWithCString: [data items]
				    encoding: encoding
				      length: [data count]];
	} @catch (id e) {
		[self release];
		@throw e;
	}





	return self;
}

- (instancetype)initWithString: (OFString *)string
{
	OF_INVALID_INIT_METHOD







<
<
<
<




>
>
>
>







901
902
903
904
905
906
907




908
909
910
911
912
913
914
915
916
917
918
919
920
921
922

- (instancetype)initWithData: (OFData *)data
		    encoding: (of_string_encoding_t)encoding
{
	@try {
		if ([data itemSize] != 1)
			@throw [OFInvalidArgumentException exception];




	} @catch (id e) {
		[self release];
		@throw e;
	}

	self = [self initWithCString: [data items]
			    encoding: encoding
			      length: [data count]];

	return self;
}

- (instancetype)initWithString: (OFString *)string
{
	OF_INVALID_INIT_METHOD
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043


1044



1045



1046
1047
1048

1049
1050
1051



1052
1053
1054


1055
1056
1057
1058
1059






1060
1061
1062

1063


1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086

1087
1088
1089
1090






1091
1092
1093
1094
1095
1096
1097
1098

1099

1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117




1118
1119
1120
1121
1122
1123
1124
			      encoding: (of_string_encoding_t)encoding
{
	char *tmp;
	uintmax_t fileSize;

	@try {
		void *pool = objc_autoreleasePoolPush();
		OFFile *file;

		@try {
			fileSize = [[[OFFileManager defaultManager]
			    attributesOfItemAtPath: path] fileSize];
		} @catch (OFRetrieveItemAttributesFailedException *e) {
			@throw [OFOpenItemFailedException
			    exceptionWithPath: path
					 mode: @"r"
					errNo: errno];
		}

		objc_autoreleasePoolPop(pool);

# if UINTMAX_MAX > SIZE_MAX
		if (fileSize > SIZE_MAX)
			@throw [OFOutOfRangeException exception];
#endif



		file = [[OFFile alloc] initWithPath: path



					       mode: @"r"];




		@try {
			tmp = [self allocMemoryWithSize: (size_t)fileSize];


			[file readIntoBuffer: tmp
				 exactLength: (size_t)fileSize];



		} @finally {
			[file release];
		}


	} @catch (id e) {
		[self release];
		@throw e;
	}







	self = [self initWithCString: tmp
			    encoding: encoding
			      length: (size_t)fileSize];

	[self freeMemory: tmp];



	return self;
}
#endif

- (instancetype)initWithContentsOfURL: (OFURL *)URL
{
	return [self initWithContentsOfURL: URL
				  encoding: OF_STRING_ENCODING_AUTODETECT];
}

- (instancetype)initWithContentsOfURL: (OFURL *)URL
			     encoding: (of_string_encoding_t)encoding
{
	@try {
		void *pool = objc_autoreleasePoolPush();
		OFData *data = [OFData dataWithContentsOfURL: URL];

		self = [self initWithCString: [data items]
				    encoding: encoding
				      length: [data count]];

		objc_autoreleasePoolPop(pool);

	} @catch (id e) {
		[self release];
		@throw e;
	}







	return self;
}

- (instancetype)initWithSerialization: (OFXMLElement *)element
{
	@try {
		void *pool = objc_autoreleasePoolPush();



		if (![[element namespace] isEqual: OF_SERIALIZATION_NS])
			@throw [OFInvalidArgumentException exception];

		if ([self isKindOfClass: [OFMutableString class]]) {
			if (![[element name] isEqual: @"OFMutableString"])
				@throw [OFInvalidArgumentException exception];
		} else {
			if (![[element name] isEqual: @"OFString"])
				@throw [OFInvalidArgumentException exception];
		}

		self = [self initWithString: [element stringValue]];

		objc_autoreleasePoolPop(pool);
	} @catch (id e) {
		[self release];
		@throw e;
	}





	return self;
}

- (size_t)of_getCString: (char *)cString
	      maxLength: (size_t)maxLength
	       encoding: (of_string_encoding_t)encoding







|


















>
>
|
>
>
>
|
>
>
>


|
>



>
>
>



>
>





>
>
>
>
>
>
|
|
|
>
|
>
>














<
|
|

<
<
<
|
<
>




>
>
>
>
>
>






<
|
>

>











|
<
<




>
>
>
>







1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100

1101
1102
1103



1104

1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121

1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137


1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
			      encoding: (of_string_encoding_t)encoding
{
	char *tmp;
	uintmax_t fileSize;

	@try {
		void *pool = objc_autoreleasePoolPush();
		OFFile *file = nil;

		@try {
			fileSize = [[[OFFileManager defaultManager]
			    attributesOfItemAtPath: path] fileSize];
		} @catch (OFRetrieveItemAttributesFailedException *e) {
			@throw [OFOpenItemFailedException
			    exceptionWithPath: path
					 mode: @"r"
					errNo: errno];
		}

		objc_autoreleasePoolPop(pool);

# if UINTMAX_MAX > SIZE_MAX
		if (fileSize > SIZE_MAX)
			@throw [OFOutOfRangeException exception];
#endif

		/*
		 * We need one extra byte for the terminating zero if we want
		 * to use -[initWithUTF8StringNoCopy:length:freeWhenDone:].
		 */
		if (SIZE_MAX - (size_t)fileSize < 1)
			@throw [OFOutOfRangeException exception];

		if ((tmp = malloc((size_t)fileSize + 1)) == NULL)
			@throw [OFOutOfMemoryException
			    exceptionWithRequestedSize: (size_t)fileSize];

		@try {
			file = [[OFFile alloc] initWithPath: path
						       mode: @"r"];

			[file readIntoBuffer: tmp
				 exactLength: (size_t)fileSize];
		} @catch (id e) {
			free(tmp);
			@throw e;
		} @finally {
			[file release];
		}

		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);
		}
	}

	return self;
}
#endif

- (instancetype)initWithContentsOfURL: (OFURL *)URL
{
	return [self initWithContentsOfURL: URL
				  encoding: OF_STRING_ENCODING_AUTODETECT];
}

- (instancetype)initWithContentsOfURL: (OFURL *)URL
			     encoding: (of_string_encoding_t)encoding
{

	void *pool = objc_autoreleasePoolPush();
	OFData *data;




	@try {

		data = [OFData dataWithContentsOfURL: URL];
	} @catch (id e) {
		[self release];
		@throw e;
	}

	self = [self initWithCString: [data items]
			    encoding: encoding
			      length: [data count]];

	objc_autoreleasePoolPop(pool);

	return self;
}

- (instancetype)initWithSerialization: (OFXMLElement *)element
{

	void *pool = objc_autoreleasePoolPush();
	OFString *stringValue;

	@try {
		if (![[element namespace] isEqual: OF_SERIALIZATION_NS])
			@throw [OFInvalidArgumentException exception];

		if ([self isKindOfClass: [OFMutableString class]]) {
			if (![[element name] isEqual: @"OFMutableString"])
				@throw [OFInvalidArgumentException exception];
		} else {
			if (![[element name] isEqual: @"OFString"])
				@throw [OFInvalidArgumentException exception];
		}

		stringValue = [element stringValue];


	} @catch (id e) {
		[self release];
		@throw e;
	}

	self = [self initWithString: stringValue];

	objc_autoreleasePoolPop(pool);

	return self;
}

- (size_t)of_getCString: (char *)cString
	      maxLength: (size_t)maxLength
	       encoding: (of_string_encoding_t)encoding

Modified src/OFURL.m from [5197d0902a] to [bb7c13caac].

596
597
598
599
600
601
602


603
604
605
606
607
608
609
610
611
612
613
614
615
616



617
618
619
620
621
622
623

	return self;
}

#ifdef OF_HAVE_FILES
- (instancetype)initFileURLWithPath: (OFString *)path
{


	@try {
		void *pool = objc_autoreleasePoolPush();
		bool isDirectory;

		isDirectory = ([path hasSuffix: OF_PATH_DELIMITER_STRING] ||
		    [OFURLHandler_file of_directoryExistsAtPath: path]);
		self = [self initFileURLWithPath: path
				     isDirectory: isDirectory];

		objc_autoreleasePoolPop(pool);
	} @catch (id e) {
		[self release];
		@throw e;
	}




	return self;
}

- (instancetype)initFileURLWithPath: (OFString *)path
			isDirectory: (bool)isDirectory
{







>
>


<



<
<






>
>
>







596
597
598
599
600
601
602
603
604
605
606

607
608
609


610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625

	return self;
}

#ifdef OF_HAVE_FILES
- (instancetype)initFileURLWithPath: (OFString *)path
{
	bool isDirectory;

	@try {
		void *pool = objc_autoreleasePoolPush();


		isDirectory = ([path hasSuffix: OF_PATH_DELIMITER_STRING] ||
		    [OFURLHandler_file of_directoryExistsAtPath: path]);



		objc_autoreleasePoolPop(pool);
	} @catch (id e) {
		[self release];
		@throw e;
	}

	self = [self initFileURLWithPath: path
			     isDirectory: isDirectory];

	return self;
}

- (instancetype)initFileURLWithPath: (OFString *)path
			isDirectory: (bool)isDirectory
{
674
675
676
677
678
679
680
681
682

683

684
685
686
687
688
689
690
691
692
693
694




695
696
697
698
699
700
701

	return self;
}
#endif

- (instancetype)initWithSerialization: (OFXMLElement *)element
{
	@try {
		void *pool = objc_autoreleasePoolPush();



		if (![[element name] isEqual: [self className]] ||
		    ![[element namespace] isEqual: OF_SERIALIZATION_NS])
			@throw [OFInvalidArgumentException exception];

		self = [self initWithString: [element stringValue]];

		objc_autoreleasePoolPop(pool);
	} @catch (id e) {
		[self release];
		@throw e;
	}





	return self;
}

- (void)dealloc
{
	[_URLEncodedScheme release];







<
|
>

>




|
<
<




>
>
>
>







676
677
678
679
680
681
682

683
684
685
686
687
688
689
690
691


692
693
694
695
696
697
698
699
700
701
702
703
704
705
706

	return self;
}
#endif

- (instancetype)initWithSerialization: (OFXMLElement *)element
{

	void *pool = objc_autoreleasePoolPush();
	OFString *stringValue;

	@try {
		if (![[element name] isEqual: [self className]] ||
		    ![[element namespace] isEqual: OF_SERIALIZATION_NS])
			@throw [OFInvalidArgumentException exception];

		stringValue = [element stringValue];


	} @catch (id e) {
		[self release];
		@throw e;
	}

	self = [self initWithString: stringValue];

	objc_autoreleasePoolPop(pool);

	return self;
}

- (void)dealloc
{
	[_URLEncodedScheme release];