Browse Source

r18264@catbus: nickm | 2008-02-20 12:48:21 -0500
fix bufs in buf_pos_t implementation.


svn:r13623

Nick Mathewson 16 years ago
parent
commit
7521ef9a00
3 changed files with 16 additions and 14 deletions
  1. 2 0
      ChangeLog
  2. 12 14
      src/or/buffers.c
  3. 2 0
      src/or/test.c

+ 2 - 0
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

+ 12 - 14
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 <b>out</b> to point to the first character of <b>buf</b>.*/
@@ -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 <b>out</b> to the first appearance of <b>ch</b> 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;

+ 2 - 0
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;