ObjFW  Check-in [f38744df74]

Overview
Comment:OF_INVALID_INDEX -> OF_NOT_FOUND.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: f38744df743f1addc4ba062b15365ff85dcc61c4937b8647c89590c093f7848f
User & Date: js on 2012-10-14 00:59:55
Other Links: manifest | tags
Context
2012-10-14
10:36
More integer overflow checks. check-in: 53af033b0e user: js tags: trunk
00:59
OF_INVALID_INDEX -> OF_NOT_FOUND. check-in: f38744df74 user: js tags: trunk
00:54
range.start -> range.location. check-in: c9433ea60d user: js tags: trunk
Changes

Modified src/OFArray.h from [3cf581ad26] to [d93b4c3a6a].

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
 *
 * \return The objects of the array as a C array
 */
- (id*)objects;

/**
 * \brief Returns the index of the first object that is equivalent to the
 *	  specified object or OF_INVALID_INDEX if it was not found.
 *
 * \param object The object whose index is returned
 * \return The index of the first object equivalent to the specified object
 *	   or OF_INVALID_INDEX if it was not found
 */
- (size_t)indexOfObject: (id)object;

/**
 * \brief Returns the index of the first object that has the same address as the
 *	  specified object or OF_INVALID_INDEX if it was not found.
 *
 * \param object The object whose index is returned
 * \return The index of the first object that has the same aaddress as
 *	   the specified object or OF_INVALID_INDEX if it was not found
 */
- (size_t)indexOfObjectIdenticalTo: (id)object;

/**
 * \brief Checks whether the array contains an object with the specified
 *	  address.
 *







|



|





|



|







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
 *
 * \return The objects of the array as a C array
 */
- (id*)objects;

/**
 * \brief Returns the index of the first object that is equivalent to the
 *	  specified object or OF_NOT_FOUND if it was not found.
 *
 * \param object The object whose index is returned
 * \return The index of the first object equivalent to the specified object
 *	   or OF_NOT_FOUND if it was not found
 */
- (size_t)indexOfObject: (id)object;

/**
 * \brief Returns the index of the first object that has the same address as the
 *	  specified object or OF_NOT_FOUND if it was not found.
 *
 * \param object The object whose index is returned
 * \return The index of the first object that has the same aaddress as
 *	   the specified object or OF_NOT_FOUND if it was not found
 */
- (size_t)indexOfObjectIdenticalTo: (id)object;

