[PATCH 06/41] util, buffers: Preparatory improvements

Ian Jackson ijackson at chiark.greenend.org.uk
Thu Jul 25 18:40:32 BST 2013


We invent a new kind of buffer_if which is a readonly view of another
block of memory; such a view can be initialised with
buffer_view_readonly or buffer_view_clone.  This makes a convenient
function to allow reparsing a packet, or using the buffer machinery to
parse a particular existing block of memory.

Also, make buffer_assert_free and buffer_assert_used actually call
assert (as well as logging the buffer ownership).  So when these fail
(a) we don't attempt the clean teardown, and (b) we get a core dump if
those are enabled.

Signed-off-by: Ian Jackson <ijackson at chiark.greenend.org.uk>
---
 util.c |   26 ++++++++++++++++++++++----
 util.h |    6 ++++++
 2 files changed, 28 insertions(+), 4 deletions(-)

diff --git a/util.c b/util.c
index f5f3d75..de91e1e 100644
--- a/util.c
+++ b/util.c
@@ -228,8 +228,9 @@ void buffer_assert_free(struct buffer_if *buffer, cstring_t file,
 			int line)
 {
     if (!buffer->free) {
-	fatal("BUF_ASSERT_FREE, %s line %d, owned by %s",
-	      file,line,buffer->owner);
+	fprintf(stderr,"secnet: BUF_ASSERT_FREE, %s line %d, owned by %s",
+		file,line,buffer->owner);
+	assert(!"buffer_assert_free failure");
     }
 }
 
@@ -237,8 +238,9 @@ void buffer_assert_used(struct buffer_if *buffer, cstring_t file,
 			int line)
 {
     if (buffer->free) {
-	fatal("BUF_ASSERT_USED, %s line %d, last owned by %s",
-	      file,line,buffer->owner);
+	fprintf(stderr,"secnet: BUF_ASSERT_USED, %s line %d, last owned by %s",
+		file,line,buffer->owner);
+	assert(!"buffer_assert_used failure");
     }
 }
 
@@ -301,6 +303,22 @@ void buffer_new(struct buffer_if *buf, int32_t len)
     buf->base=safe_malloc(len,"buffer_new");
 }
 
+void buffer_readonly_view(struct buffer_if *buf, const void *data, int32_t len)
+{
+    buf->free=False;
+    buf->owner="READONLY";
+    buf->flags=0;
+    buf->loc.file=NULL;
+    buf->loc.line=0;
+    buf->size=buf->len=len;
+    buf->base=buf->start=(uint8_t*)data;
+}
+
+void buffer_readonly_clone(struct buffer_if *out, const struct buffer_if *in)
+{
+    buffer_readonly_view(out,in->start,in->size);
+}
+
 void buffer_copy(struct buffer_if *dst, const struct buffer_if *src)
 {
     if (dst->len < src->len) {
diff --git a/util.h b/util.h
index af19363..7991743 100644
--- a/util.h
+++ b/util.h
@@ -29,6 +29,12 @@ extern void *buf_prepend(struct buffer_if *buf, int32_t amount);
 extern void *buf_unappend(struct buffer_if *buf, int32_t amount);
 extern void *buf_unprepend(struct buffer_if *buf, int32_t amount);
 
+extern void buffer_readonly_view(struct buffer_if *n, const void*, int32_t len);
+extern void buffer_readonly_clone(struct buffer_if *n, const struct buffer_if*);
+  /* Caller must only use unappend, unprepend et al. on n.
+   * New buffer state (in n) before this can be undefined.  After use,
+   * it must NOT be freed. */
+
 extern void buf_append_string(struct buffer_if *buf, cstring_t s);
 
 extern void read_mpbin(MP_INT *a, uint8_t *bin, int binsize);
-- 
1.7.2.5




More information about the sgo-software-discuss mailing list