aboutsummaryrefslogtreecommitdiffstats
path: root/common
diff options
context:
space:
mode:
Diffstat (limited to 'common')
-rw-r--r--common/ChangeLog8
-rw-r--r--common/membuf.c20
-rw-r--r--common/membuf.h4
3 files changed, 28 insertions, 4 deletions
diff --git a/common/ChangeLog b/common/ChangeLog
index 7fc6af2e5..edde6b8e2 100644
--- a/common/ChangeLog
+++ b/common/ChangeLog
@@ -1,3 +1,11 @@
+2006-10-04 Werner Koch <[email protected]>
+
+ * membuf.c (init_membuf_secure): New.
+ (put_membuf): Make sure that ERRNO is set even if the underlying
+ malloc code does not work properly.
+ (get_membuf): Set ERRNO on error.
+ (get_membuf): Allow to pass LEN as NULL.
+
2006-10-02 Werner Koch <[email protected]>
* iobuf.c (iobuf_unread): Removed. This code is not required.
diff --git a/common/membuf.c b/common/membuf.c
index 2d35fefab..51014592b 100644
--- a/common/membuf.c
+++ b/common/membuf.c
@@ -42,7 +42,19 @@ init_membuf (membuf_t *mb, int initiallen)
mb->out_of_core = 0;
mb->buf = xtrymalloc (initiallen);
if (!mb->buf)
- mb->out_of_core = errno;
+ mb->out_of_core = errno;
+}
+
+/* Same as init_membuf but allocates the buffer in secure memory. */
+void
+init_membuf_secure (membuf_t *mb, int initiallen)
+{
+ mb->len = 0;
+ mb->size = initiallen;
+ mb->out_of_core = 0;
+ mb->buf = xtrymalloc (initiallen);
+ if (!mb->buf)
+ mb->out_of_core = errno;
}
@@ -60,7 +72,7 @@ put_membuf (membuf_t *mb, const void *buf, size_t len)
p = xtryrealloc (mb->buf, mb->size);
if (!p)
{
- mb->out_of_core = errno;
+ mb->out_of_core = errno ? errno : ENOMEM;
/* Wipe out what we already accumulated. This is required
in case we are storing sensitive data here. The membuf
API does not provide another way to cleanup after an
@@ -84,11 +96,13 @@ get_membuf (membuf_t *mb, size_t *len)
{
xfree (mb->buf);
mb->buf = NULL;
+ errno = mb->out_of_core;
return NULL;
}
p = mb->buf;
- *len = mb->len;
+ if (len)
+ *len = mb->len;
mb->buf = NULL;
mb->out_of_core = ENOMEM; /* hack to make sure it won't get reused. */
return p;
diff --git a/common/membuf.h b/common/membuf.h
index 9033be61e..f9c08a400 100644
--- a/common/membuf.h
+++ b/common/membuf.h
@@ -24,7 +24,8 @@
/* The definition of the structure is private, we only need it here,
so it can be allocated on the stack. */
-struct private_membuf_s {
+struct private_membuf_s
+{
size_t len;
size_t size;
char *buf;
@@ -35,6 +36,7 @@ typedef struct private_membuf_s membuf_t;
void init_membuf (membuf_t *mb, int initiallen);
+void init_membuf_secure (membuf_t *mb, int initiallen);
void put_membuf (membuf_t *mb, const void *buf, size_t len);
void *get_membuf (membuf_t *mb, size_t *len);