ObjFW  Check-in [3577c0d81c]

Overview
Comment:OFOverflowException -> OFOutOfRangeException.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 3577c0d81c6149946dfe15bd3c2870ce8625abf9ff39f6b49512bd455c3241be
User & Date: js on 2008-11-05 16:11:59
Other Links: manifest | tags
Context
2008-11-05
17:13
Start documenting stuff. check-in: 2690e9848f user: js tags: trunk
16:11
OFOverflowException -> OFOutOfRangeException. check-in: 3577c0d81c user: js tags: trunk
2008-11-02
02:09
Added -data for OFArray & one new test. check-in: 61fc89489a user: js tags: trunk
Changes

Modified src/OFArray.m from [95cb756c66] to [806072ba44].

48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
{
	return data;
}

- (void*)item: (size_t)item
{
	if (item >= items)
		/* FIXME: Maybe OFOutOfRangeException would be better? */
		[[OFOverflowException newWithObject: self] raise];

	return data + item * itemsize;
}

- (void*)last
{
	return data + (items - 1) * itemsize;







<
|







48
49
50
51
52
53
54

55
56
57
58
59
60
61
62
{
	return data;
}

- (void*)item: (size_t)item
{
	if (item >= items)

		[[OFOutOfRangeException newWithObject: self] raise];

	return data + item * itemsize;
}

- (void*)last
{
	return data + (items - 1) * itemsize;
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
	return self;
}

- addNItems: (size_t)nitems
 fromCArray: (void*)carray
{
	if (nitems > SIZE_MAX - items)
		[[OFOverflowException newWithObject: self] raise];

	data = [self resizeMem: data
		      toNItems: items + nitems
			ofSize: itemsize];

	memcpy(data + items * itemsize, carray, nitems * itemsize);
	items += nitems;

	return self;
}

- removeNItems: (size_t)nitems
{
	if (nitems > items)
		[[OFOverflowException newWithObject: self] raise];

	data = [self resizeMem: data
		      toNItems: items - nitems
			ofSize: itemsize];

	items -= nitems;








|














|







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
	return self;
}

- addNItems: (size_t)nitems
 fromCArray: (void*)carray
{
	if (nitems > SIZE_MAX - items)
		[[OFOutOfRangeException newWithObject: self] raise];

	data = [self resizeMem: data
		      toNItems: items + nitems
			ofSize: itemsize];

	memcpy(data + items * itemsize, carray, nitems * itemsize);
	items += nitems;

	return self;
}

- removeNItems: (size_t)nitems
{
	if (nitems > items)
		[[OFOutOfRangeException newWithObject: self] raise];

	data = [self resizeMem: data
		      toNItems: items - nitems
			ofSize: itemsize];

	items -= nitems;

Modified src/OFExceptions.h from [a8e30d777d] to [8fbc274411].

62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
     andPointer: (void*)ptr;
- initWithObject: (id)obj
      andPointer: (void*)ptr;
- (char*)cString;
- (void*)pointer;
@end

@interface OFOverflowException: OFException
+ newWithObject: (id)obj;
- initWithObject: (id)obj;
@end

@interface OFOpenFileFailedException: OFException
{
	char *path;







|







62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
     andPointer: (void*)ptr;
- initWithObject: (id)obj
      andPointer: (void*)ptr;
- (char*)cString;
- (void*)pointer;
@end

@interface OFOutOfRangeException: OFException
+ newWithObject: (id)obj;
- initWithObject: (id)obj;
@end

@interface OFOpenFileFailedException: OFException
{
	char *path;

Modified src/OFExceptions.m from [784d18a0a4] to [8685793b88].

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

- (void*)pointer
{
	return pointer;
}
@end

@implementation OFOverflowException
+ newWithObject: (id)obj
{
	return [[OFOverflowException alloc] initWithObject: obj];
}

- initWithObject: (id)obj
{
	return (self = [super initWithObject: obj]);
}

- (char*)cString
{
	if (string != NULL)
		return string;

	asprintf(&string, "ERROR: Overflow in object of class %s!\n",
	    object != nil ? [object name] : "(null)");

	return string;
}
@end

@implementation OFOpenFileFailedException







|


|












|







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

- (void*)pointer
{
	return pointer;
}
@end

@implementation OFOutOfRangeException
+ newWithObject: (id)obj
{
	return [[OFOutOfRangeException alloc] initWithObject: obj];
}

- initWithObject: (id)obj
{
	return (self = [super initWithObject: obj]);
}

- (char*)cString
{
	if (string != NULL)
		return string;

	asprintf(&string, "ERROR: Value out of range in object of class %s!\n",
	    object != nil ? [object name] : "(null)");

	return string;
}
@end

@implementation OFOpenFileFailedException

Modified src/OFObject.m from [6aad9c4888] to [014c4c85fd].

81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
{
	size_t memsize;
	
	if (nitems == 0 || size == 0)
		return NULL;

	if (size > SIZE_MAX / nitems)
		[[OFOverflowException newWithObject: self] raise];

	memsize = nitems * size;
	return [self getMemWithSize: memsize];
}

- (void*)resizeMem: (void*)ptr
	    toSize: (size_t)size







|







81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
{
	size_t memsize;
	
	if (nitems == 0 || size == 0)
		return NULL;

	if (size > SIZE_MAX / nitems)
		[[OFOutOfRangeException newWithObject: self] raise];

	memsize = nitems * size;
	return [self getMemWithSize: memsize];
}

- (void*)resizeMem: (void*)ptr
	    toSize: (size_t)size
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
	
	if (nitems == 0 || size == 0) {
		[self freeMem: ptr];
		return NULL;
	}

	if (size > SIZE_MAX / nitems)
		[[OFOverflowException newWithObject: self] raise];

	memsize = nitems * size;
	return [self resizeMem: ptr
			toSize: memsize];
}

- freeMem: (void*)ptr;







|







132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
	
	if (nitems == 0 || size == 0) {
		[self freeMem: ptr];
		return NULL;
	}

	if (size > SIZE_MAX / nitems)
		[[OFOutOfRangeException newWithObject: self] raise];

	memsize = nitems * size;
	return [self resizeMem: ptr
			toSize: memsize];
}

- freeMem: (void*)ptr;

Modified tests/OFArray/OFArray.m from [086a701808] to [d6db94dbe6].

41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
	void *p, *q;
	size_t i;

	puts("Trying to add too much to an array...");
	a = [OFArray newWithItemSize: 4096];
	CATCH_EXCEPTION([a addNItems: SIZE_MAX
			  fromCArray: NULL],
	    OFOverflowException)

	puts("Trying to add something after that error...");
	p = [a getMemWithSize: 4096];
	memset(p, 255, 4096);
	[a add: p];
	if (!memcmp([a last], p, 4096))
		puts("[a last] matches with p!");







|







41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
	void *p, *q;
	size_t i;

	puts("Trying to add too much to an array...");
	a = [OFArray newWithItemSize: 4096];
	CATCH_EXCEPTION([a addNItems: SIZE_MAX
			  fromCArray: NULL],
	    OFOutOfRangeException)

	puts("Trying to add something after that error...");
	p = [a getMemWithSize: 4096];
	memset(p, 255, 4096);
	[a add: p];
	if (!memcmp([a last], p, 4096))
		puts("[a last] matches with p!");
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
	[a removeNItems: 2];
	if ([a items] + 2 != i) {
		puts("[a items] + 2 != i!");
		abort();
	}

	puts("Trying to remove more data than we added...");
	CATCH_EXCEPTION([a removeNItems: [a items] + 1], OFOverflowException);

	puts("Trying to access an index that does not exist...");
	CATCH_EXCEPTION([a item: [a items]], OFOverflowException);

	[a free];

	puts("Creating new array and using it to build a string...");
	a = [OFArray newWithItemSize: 1];

	for (i = 0; i < strlen(str); i++)







|


|







90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
	[a removeNItems: 2];
	if ([a items] + 2 != i) {
		puts("[a items] + 2 != i!");
		abort();
	}

	puts("Trying to remove more data than we added...");
	CATCH_EXCEPTION([a removeNItems: [a items] + 1], OFOutOfRangeException);

	puts("Trying to access an index that does not exist...");
	CATCH_EXCEPTION([a item: [a items]], OFOutOfRangeException);

	[a free];

	puts("Creating new array and using it to build a string...");
	a = [OFArray newWithItemSize: 1];

	for (i = 0; i < strlen(str); i++)