summaryrefslogtreecommitdiff
path: root/buffer.c
diff options
context:
space:
mode:
Diffstat (limited to 'buffer.c')
-rw-r--r--buffer.c25
1 files changed, 16 insertions, 9 deletions
diff --git a/buffer.c b/buffer.c
index ad04b267e..3099234bd 100644
--- a/buffer.c
+++ b/buffer.c
@@ -12,7 +12,7 @@
12 */ 12 */
13 13
14#include "includes.h" 14#include "includes.h"
15RCSID("$OpenBSD: buffer.c,v 1.16 2002/06/26 08:54:18 markus Exp $"); 15RCSID("$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 $");
23void 23void
24buffer_init(Buffer *buffer) 24buffer_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)
34void 37void
35buffer_free(Buffer *buffer) 38buffer_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)
69void * 74void *
70buffer_append_space(Buffer *buffer, u_int len) 75buffer_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}