Mercurial > code
comparison buffer.c @ 52:14c0b77f679b
Add buffer_shrink in buffer_end() and security if realloc fails
author | David Demelier <markand@malikania.fr> |
---|---|
date | Wed, 02 Nov 2011 16:19:20 +0100 |
parents | fea13579acbe |
children | bbe0713e8e2a |
comparison
equal
deleted
inserted
replaced
51:82bbd3b869e3 | 52:14c0b77f679b |
---|---|
174 */ | 174 */ |
175 | 175 |
176 int | 176 int |
177 buffer_shrink(struct buffer *buf) | 177 buffer_shrink(struct buffer *buf) |
178 { | 178 { |
179 if ((buf->data = realloc(buf->data, buf->length + 1)) == NULL) | 179 if ((buf->data = realloc(buf->data, buf->length + 1)) == NULL) { |
180 return -1; | 180 buf->size = 0; |
181 return -1; | |
182 } | |
181 | 183 |
182 buf->size = buf->length + 1; | 184 buf->size = buf->length + 1; |
183 | 185 |
184 return 0; | 186 return 0; |
185 } | 187 } |
191 char * | 193 char * |
192 buffer_end(struct buffer *buf) | 194 buffer_end(struct buffer *buf) |
193 { | 195 { |
194 char *data; | 196 char *data; |
195 | 197 |
198 buffer_shrink(buf); | |
196 data = buf->data; | 199 data = buf->data; |
197 free(buf); | 200 free(buf); |
198 | 201 |
199 return data; | 202 return data; |
200 } | 203 } |
238 if (buf->type == BUFFER_AUTO) { | 241 if (buf->type == BUFFER_AUTO) { |
239 newlen = buf->size; | 242 newlen = buf->size; |
240 while (newlen - buf->length - 1 <= needed) | 243 while (newlen - buf->length - 1 <= needed) |
241 newlen += buf->bsize; | 244 newlen += buf->bsize; |
242 | 245 |
243 if ((buf->data = realloc(buf->data, newlen)) == NULL) | 246 if ((buf->data = realloc(buf->data, newlen)) == NULL) { |
247 buf->size = 0; | |
244 return -1; | 248 return -1; |
249 } | |
245 | 250 |
246 buf->size = newlen; | 251 buf->size = newlen; |
247 memset(buf->data + buf->length, 0, buf->size - buf->length); | 252 memset(buf->data + buf->length, 0, buf->size - buf->length); |
248 } | 253 } |
249 | 254 |