/**
 * \brief Checks whether the array contains an object with the specified
 *	  address.
 *

Modified src/OFArray.m from [462ac6c7f4] to [c7257b8dbc].

280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
{
	size_t i, count = [self count];

	for (i = 0; i < count; i++)
		if ([[self objectAtIndex: i] isEqual: object])
			return i;

	return OF_INVALID_INDEX;
}

- (size_t)indexOfObjectIdenticalTo: (id)object
{
	size_t i, count = [self count];

	for (i = 0; i < count; i++)
		if ([self objectAtIndex: i] == object)
			return i;

	return OF_INVALID_INDEX;
}

- (BOOL)containsObject: (id)object
{
	return ([self indexOfObject: object] != OF_INVALID_INDEX);
}

- (BOOL)containsObjectIdenticalTo: (id)object
{
	return ([self indexOfObjectIdenticalTo: object] != OF_INVALID_INDEX);
}

- (id)firstObject
{
	if ([self count] > 0)
		return [self objectAtIndex: 0];








|










|




|




|







280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
{
	size_t i, count = [self count];

	for (i = 0; i < count; i++)
		if ([[self objectAtIndex: i] isEqual: object])
			return i;

	return OF_NOT_FOUND;
}

- (size_t)indexOfObjectIdenticalTo: (id)object
{
	size_t i, count = [self count];

	for (i = 0; i < count; i++)
		if ([self objectAtIndex: i] == object)
			return i;

	return OF_NOT_FOUND;
}

- (BOOL)containsObject: (id)object
{
	return ([self indexOfObject: object] != OF_NOT_FOUND);
}

- (BOOL)containsObjectIdenticalTo: (id)object
{
	return ([self indexOfObjectIdenticalTo: object] != OF_NOT_FOUND);
}

- (id)firstObject
{
	if ([self count] > 0)
		return [self objectAtIndex: 0];

Modified src/OFArray_adjacent.m from [96ab47295a] to [d65c9872b3].

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
	id *objects = [array cArray];
	size_t i, count = [array count];

	for (i = 0; i < count; i++)
		if ([objects[i] isEqual: object])
			return i;

	return OF_INVALID_INDEX;
}

- (size_t)indexOfObjectIdenticalTo: (id)object
{
	id *objects = [array cArray];
	size_t i, count = [array count];

	for (i = 0; i < count; i++)
		if (objects[i] == object)
			return i;

	return OF_INVALID_INDEX;
}


- (OFArray*)objectsInRange: (of_range_t)range
{
	size_t count;








|











|







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
	id *objects = [array cArray];
	size_t i, count = [array count];

	for (i = 0; i < count; i++)
		if ([objects[i] isEqual: object])
			return i;

	return OF_NOT_FOUND;
}

- (size_t)indexOfObjectIdenticalTo: (id)object
{
	id *objects = [array cArray];
	size_t i, count = [array count];

	for (i = 0; i < count; i++)
		if (objects[i] == object)
			return i;

	return OF_NOT_FOUND;
}


- (OFArray*)objectsInRange: (of_range_t)range
{
	size_t count;

Modified src/OFArray_subarray.m from [9c788f37d8] to [327edac8fd].

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
}

- (size_t)indexOfObject: (id)object
{
	size_t index = [array indexOfObject: object];

	if (index < range.location)
		return OF_INVALID_INDEX;

	index -= range.location;

	if (index >= range.length)
		return OF_INVALID_INDEX;

	return index;
}

- (size_t)indexOfObjectIdenticalTo: (id)object
{
	size_t index = [array indexOfObjectIdenticalTo: object];

	if (index < range.location)
		return OF_INVALID_INDEX;

	index -= range.location;

	if (index >= range.length)
		return OF_INVALID_INDEX;

	return index;
}

- (OFArray*)objectsInRange: (of_range_t)range_
{
	if (range_.location + range_.length > range.length)







|




|









|




|







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
}

- (size_t)indexOfObject: (id)object
{
	size_t index = [array indexOfObject: object];

	if (index < range.location)
		return OF_NOT_FOUND;

	index -= range.location;

	if (index >= range.length)
		return OF_NOT_FOUND;

	return index;
}

- (size_t)indexOfObjectIdenticalTo: (id)object
{
	size_t index = [array indexOfObjectIdenticalTo: object];

	if (index < range.location)
		return OF_NOT_FOUND;

	index -= range.location;

	if (index >= range.length)
		return OF_NOT_FOUND;

	return index;
}

- (OFArray*)objectsInRange: (of_range_t)range_
{
	if (range_.location + range_.length > range.length)

Modified src/OFHTTPRequest.m from [997525c718] to [2442ea8723].

409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
					line = [sock readLine];
				} @catch (OFInvalidEncodingException *e) {
					@throw [OFInvalidServerReplyException
					    exceptionWithClass: [self class]];
				}

				range = [line rangeOfString: @";"];
				if (range.location != OF_INVALID_INDEX)
					line = [line substringWithRange:
					    of_range(0, range.location)];

				@try {
					toRead =
					    (size_t)[line hexadecimalValue];
				} @catch (OFInvalidFormatException *e) {







|







409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
					line = [sock readLine];
				} @catch (OFInvalidEncodingException *e) {
					@throw [OFInvalidServerReplyException
					    exceptionWithClass: [self class]];
				}

				range = [line rangeOfString: @";"];
				if (range.location != OF_NOT_FOUND)
					line = [line substringWithRange:
					    of_range(0, range.location)];

				@try {
					toRead =
					    (size_t)[line hexadecimalValue];
				} @catch (OFInvalidFormatException *e) {

Modified src/OFObject.h from [b910dda1d9] to [b6dffac34a].

96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# define OF_WEAK_UNAVAILABLE
# define __unsafe_unretained
# define __bridge
# define __autoreleasing
#endif

#define OF_RETAIN_COUNT_MAX UINT_MAX
#define OF_INVALID_INDEX SIZE_MAX

/**
 * \brief A result of a comparison.
 */
