diff options
author | Christian Grothoff <christian@grothoff.org> | 2010-07-02 21:05:43 +0000 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2010-07-02 21:05:43 +0000 |
commit | ba9c0e16195d8468f0a9c080e658f3d3f2131c6a (patch) | |
tree | d5ca467343b4f85b04474de784c63b479262831e /src/util/server_mst.c | |
parent | 3951bd96d608ab0f88ccb5a7b4b7e94e15d91a89 (diff) | |
download | gnunet-ba9c0e16195d8468f0a9c080e658f3d3f2131c6a.tar.gz gnunet-ba9c0e16195d8468f0a9c080e658f3d3f2131c6a.zip |
fixing calculations and code for buffer realloc
Diffstat (limited to 'src/util/server_mst.c')
-rw-r--r-- | src/util/server_mst.c | 34 |
1 files changed, 13 insertions, 21 deletions
diff --git a/src/util/server_mst.c b/src/util/server_mst.c index 5a152be28..0856ba576 100644 --- a/src/util/server_mst.c +++ b/src/util/server_mst.c | |||
@@ -129,14 +129,9 @@ GNUNET_SERVER_mst_receive (struct GNUNET_SERVER_MessageStreamTokenizer *mst, | |||
129 | size_t delta; | 129 | size_t delta; |
130 | uint16_t want; | 130 | uint16_t want; |
131 | char *ibuf; | 131 | char *ibuf; |
132 | |||
133 | #if !REALLOC | ||
134 | char *temp_buf; | ||
135 | #endif | ||
136 | int need_align; | 132 | int need_align; |
137 | unsigned long offset; | 133 | unsigned long offset; |
138 | int ret; | 134 | int ret; |
139 | size_t newsize; | ||
140 | 135 | ||
141 | #if DEBUG_SERVER_MST | 136 | #if DEBUG_SERVER_MST |
142 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 137 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
@@ -144,22 +139,6 @@ GNUNET_SERVER_mst_receive (struct GNUNET_SERVER_MessageStreamTokenizer *mst, | |||
144 | (unsigned int) size, | 139 | (unsigned int) size, |
145 | (unsigned int) (mst->pos - mst->off)); | 140 | (unsigned int) (mst->pos - mst->off)); |
146 | #endif | 141 | #endif |
147 | if ((size > mst->curr_buf) && (size < GNUNET_SERVER_MAX_MESSAGE_SIZE)) /* Received bigger message than we can currently handle! */ | ||
148 | { | ||
149 | newsize = mst->curr_buf + size + 1; /* How much space do we need? */ | ||
150 | if (newsize >= GNUNET_SERVER_MAX_MESSAGE_SIZE) | ||
151 | newsize = GNUNET_SERVER_MAX_MESSAGE_SIZE - 1; /* Check it's not bigger than GNUNET_SERVER_MAX_MESSAGE_SIZE */ | ||
152 | #if REALLOC | ||
153 | mst->hdr = GNUNET_realloc(mst->hdr, newsize); | ||
154 | #else | ||
155 | temp_buf = GNUNET_malloc(newsize); | ||
156 | memcpy(temp_buf, mst->hdr, mst->curr_buf); | ||
157 | GNUNET_free(mst->hdr); | ||
158 | mst->hdr = temp_buf; | ||
159 | #endif | ||
160 | mst->curr_buf = newsize; | ||
161 | } | ||
162 | |||
163 | ret = GNUNET_OK; | 142 | ret = GNUNET_OK; |
164 | ibuf = (char*)mst->hdr; | 143 | ibuf = (char*)mst->hdr; |
165 | while (mst->pos > 0) | 144 | while (mst->pos > 0) |
@@ -211,6 +190,13 @@ GNUNET_SERVER_mst_receive (struct GNUNET_SERVER_MessageStreamTokenizer *mst, | |||
211 | mst->pos); | 190 | mst->pos); |
212 | mst->off = 0; | 191 | mst->off = 0; |
213 | } | 192 | } |
193 | if (want > mst->curr_buf) | ||
194 | { | ||
195 | mst->hdr = GNUNET_realloc(mst->hdr, want); | ||
196 | ibuf = (char*)mst->hdr; | ||
197 | mst->curr_buf = want; | ||
198 | } | ||
199 | hdr = (const struct GNUNET_MessageHeader*) &ibuf[mst->off]; | ||
214 | if (mst->pos - mst->off < want) | 200 | if (mst->pos - mst->off < want) |
215 | { | 201 | { |
216 | delta = GNUNET_MIN (want - (mst->pos - mst->off), | 202 | delta = GNUNET_MIN (want - (mst->pos - mst->off), |
@@ -290,6 +276,12 @@ GNUNET_SERVER_mst_receive (struct GNUNET_SERVER_MessageStreamTokenizer *mst, | |||
290 | copy: | 276 | copy: |
291 | if ( (size > 0) && (! purge) ) | 277 | if ( (size > 0) && (! purge) ) |
292 | { | 278 | { |
279 | if (size + mst->pos > mst->curr_buf) | ||
280 | { | ||
281 | mst->hdr = GNUNET_realloc(mst->hdr, size + mst->pos); | ||
282 | ibuf = (char*)mst->hdr; | ||
283 | mst->curr_buf = size + mst->pos; | ||
284 | } | ||
293 | GNUNET_assert (mst->pos + size <= mst->curr_buf); | 285 | GNUNET_assert (mst->pos + size <= mst->curr_buf); |
294 | memcpy (&ibuf[mst->pos], buf, size); | 286 | memcpy (&ibuf[mst->pos], buf, size); |
295 | mst->pos += size; | 287 | mst->pos += size; |