diff options
Diffstat (limited to 'buffer.c')
-rw-r--r-- | buffer.c | 25 |
1 files changed, 16 insertions, 9 deletions
@@ -12,7 +12,7 @@ | |||
12 | */ | 12 | */ |
13 | 13 | ||
14 | #include "includes.h" | 14 | #include "includes.h" |
15 | RCSID("$OpenBSD: buffer.c,v 1.16 2002/06/26 08:54:18 markus Exp $"); | 15 | RCSID("$OpenBSD: buffer.c,v 1.18 2003/09/16 21:02:39 markus Exp $"); |
16 | 16 | ||
17 | #include "xmalloc.h" | 17 | #include "xmalloc.h" |
18 | #include "buffer.h" | 18 | #include "buffer.h" |
@@ -23,8 +23,11 @@ RCSID("$OpenBSD: buffer.c,v 1.16 2002/06/26 08:54:18 markus Exp $"); | |||
23 | void | 23 | void |
24 | buffer_init(Buffer *buffer) | 24 | buffer_init(Buffer *buffer) |
25 | { | 25 | { |
26 | buffer->alloc = 4096; | 26 | const u_int len = 4096; |
27 | buffer->buf = xmalloc(buffer->alloc); | 27 | |
28 | buffer->alloc = 0; | ||
29 | buffer->buf = xmalloc(len); | ||
30 | buffer->alloc = len; | ||
28 | buffer->offset = 0; | 31 | buffer->offset = 0; |
29 | buffer->end = 0; | 32 | buffer->end = 0; |
30 | } | 33 | } |
@@ -34,8 +37,10 @@ buffer_init(Buffer *buffer) | |||
34 | void | 37 | void |
35 | buffer_free(Buffer *buffer) | 38 | buffer_free(Buffer *buffer) |
36 | { | 39 | { |
37 | memset(buffer->buf, 0, buffer->alloc); | 40 | if (buffer->alloc > 0) { |
38 | xfree(buffer->buf); | 41 | memset(buffer->buf, 0, buffer->alloc); |
42 | xfree(buffer->buf); | ||
43 | } | ||
39 | } | 44 | } |
40 | 45 | ||
41 | /* | 46 | /* |
@@ -69,6 +74,7 @@ buffer_append(Buffer *buffer, const void *data, u_int len) | |||
69 | void * | 74 | void * |
70 | buffer_append_space(Buffer *buffer, u_int len) | 75 | buffer_append_space(Buffer *buffer, u_int len) |
71 | { | 76 | { |
77 | u_int newlen; | ||
72 | void *p; | 78 | void *p; |
73 | 79 | ||
74 | if (len > 0x100000) | 80 | if (len > 0x100000) |
@@ -98,11 +104,12 @@ restart: | |||
98 | goto restart; | 104 | goto restart; |
99 | } | 105 | } |
100 | /* Increase the size of the buffer and retry. */ | 106 | /* Increase the size of the buffer and retry. */ |
101 | buffer->alloc += len + 32768; | 107 | newlen = buffer->alloc + len + 32768; |
102 | if (buffer->alloc > 0xa00000) | 108 | if (newlen > 0xa00000) |
103 | fatal("buffer_append_space: alloc %u not supported", | 109 | fatal("buffer_append_space: alloc %u not supported", |
104 | buffer->alloc); | 110 | newlen); |
105 | buffer->buf = xrealloc(buffer->buf, buffer->alloc); | 111 | buffer->buf = xrealloc(buffer->buf, newlen); |
112 | buffer->alloc = newlen; | ||
106 | goto restart; | 113 | goto restart; |
107 | /* NOTREACHED */ | 114 | /* NOTREACHED */ |
108 | } | 115 | } |