From 7521ef9a00e71b62bcdce199639e923dd284976b Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Wed, 20 Feb 2008 17:48:39 +0000 Subject: [PATCH] r18264@catbus: nickm | 2008-02-20 12:48:21 -0500 fix bufs in buf_pos_t implementation. svn:r13623 --- ChangeLog | 2 ++ src/or/buffers.c | 26 ++++++++++++-------------- src/or/test.c | 2 ++ 3 files changed, 16 insertions(+), 14 deletions(-) diff --git a/ChangeLog b/ChangeLog index b592f1a93c..ae981ade36 100644 --- a/ChangeLog +++ b/ChangeLog @@ -17,6 +17,8 @@ Changes in version 0.2.0.20-?? - 2008-02-?? number we've gotten from accept(). This bug made us fail to count all sockets that we were using for incoming connections. Bugfix on 0.2.0.x + - Fix code used to find strings within buffers, when those strings + are not in the first chunk of the buffer. o Minor features (performance): - Tune parameters for cell pool allocation to minimize amount of diff --git a/src/or/buffers.c b/src/or/buffers.c index 13fda0fb55..f5c1b3ba3c 100644 --- a/src/or/buffers.c +++ b/src/or/buffers.c @@ -998,9 +998,8 @@ move_buf_to_buf(buf_t *buf_out, buf_t *buf_in, size_t *buf_flushlen) /** Internal structure: represents a position in a buffer. */ typedef struct buf_pos_t { const chunk_t *chunk; /**< Which chunk are we pointing to? */ - int pos; /**< Which character inside the chunk's data are we pointing to? */ - int pos_absolute; /**< Which character inside the buffer are we pointing to? - */ + off_t pos;/**< Which character inside the chunk's data are we pointing to? */ + size_t chunk_pos; /**< Total length of all previous chunks. */ } buf_pos_t; /** Initialize out to point to the first character of buf.*/ @@ -1009,7 +1008,7 @@ buf_pos_init(const buf_t *buf, buf_pos_t *out) { out->chunk = buf->head; out->pos = 0; - out->pos_absolute = 0; + out->chunk_pos = 0; } /** Advance out to the first appearance of ch at the current @@ -1019,19 +1018,19 @@ static int buf_find_pos_of_char(char ch, buf_pos_t *out) { const chunk_t *chunk; - int offset = 0; /*XXXX020 should this be pos_absolute? Otherwise, bug. */ int pos; - tor_assert(out && out->chunk && out->pos < (int)out->chunk->datalen); + tor_assert(out); + if (out->chunk) + tor_assert(out->pos < out->chunk->datalen); pos = out->pos; for (chunk = out->chunk; chunk; chunk = chunk->next) { char *cp = memchr(chunk->data+pos, ch, chunk->datalen-pos); if (cp) { out->chunk = chunk; out->pos = cp - chunk->data; - out->pos_absolute = offset + (cp - chunk->data); - return out->pos_absolute; + return out->chunk_pos + out->pos; } else { - offset += chunk->datalen; + out->chunk_pos += chunk->datalen; pos = 0; } } @@ -1043,15 +1042,14 @@ buf_find_pos_of_char(char ch, buf_pos_t *out) static INLINE int buf_pos_inc(buf_pos_t *pos) { - if ((size_t)pos->pos == pos->chunk->datalen) { + ++pos->pos; + if (pos->pos == pos->chunk->datalen) { if (!pos->chunk->next) return -1; + pos->chunk_pos += pos->chunk->datalen; pos->chunk = pos->chunk->next; pos->pos = 0; - } else { - ++pos->pos; } - ++pos->pos_absolute; return 0; } @@ -1084,7 +1082,7 @@ buf_find_string_offset(const buf_t *buf, const char *s, size_t n) buf_pos_init(buf, &pos); while (buf_find_pos_of_char(*s, &pos) >= 0) { if (buf_matches_at_pos(&pos, s, n)) { - return pos.pos_absolute; + return pos.chunk_pos + pos.pos; } else { if (buf_pos_inc(&pos)<0) return -1; diff --git a/src/or/test.c b/src/or/test.c index ffaf29097f..3dff0a42ec 100644 --- a/src/or/test.c +++ b/src/or/test.c @@ -255,6 +255,8 @@ test_buffers(void) buf_free(buf); buf_free(buf2); + /*XXXX020 Test code to find chars and strings on buffers. */ + #if 0 { int s;