]> mj.ucw.cz Git - libucw.git/commitdiff
MJ's objections:
authorRobert Spalek <robert@ucw.cz>
Fri, 25 Jun 2004 15:35:57 +0000 (15:35 +0000)
committerRobert Spalek <robert@ucw.cz>
Fri, 25 Jun 2004 15:35:57 +0000 (15:35 +0000)
- test can_overwrite outside the loop in decode_attributes()
- mp_alloc() -> mp_alloc_fast_noalign()
- MAX() is a macro, be careful
- do not call bflush(body), but comment it in the header file

lib/buck2obj.c
lib/buck2obj.h

index 4270b7a36a7dd8f3028b82cb4115e40001175e7d..b2eb455ee165537e70bb16fbc28172f726c4f719 100644 (file)
@@ -51,33 +51,51 @@ buck2obj_free(struct buck2obj_buf *buf)
 static inline byte *
 decode_attributes(byte *ptr, byte *end, struct odes *o, uns can_overwrite)
 {
-  while (ptr < end)
-  {
-    uns len;
-    GET_UTF8(ptr, len);
-    if (!len--)
-      break;
-    byte type = ptr[len];
-    if (can_overwrite == 2)
+  if (can_overwrite >= 2)
+    while (ptr < end)
     {
+      uns len;
+      GET_UTF8(ptr, len);
+      if (!len--)
+       break;
+      byte type = ptr[len];
+
       ptr[len] = 0;
       obj_add_attr_ref(o, type, ptr);
+
+      ptr += len + 1;
     }
-    else if (can_overwrite == 1)
+  else if (can_overwrite == 1)
+    while (ptr < end)
     {
+      uns len;
+      GET_UTF8(ptr, len);
+      if (!len--)
+       break;
+      byte type = ptr[len];
+
       ptr[len] = 0;
       obj_add_attr(o, type, ptr);
       ptr[len] = type;
+
+      ptr += len + 1;
     }
-    else
+  else
+    while (ptr < end)
     {
-      byte *dup = mp_alloc(o->pool, len+1);
+      uns len;
+      GET_UTF8(ptr, len);
+      if (!len--)
+       break;
+      byte type = ptr[len];
+
+      byte *dup = mp_alloc_fast_noalign(o->pool, len+1);
       memcpy(dup, ptr, len);
       dup[len] = 0;
-      obj_add_attr_ref(o, type, ptr);
+      obj_add_attr_ref(o, type, dup);
+
+      ptr += len + 1;
     }
-    ptr += len + 1;
-  }
   return ptr;
 }
 
@@ -98,7 +116,9 @@ buck2obj_convert(struct buck2obj_buf *buf, uns buck_type, struct fastbuf *body)
     bsetpos(body, 0);
 
     /* Read all the bucket into 1 buffer, 0-copy if possible.  */
-    int can_overwrite = MAX(bconfig(body, BCONFIG_CAN_OVERWRITE, 0), 0);
+    int can_overwrite = bconfig(body, BCONFIG_CAN_OVERWRITE, 0);
+    if (can_overwrite < 0)
+      can_overwrite = 0;
     uns overwritten;
     byte *ptr, *end;
     uns len = bdirect_read_prepare(body, &ptr);
@@ -143,8 +163,7 @@ buck2obj_convert(struct buck2obj_buf *buf, uns buck_type, struct fastbuf *body)
 
     if (ptr != end)
       RET_ERR(EINVAL);
-    if (overwritten)
-      bflush(body);
+    /* If (overwritten), bflush(body) might be needed.  */
   }
   return o;
 }
index d98364e4f2674d7adbfc545dfe3b4617cdbbe273..e59ead024b0d76f128597c4b7b8c7fc7c1501473 100644 (file)
@@ -9,3 +9,7 @@ struct buck2obj_buf;
 struct buck2obj_buf *buck2obj_alloc(uns max_len, struct mempool *mp);
 void buck2obj_free(struct buck2obj_buf *buf);
 struct odes *buck2obj_convert(struct buck2obj_buf *buf, uns buck_type, struct fastbuf *body);
+  /* If BCONFIG_CAN_OVERWRITE(body)==2, then the buffer of body has probably
+   * been tampered (unless the bucket is larger than the buffer).  In such a
+   * case, you must call bflush(body) before you do anything else than
+   * sequential read.  */