typedef enum of_comparison_result_t {
	/// The left object is smaller than the right
	OF_ORDERED_ASCENDING = -1,







|







96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# define OF_WEAK_UNAVAILABLE
# define __unsafe_unretained
# define __bridge
# define __autoreleasing
#endif

#define OF_RETAIN_COUNT_MAX UINT_MAX
#define OF_NOT_FOUND SIZE_MAX

/**
 * \brief A result of a comparison.
 */
typedef enum of_comparison_result_t {
	/// The left object is smaller than the right
	OF_ORDERED_ASCENDING = -1,

Modified src/OFString.h from [0ffb1811bb] to [9619983a96].

615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
	      inRange: (of_range_t)range;

/**
 * \brief Returns the range of the first occurrence of the string.
 *
 * \param string The string to search
 * \return The range of the first occurrence of the string or a range with
 *	   OF_INVALID_INDEX as start position if it was not found
 */
- (of_range_t)rangeOfString: (OFString*)string;

/**
 * \brief Returns the range of the string.
 *
 * \param string The string to search
 * \param options Options modifying search behaviour
 * \return The range of the first occurrence of the string or a range with
 *	   OF_INVALID_INDEX as start position if it was not found
 */
- (of_range_t)rangeOfString: (OFString*)string
		    options: (of_string_search_options_t)options;

/**
 * \brief Returns the range of the string in the specified range.
 *
 * \param string The string to search
 * \param options Options modifying search behaviour
 * \param range The range in which to search
 * \return The range of the first occurrence of the string or a range with
 *	   OF_INVALID_INDEX as start position if it was not found
 */
- (of_range_t)rangeOfString: (OFString*)string
		    options: (of_string_search_options_t)options
		      range: (of_range_t)range;

/**
 * \brief Returns whether the string contains the specified string.







|









|











|







615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
	      inRange: (of_range_t)range;

/**
 * \brief Returns the range of the first occurrence of the string.
 *
 * \param string The string to search
 * \return The range of the first occurrence of the string or a range with
 *	   OF_NOT_FOUND as start position if it was not found
 */
- (of_range_t)rangeOfString: (OFString*)string;

/**
 * \brief Returns the range of the string.
 *
 * \param string The string to search
 * \param options Options modifying search behaviour
 * \return The range of the first occurrence of the string or a range with
 *	   OF_NOT_FOUND as start position if it was not found
 */
- (of_range_t)rangeOfString: (OFString*)string
		    options: (of_string_search_options_t)options;

/**
 * \brief Returns the range of the string in the specified range.
 *
 * \param string The string to search
 * \param options Options modifying search behaviour
 * \param range The range in which to search
 * \return The range of the first occurrence of the string or a range with
 *	   OF_NOT_FOUND as start position if it was not found
 */
- (of_range_t)rangeOfString: (OFString*)string
		    options: (of_string_search_options_t)options
		      range: (of_range_t)range;

/**
 * \brief Returns whether the string contains the specified string.

Modified src/OFString.m from [651cb0fc54] to [10f45f3070].

1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
	of_unichar_t *unicodeString;
	size_t i, searchLength;

	if ((searchLength = [string length]) == 0)
		return of_range(0, 0);

	if (searchLength > range.length)
		return of_range(OF_INVALID_INDEX, 0);

	if (range.length > SIZE_MAX / sizeof(of_unichar_t))
		@throw [OFOutOfRangeException exceptionWithClass: [self class]];

	pool = objc_autoreleasePoolPush();

	searchString = [string unicodeString];







|







1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
	of_unichar_t *unicodeString;
	size_t i, searchLength;

	if ((searchLength = [string length]) == 0)
		return of_range(0, 0);

	if (searchLength > range.length)
		return of_range(OF_NOT_FOUND, 0);

	if (range.length > SIZE_MAX / sizeof(of_unichar_t))
		@throw [OFOutOfRangeException exceptionWithClass: [self class]];

	pool = objc_autoreleasePoolPush();

	searchString = [string unicodeString];
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
		}
	} @finally {
		free(unicodeString);
	}

	objc_autoreleasePoolPop(pool);

	return of_range(OF_INVALID_INDEX, 0);
}

- (BOOL)containsString: (OFString*)string
{
	void *pool;
	const of_unichar_t *unicodeString, *searchString;
	size_t i, length, searchLength;







|







1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
		}
	} @finally {
		free(unicodeString);
	}

	objc_autoreleasePoolPop(pool);

	return of_range(OF_NOT_FOUND, 0);
}

- (BOOL)containsString: (OFString*)string
{
	void *pool;
	const of_unichar_t *unicodeString, *searchString;
	size_t i, length, searchLength;

Modified src/OFString_UTF8.m from [e621285ff6] to [f11d00232a].

208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
of_string_utf8_get_position(const char *string, size_t index, size_t length)
{
	size_t i;

	for (i = 0; i <= index; i++)
		if OF_UNLIKELY ((string[i] & 0xC0) == 0x80)
			if (++index > length)
				return OF_INVALID_INDEX;

	return index;
}

@implementation OFString_UTF8
- init
{







|







208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
of_string_utf8_get_position(const char *string, size_t index, size_t length)
{
	size_t i;

	for (i = 0; i <= index; i++)
		if OF_UNLIKELY ((string[i] & 0xC0) == 0x80)
			if (++index > length)
				return OF_NOT_FOUND;

	return index;
}

@implementation OFString_UTF8
- init
{
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
	}

	if (cStringLength == 0)
		return of_range(0, 0);

	if (cStringLength > rangeLength ||
	    rangeStart + rangeLength > s->cStringLength)
		return of_range(OF_INVALID_INDEX, 0);

	if (options & OF_STRING_SEARCH_BACKWARDS) {
		for (i = rangeLength - cStringLength;; i--) {
			if (!memcmp(s->cString + rangeStart + i, cString,
			    cStringLength)) {
				range.location += of_string_utf8_get_index(
				    s->cString + rangeStart, i);
				range.length = [string length];

				return range;
			}

			/* Did not match and we're at the last char */
			if (i == 0)
				return of_range(OF_INVALID_INDEX, 0);
		}
	} else {
		for (i = 0; i <= rangeLength - cStringLength; i++) {
			if (!memcmp(s->cString + rangeStart + i, cString,
			    cStringLength)) {
				range.location += of_string_utf8_get_index(
				    s->cString + rangeStart, i);
				range.length = [string length];

				return range;
			}
		}
	}

	return of_range(OF_INVALID_INDEX, 0);
}

