ObjFW  Check-in [09e6b3fc7b]

Overview
Comment:Returning newly allocated buffers on reading is a bad idea.
The programmer might forget that some of the data is arbitrary because
there was less data than he requested which might lead to serious
problems.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 09e6b3fc7bddde57e9c18cd44c105fbb7d838c7c760491d9f2b824bc288fb1f2
User & Date: js on 2009-04-07 15:44:54
Other Links: manifest | tags
Context
2009-04-08
13:13
Throw an OFNotConnected exception when there was a clean disconnect. check-in: 69e41c48ff user: js tags: trunk
2009-04-07
15:44
Returning newly allocated buffers on reading is a bad idea.
The programmer might forget that some of the data is arbitrary because
there was less data than he requested which might lead to serious
problems.
check-in: 09e6b3fc7b user: js tags: trunk
2009-04-04
19:56
Fix wrong documentation. check-in: a72c435267 user: js tags: trunk
Changes

Modified src/OFFile.h from [83b21b531b] to [8001780b38].

123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
 *	  The buffer MUST be at least size * nitems big!
 * \return The number of bytes read
 */
- (size_t)readNItems: (size_t)nitems
	      ofSize: (size_t)size
	  intoBuffer: (uint8_t*)buf;

/**
 * Reads from the file into a new buffer.
 *
 * \param size The size of the data that should be read
 * \param nitem The number of items to read
 * \return A new buffer with the data read.
 *	   It is part of the memory pool of the OFFile.
 */
- (uint8_t*)readNItems: (size_t)nitems
		ofSize: (size_t)size;

/**
 * Writes from a buffer into the file.
 *
 * \param buf The buffer from which the data is written to the file
 * \param size The size of the data that should be written
 * \param nitem The number of items to write
 * \return The number of bytes written
 */
- (size_t)writeNItems: (size_t)nitems
	       ofSize: (size_t)size
	   fromBuffer: (const uint8_t*)buf;
@end







<
<
<
<
<
<
<
<
<
<
<












123
124
125
126
127
128
129











130
131
132
133
134
135
136
137
138
139
140
141
 *	  The buffer MUST be at least size * nitems big!
 * \return The number of bytes read
 */
- (size_t)readNItems: (size_t)nitems
	      ofSize: (size_t)size
	  intoBuffer: (uint8_t*)buf;












/**
 * Writes from a buffer into the file.
 *
 * \param buf The buffer from which the data is written to the file
 * \param size The size of the data that should be written
 * \param nitem The number of items to write
 * \return The number of bytes written
 */
- (size_t)writeNItems: (size_t)nitems
	       ofSize: (size_t)size
	   fromBuffer: (const uint8_t*)buf;
@end

Modified src/OFFile.m from [68e15040b2] to [2951d584dc].

124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
	  intoBuffer: (uint8_t*)buf
{
	return [self readNItems: size
			 ofSize: 1
		     intoBuffer: buf];
}

- (uint8_t*)readNItems: (size_t)nitems
		ofSize: (size_t)size
{
	uint8_t	*ret;

	ret = [self getMemForNItems: nitems
			     ofSize: size];

	@try {
		[self readNItems: nitems
			  ofSize: size
		      intoBuffer: ret];
	} @catch (id exception) {
		[self freeMem: ret];
		@throw exception;
	}

	return ret;
}

- (uint8_t*)readNBytes: (size_t)size
{
	return [self readNItems: size
			 ofSize: 1];
}

- (size_t)writeNItems: (size_t)nitems
	       ofSize: (size_t)size
	   fromBuffer: (const uint8_t*)buf
{
	size_t ret;

	if ((ret = fwrite(buf, size, nitems, fp)) == 0 &&







<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<







124
125
126
127
128
129
130


























131
132
133
134
135
136
137
	  intoBuffer: (uint8_t*)buf
{
	return [self readNItems: size
			 ofSize: 1
		     intoBuffer: buf];
}



























- (size_t)writeNItems: (size_t)nitems
	       ofSize: (size_t)size
	   fromBuffer: (const uint8_t*)buf
{
	size_t ret;

	if ((ret = fwrite(buf, size, nitems, fp)) == 0 &&

Modified src/OFStream.h from [ae18f564f1] to [ed38ea4d16].

20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
 * \param size The size of the data that should be read.
 *	  The buffer MUST be at least size big!
 * \return The number of bytes read
 */
- (size_t)readNBytes: (size_t)size
	  intoBuffer: (uint8_t*)buf;

/**
 * Reads from the stream into a new buffer.
 *
 * \param size The size of the data that should be read
 * \return A new buffer with the data read.
 *	   It is part of the memory pool of the OFFile.
 */
- (uint8_t*)readNBytes: (size_t)size;

/**
 * Writes from a buffer into the stream.
 *
 * \param buf The buffer from which the data is written to the stream
 * \param size The size of the data that should be written
 * \return The number of bytes written
 */







<
<
<
<
<
<
<
<
<







20
21
22
23
24
25
26









27
28
29
30
31
32
33
 * \param size The size of the data that should be read.
 *	  The buffer MUST be at least size big!
 * \return The number of bytes read
 */
- (size_t)readNBytes: (size_t)size
	  intoBuffer: (uint8_t*)buf;










/**
 * Writes from a buffer into the stream.
 *
 * \param buf The buffer from which the data is written to the stream
 * \param size The size of the data that should be written
 * \return The number of bytes written
 */

Modified src/OFTCPSocket.m from [efdb457766] to [3dac29634e].

231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
		@throw [OFNotConnectedException newWithClass: [self class]];

	if ((ret = recv(sock, (char*)buf, size, 0)) < 1)
		@throw [OFReadFailedException newWithClass: [self class]
						   andSize: size];

	/* This is safe, as we already checked < 1 */
	return ret;
}

- (uint8_t*)readNBytes: (size_t)size
{
	uint8_t *ret;

	if (sock == INVALID_SOCKET)
		@throw [OFNotConnectedException newWithClass: [self class]];

	ret = [self getMemWithSize: size];

	@try {
		[self readNBytes: size
		      intoBuffer: ret];
	} @catch (id exception) {
		[self freeMem: ret];
		@throw exception;
	}

	return ret;
}

- (size_t)writeNBytes: (size_t)size
	   fromBuffer: (const uint8_t*)buf
{
	ssize_t ret;







<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<







231
232
233
234
235
236
237




















238
239
240
241
242
243
244
		@throw [OFNotConnectedException newWithClass: [self class]];

	if ((ret = recv(sock, (char*)buf, size, 0)) < 1)
		@throw [OFReadFailedException newWithClass: [self class]
						   andSize: size];

	/* This is safe, as we already checked < 1 */




















	return ret;
}

- (size_t)writeNBytes: (size_t)size
	   fromBuffer: (const uint8_t*)buf
{
	ssize_t ret;