- (BOOL)containsString: (OFString*)string
{
	const char *cString = [string UTF8String];
	size_t i, cStringLength = [string UTF8StringLength];








|














|














|







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
	}

	if (cStringLength == 0)
		return of_range(0, 0);

	if (cStringLength > rangeLength ||
	    rangeStart + rangeLength > s->cStringLength)
		return of_range(OF_NOT_FOUND, 0);

	if (options & OF_STRING_SEARCH_BACKWARDS) {
		for (i = rangeLength - cStringLength;; i--) {
			if (!memcmp(s->cString + rangeStart + i, cString,
			    cStringLength)) {
				range.location += of_string_utf8_get_index(
				    s->cString + rangeStart, i);
				range.length = [string length];

				return range;
			}

			/* Did not match and we're at the last char */
			if (i == 0)
				return of_range(OF_NOT_FOUND, 0);
		}
	} else {
		for (i = 0; i <= rangeLength - cStringLength; i++) {
			if (!memcmp(s->cString + rangeStart + i, cString,
			    cStringLength)) {
				range.location += of_string_utf8_get_index(
				    s->cString + rangeStart, i);
				range.length = [string length];

				return range;
			}
		}
	}

	return of_range(OF_NOT_FOUND, 0);
}

- (BOOL)containsString: (OFString*)string
{
	const char *cString = [string UTF8String];
	size_t i, cStringLength = [string UTF8StringLength];

Modified tests/OFStringTests.m from [42e3523e42] to [8d7d5918e5].

221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
	    R(([s[0] appendFormat: @"%02X", 15])) &&
	    [s[0] isEqual: @"test:1230F"])

	TEST(@"-[rangeOfString:]",
	    [@"π„žΓΆΓΆ" rangeOfString: @"ΓΆΓΆ"].location == 1 &&
	    [@"π„žΓΆΓΆ" rangeOfString: @"ΓΆ"].location == 1 &&
	    [@"π„žΓΆΓΆ" rangeOfString: @"π„ž"].location == 0 &&
	    [@"π„žΓΆΓΆ" rangeOfString: @"x"].location == OF_INVALID_INDEX &&
	    [@"π„žΓΆΓΆ" rangeOfString: @"ΓΆΓΆ"
			  options: OF_STRING_SEARCH_BACKWARDS].location == 1 &&
	    [@"π„žΓΆΓΆ" rangeOfString: @"ΓΆ"
			  options: OF_STRING_SEARCH_BACKWARDS].location == 2 &&
	    [@"π„žΓΆΓΆ" rangeOfString: @"π„ž"
			  options: OF_STRING_SEARCH_BACKWARDS].location == 0 &&
	    [@"π„žΓΆΓΆ" rangeOfString: @"x"
			  options: OF_STRING_SEARCH_BACKWARDS].location ==
	     OF_INVALID_INDEX)

	TEST(@"-[substringWithRange:]",
	    [[@"π„žΓΆΓΆ" substringWithRange: of_range(1, 1)] isEqual: @"ΓΆ"] &&
	    [[@"π„žΓΆΓΆ" substringWithRange: of_range(3, 0)] isEqual: @""])

	EXPECT_EXCEPTION(@"Detect out of range in -[substringWithRange:] #1",
	    OFOutOfRangeException, [@"π„žΓΆΓΆ" substringWithRange: of_range(2, 2)])







|








|







221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
	    R(([s[0] appendFormat: @"%02X", 15])) &&
	    [s[0] isEqual: @"test:1230F"])

	TEST(@"-[rangeOfString:]",
	    [@"π„žΓΆΓΆ" rangeOfString: @"ΓΆΓΆ"].location == 1 &&
	    [@"π„žΓΆΓΆ" rangeOfString: @"ΓΆ"].location == 1 &&
	    [@"π„žΓΆΓΆ" rangeOfString: @"π„ž"].location == 0 &&
	    [@"π„žΓΆΓΆ" rangeOfString: @"x"].location == OF_NOT_FOUND &&
	    [@"π„žΓΆΓΆ" rangeOfString: @"ΓΆΓΆ"
			  options: OF_STRING_SEARCH_BACKWARDS].location == 1 &&
	    [@"π„žΓΆΓΆ" rangeOfString: @"ΓΆ"
			  options: OF_STRING_SEARCH_BACKWARDS].location == 2 &&
	    [@"π„žΓΆΓΆ" rangeOfString: @"π„ž"
			  options: OF_STRING_SEARCH_BACKWARDS].location == 0 &&
	    [@"π„žΓΆΓΆ" rangeOfString: @"x"
			  options: OF_STRING_SEARCH_BACKWARDS].location ==
	     OF_NOT_FOUND)

	TEST(@"-[substringWithRange:]",
	    [[@"π„žΓΆΓΆ" substringWithRange: of_range(1, 1)] isEqual: @"ΓΆ"] &&
	    [[@"π„žΓΆΓΆ" substringWithRange: of_range(3, 0)] isEqual: @""])

	EXPECT_EXCEPTION(@"Detect out of range in -[substringWithRange:] #1",
	    OFOutOfRangeException, [@"π„žΓΆΓΆ" substringWithRange: of_range(2, 2)])