8d2dcd
diff --git a/modules/filters/mod_sed.c b/modules/filters/mod_sed.c
8d2dcd
index 8595e41..9b99a6b 100644
8d2dcd
--- a/modules/filters/mod_sed.c
8d2dcd
+++ b/modules/filters/mod_sed.c
8d2dcd
@@ -59,7 +59,7 @@ typedef struct sed_filter_ctxt
8d2dcd
 module AP_MODULE_DECLARE_DATA sed_module;
8d2dcd
 
8d2dcd
 /* This function will be call back from libsed functions if there is any error
8d2dcd
- * happend during execution of sed scripts
8d2dcd
+ * happened during execution of sed scripts
8d2dcd
  */
8d2dcd
 static apr_status_t log_sed_errf(void *data, const char *error)
8d2dcd
 {
8d2dcd
@@ -276,7 +276,7 @@ static apr_status_t sed_response_filter(ap_filter_t *f,
8d2dcd
                                         apr_bucket_brigade *bb)
8d2dcd
 {
8d2dcd
     apr_bucket *b;
8d2dcd
-    apr_status_t status;
8d2dcd
+    apr_status_t status = APR_SUCCESS;
8d2dcd
     sed_config *cfg = ap_get_module_config(f->r->per_dir_config,
8d2dcd
                                            &sed_module);
8d2dcd
     sed_filter_ctxt *ctx = f->ctx;
8d2dcd
@@ -301,9 +301,9 @@ static apr_status_t sed_response_filter(ap_filter_t *f,
8d2dcd
              return status;
8d2dcd
         ctx = f->ctx;
8d2dcd
         apr_table_unset(f->r->headers_out, "Content-Length");
8d2dcd
-    }
8d2dcd
 
8d2dcd
-    ctx->bb = apr_brigade_create(f->r->pool, f->c->bucket_alloc);
8d2dcd
+        ctx->bb = apr_brigade_create(f->r->pool, f->c->bucket_alloc);
8d2dcd
+    }
8d2dcd
 
8d2dcd
     /* Here is the main logic. Iterate through all the buckets, read the
8d2dcd
      * content of the bucket, call sed_eval_buffer on the data.
8d2dcd
@@ -325,63 +325,52 @@ static apr_status_t sed_response_filter(ap_filter_t *f,
8d2dcd
      * in sed's internal buffer which can't be flushed until new line
8d2dcd
      * character is arrived.
8d2dcd
      */
8d2dcd
-    for (b = APR_BRIGADE_FIRST(bb); b != APR_BRIGADE_SENTINEL(bb);) {
8d2dcd
-        const char *buf = NULL;
8d2dcd
-        apr_size_t bytes = 0;
8d2dcd
+    while (!APR_BRIGADE_EMPTY(bb)) {
8d2dcd
+        b = APR_BRIGADE_FIRST(bb);
8d2dcd
         if (APR_BUCKET_IS_EOS(b)) {
8d2dcd
-            apr_bucket *b1 = APR_BUCKET_NEXT(b);
8d2dcd
             /* Now clean up the internal sed buffer */
8d2dcd
             sed_finalize_eval(&ctx->eval, ctx);
8d2dcd
             status = flush_output_buffer(ctx);
8d2dcd
             if (status != APR_SUCCESS) {
8d2dcd
-                clear_ctxpool(ctx);
8d2dcd
-                return status;
8d2dcd
+                break;
8d2dcd
             }
8d2dcd
+            /* Move the eos bucket to ctx->bb brigade */
8d2dcd
             APR_BUCKET_REMOVE(b);
8d2dcd
-            /* Insert the eos bucket to ctx->bb brigade */
8d2dcd
             APR_BRIGADE_INSERT_TAIL(ctx->bb, b);
8d2dcd
-            b = b1;
8d2dcd
         }
8d2dcd
         else if (APR_BUCKET_IS_FLUSH(b)) {
8d2dcd
-            apr_bucket *b1 = APR_BUCKET_NEXT(b);
8d2dcd
-            APR_BUCKET_REMOVE(b);
8d2dcd
             status = flush_output_buffer(ctx);
8d2dcd
             if (status != APR_SUCCESS) {
8d2dcd
-                clear_ctxpool(ctx);
8d2dcd
-                return status;
8d2dcd
+                break;
8d2dcd
             }
8d2dcd
+            /* Move the flush bucket to ctx->bb brigade */
8d2dcd
+            APR_BUCKET_REMOVE(b);
8d2dcd
             APR_BRIGADE_INSERT_TAIL(ctx->bb, b);
8d2dcd
-            b = b1;
8d2dcd
-        }
8d2dcd
-        else if (APR_BUCKET_IS_METADATA(b)) {
8d2dcd
-            b = APR_BUCKET_NEXT(b);
8d2dcd
         }
8d2dcd
-        else if (apr_bucket_read(b, &buf, &bytes, APR_BLOCK_READ)
8d2dcd
-                 == APR_SUCCESS) {
8d2dcd
-            apr_bucket *b1 = APR_BUCKET_NEXT(b);
8d2dcd
-            status = sed_eval_buffer(&ctx->eval, buf, bytes, ctx);
8d2dcd
-            if (status != APR_SUCCESS) {
8d2dcd
-                clear_ctxpool(ctx);
8d2dcd
-                return status;
8d2dcd
+        else {
8d2dcd
+            if (!APR_BUCKET_IS_METADATA(b)) {
8d2dcd
+                const char *buf = NULL;
8d2dcd
+                apr_size_t bytes = 0;
8d2dcd
+
8d2dcd
+                status = apr_bucket_read(b, &buf, &bytes, APR_BLOCK_READ);
8d2dcd
+                if (status == APR_SUCCESS) {
8d2dcd
+                    status = sed_eval_buffer(&ctx->eval, buf, bytes, ctx);
8d2dcd
+                }
8d2dcd
+                if (status != APR_SUCCESS) {
8d2dcd
+                    ap_log_rerror(APLOG_MARK, APLOG_ERR, status, f->r, APLOGNO(10394) "error evaluating sed on output");
8d2dcd
+                    break;
8d2dcd
+                }
8d2dcd
             }
8d2dcd
-            APR_BUCKET_REMOVE(b);
8d2dcd
             apr_bucket_delete(b);
8d2dcd
-            b = b1;
8d2dcd
-        }
8d2dcd
-        else {
8d2dcd
-            apr_bucket *b1 = APR_BUCKET_NEXT(b);
8d2dcd
-            APR_BUCKET_REMOVE(b);
8d2dcd
-            b = b1;
8d2dcd
         }
8d2dcd
     }
8d2dcd
-    apr_brigade_cleanup(bb);
8d2dcd
-    status = flush_output_buffer(ctx);
8d2dcd
-    if (status != APR_SUCCESS) {
8d2dcd
-        clear_ctxpool(ctx);
8d2dcd
-        return status;
8d2dcd
+    if (status == APR_SUCCESS) {
8d2dcd
+        status = flush_output_buffer(ctx);
8d2dcd
     }
8d2dcd
     if (!APR_BRIGADE_EMPTY(ctx->bb)) {
8d2dcd
-        status = ap_pass_brigade(f->next, ctx->bb);
8d2dcd
+        if (status == APR_SUCCESS) {
8d2dcd
+            status = ap_pass_brigade(f->next, ctx->bb);
8d2dcd
+        }
8d2dcd
         apr_brigade_cleanup(ctx->bb);
8d2dcd
     }
8d2dcd
     clear_ctxpool(ctx);
8d2dcd
@@ -432,7 +421,7 @@ static apr_status_t sed_request_filter(ap_filter_t *f,
8d2dcd
      * the buckets in bbinp and read the data from buckets and invoke
8d2dcd
      * sed_eval_buffer on the data. libsed will generate its output using
8d2dcd
      * sed_write_output which will add data in ctx->bb. Do it until it have
8d2dcd
-     * atleast one bucket in ctx->bb. At the end of data eos bucket
8d2dcd
+     * at least one bucket in ctx->bb. At the end of data eos bucket
8d2dcd
      * should be there.
8d2dcd
      *
8d2dcd
      * Once eos bucket is seen, then invoke sed_finalize_eval to clear the
8d2dcd
@@ -474,8 +463,10 @@ static apr_status_t sed_request_filter(ap_filter_t *f,
8d2dcd
             if (apr_bucket_read(b, &buf, &bytes, APR_BLOCK_READ)
8d2dcd
                      == APR_SUCCESS) {
8d2dcd
                 status = sed_eval_buffer(&ctx->eval, buf, bytes, ctx);
8d2dcd
-                if (status != APR_SUCCESS)
8d2dcd
+                if (status != APR_SUCCESS) { 
8d2dcd
+                    ap_log_rerror(APLOG_MARK, APLOG_ERR, status, f->r, APLOGNO(10395) "error evaluating sed on input");
8d2dcd
                     return status;
8d2dcd
+                }
8d2dcd
                 flush_output_buffer(ctx);
8d2dcd
             }
8d2dcd
         }
8d2dcd
diff --git a/modules/filters/sed1.c b/modules/filters/sed1.c
8d2dcd
index 67a8d06..047f49b 100644
8d2dcd
--- a/modules/filters/sed1.c
8d2dcd
+++ b/modules/filters/sed1.c
8d2dcd
@@ -87,18 +87,20 @@ static void eval_errf(sed_eval_t *eval, const char *fmt, ...)
8d2dcd
 }
8d2dcd
 
8d2dcd
 #define INIT_BUF_SIZE 1024
8d2dcd
+#define MAX_BUF_SIZE 1024*8192
8d2dcd
 
8d2dcd
 /*
8d2dcd
  * grow_buffer
8d2dcd
  */
8d2dcd
-static void grow_buffer(apr_pool_t *pool, char **buffer,
8d2dcd
+static apr_status_t grow_buffer(apr_pool_t *pool, char **buffer,
8d2dcd
                         char **spend, apr_size_t *cursize,
8d2dcd
                         apr_size_t newsize)
8d2dcd
 {
8d2dcd
     char* newbuffer = NULL;
8d2dcd
     apr_size_t spendsize = 0;
8d2dcd
-    if (*cursize >= newsize)
8d2dcd
-        return;
8d2dcd
+    if (*cursize >= newsize) {
8d2dcd
+        return APR_SUCCESS;
8d2dcd
+    }
8d2dcd
     /* Avoid number of times realloc is called. It could cause huge memory
8d2dcd
      * requirement if line size is huge e.g 2 MB */
8d2dcd
     if (newsize < *cursize * 2) {
8d2dcd
@@ -107,6 +109,9 @@ static void grow_buffer(apr_pool_t *pool, char **buffer,
8d2dcd
 
8d2dcd
     /* Align it to 4 KB boundary */
8d2dcd
     newsize = (newsize  + ((1 << 12) - 1)) & ~((1 << 12) - 1);
8d2dcd
+    if (newsize > MAX_BUF_SIZE) { 
8d2dcd
+        return APR_ENOMEM;
8d2dcd
+    }
8d2dcd
     newbuffer = apr_pcalloc(pool, newsize);
8d2dcd
     if (*spend && *buffer && (*cursize > 0)) {
8d2dcd
         spendsize = *spend - *buffer;
8d2dcd
@@ -119,63 +124,77 @@ static void grow_buffer(apr_pool_t *pool, char **buffer,
8d2dcd
     if (spend != buffer) {
8d2dcd
         *spend = *buffer + spendsize;
8d2dcd
     }
8d2dcd
+    return APR_SUCCESS;
8d2dcd
 }
8d2dcd
 
8d2dcd
 /*
8d2dcd
  * grow_line_buffer
8d2dcd
  */
8d2dcd
-static void grow_line_buffer(sed_eval_t *eval, apr_size_t newsize)
8d2dcd
+static apr_status_t grow_line_buffer(sed_eval_t *eval, apr_size_t newsize)
8d2dcd
 {
8d2dcd
-    grow_buffer(eval->pool, &eval->linebuf, &eval->lspend,
8d2dcd
+    return grow_buffer(eval->pool, &eval->linebuf, &eval->lspend,
8d2dcd
                 &eval->lsize, newsize);
8d2dcd
 }
8d2dcd
 
8d2dcd
 /*
8d2dcd
  * grow_hold_buffer
8d2dcd
  */
8d2dcd
-static void grow_hold_buffer(sed_eval_t *eval, apr_size_t newsize)
8d2dcd
+static apr_status_t grow_hold_buffer(sed_eval_t *eval, apr_size_t newsize)
8d2dcd
 {
8d2dcd
-    grow_buffer(eval->pool, &eval->holdbuf, &eval->hspend,
8d2dcd
+    return grow_buffer(eval->pool, &eval->holdbuf, &eval->hspend,
8d2dcd
                 &eval->hsize, newsize);
8d2dcd
 }
8d2dcd
 
8d2dcd
 /*
8d2dcd
  * grow_gen_buffer
8d2dcd
  */
8d2dcd
-static void grow_gen_buffer(sed_eval_t *eval, apr_size_t newsize,
8d2dcd
+static apr_status_t grow_gen_buffer(sed_eval_t *eval, apr_size_t newsize,
8d2dcd
                             char **gspend)
8d2dcd
 {
8d2dcd
+    apr_status_t rc = 0;
8d2dcd
     if (gspend == NULL) {
8d2dcd
         gspend = &eval->genbuf;
8d2dcd
     }
8d2dcd
-    grow_buffer(eval->pool, &eval->genbuf, gspend,
8d2dcd
-                &eval->gsize, newsize);
8d2dcd
-    eval->lcomend = &eval->genbuf[71];
8d2dcd
+    rc = grow_buffer(eval->pool, &eval->genbuf, gspend,
8d2dcd
+                     &eval->gsize, newsize);
8d2dcd
+    if (rc == APR_SUCCESS) { 
8d2dcd
+        eval->lcomend = &eval->genbuf[71];
8d2dcd
+    }
8d2dcd
+    return rc;
8d2dcd
 }
8d2dcd
 
8d2dcd
 /*
8d2dcd
  * appendmem_to_linebuf
8d2dcd
  */
8d2dcd
-static void appendmem_to_linebuf(sed_eval_t *eval, const char* sz, apr_size_t len)
8d2dcd
+static apr_status_t appendmem_to_linebuf(sed_eval_t *eval, const char* sz, apr_size_t len)
8d2dcd
 {
8d2dcd
+    apr_status_t rc = 0;
8d2dcd
     apr_size_t reqsize = (eval->lspend - eval->linebuf) + len;
8d2dcd
     if (eval->lsize < reqsize) {
8d2dcd
-        grow_line_buffer(eval, reqsize);
8d2dcd
+        rc = grow_line_buffer(eval, reqsize);
8d2dcd
+        if (rc != APR_SUCCESS) { 
8d2dcd
+            return rc;
8d2dcd
+        }
8d2dcd
     }
8d2dcd
     memcpy(eval->lspend, sz, len);
8d2dcd
     eval->lspend += len;
8d2dcd
+    return APR_SUCCESS;
8d2dcd
 }
8d2dcd
 
8d2dcd
 /*
8d2dcd
  * append_to_linebuf
8d2dcd
  */
8d2dcd
-static void append_to_linebuf(sed_eval_t *eval, const char* sz,
8d2dcd
+static apr_status_t append_to_linebuf(sed_eval_t *eval, const char* sz,
8d2dcd
                               step_vars_storage *step_vars)
8d2dcd
 {
8d2dcd
     apr_size_t len = strlen(sz);
8d2dcd
     char *old_linebuf = eval->linebuf;
8d2dcd
+    apr_status_t rc = 0;
8d2dcd
     /* Copy string including null character */
8d2dcd
-    appendmem_to_linebuf(eval, sz, len + 1);
8d2dcd
+    rc = appendmem_to_linebuf(eval, sz, len + 1);
8d2dcd
+    if (rc != APR_SUCCESS) { 
8d2dcd
+        return rc;
8d2dcd
+    }
8d2dcd
     --eval->lspend; /* lspend will now point to NULL character */
8d2dcd
     /* Sync step_vars after a possible linebuf expansion */
8d2dcd
     if (step_vars && old_linebuf != eval->linebuf) {
8d2dcd
@@ -189,68 +208,84 @@ static void append_to_linebuf(sed_eval_t *eval, const char* sz,
8d2dcd
             step_vars->locs = step_vars->locs - old_linebuf + eval->linebuf;
8d2dcd
         }
8d2dcd
     }
8d2dcd
+    return APR_SUCCESS;
8d2dcd
 }
8d2dcd
 
8d2dcd
 /*
8d2dcd
  * copy_to_linebuf
8d2dcd
  */
8d2dcd
-static void copy_to_linebuf(sed_eval_t *eval, const char* sz,
8d2dcd
+static apr_status_t copy_to_linebuf(sed_eval_t *eval, const char* sz,
8d2dcd
                             step_vars_storage *step_vars)
8d2dcd
 {
8d2dcd
     eval->lspend = eval->linebuf;
8d2dcd
-    append_to_linebuf(eval, sz, step_vars);
8d2dcd
+    return append_to_linebuf(eval, sz, step_vars);
8d2dcd
 }
8d2dcd
 
8d2dcd
 /*
8d2dcd
  * append_to_holdbuf
8d2dcd
  */
8d2dcd
-static void append_to_holdbuf(sed_eval_t *eval, const char* sz)
8d2dcd
+static apr_status_t append_to_holdbuf(sed_eval_t *eval, const char* sz)
8d2dcd
 {
8d2dcd
     apr_size_t len = strlen(sz);
8d2dcd
     apr_size_t reqsize = (eval->hspend - eval->holdbuf) + len + 1;
8d2dcd
+    apr_status_t rc = 0;
8d2dcd
     if (eval->hsize <= reqsize) {
8d2dcd
-        grow_hold_buffer(eval, reqsize);
8d2dcd
+        rc = grow_hold_buffer(eval, reqsize);
8d2dcd
+        if (rc != APR_SUCCESS) { 
8d2dcd
+            return rc;
8d2dcd
+        }
8d2dcd
     }
8d2dcd
     memcpy(eval->hspend, sz, len + 1);
8d2dcd
     /* hspend will now point to NULL character */
8d2dcd
     eval->hspend += len;
8d2dcd
+    return APR_SUCCESS;
8d2dcd
 }
8d2dcd
 
8d2dcd
 /*
8d2dcd
  * copy_to_holdbuf
8d2dcd
  */
8d2dcd
-static void copy_to_holdbuf(sed_eval_t *eval, const char* sz)
8d2dcd
+static apr_status_t copy_to_holdbuf(sed_eval_t *eval, const char* sz)
8d2dcd
 {
8d2dcd
     eval->hspend = eval->holdbuf;
8d2dcd
-    append_to_holdbuf(eval, sz);
8d2dcd
+    return append_to_holdbuf(eval, sz);
8d2dcd
 }
8d2dcd
 
8d2dcd
 /*
8d2dcd
  * append_to_genbuf
8d2dcd
  */
8d2dcd
-static void append_to_genbuf(sed_eval_t *eval, const char* sz, char **gspend)
8d2dcd
+static apr_status_t append_to_genbuf(sed_eval_t *eval, const char* sz, char **gspend)
8d2dcd
 {
8d2dcd
     apr_size_t len = strlen(sz);
8d2dcd
     apr_size_t reqsize = (*gspend - eval->genbuf) + len + 1;
8d2dcd
+    apr_status_t rc = 0;
8d2dcd
     if (eval->gsize < reqsize) {
8d2dcd
-        grow_gen_buffer(eval, reqsize, gspend);
8d2dcd
+        rc = grow_gen_buffer(eval, reqsize, gspend);
8d2dcd
+        if (rc != APR_SUCCESS) { 
8d2dcd
+            return rc;
8d2dcd
+        }
8d2dcd
     }
8d2dcd
     memcpy(*gspend, sz, len + 1);
8d2dcd
     /* *gspend will now point to NULL character */
8d2dcd
     *gspend += len;
8d2dcd
+    return APR_SUCCESS;
8d2dcd
 }
8d2dcd
 
8d2dcd
 /*
8d2dcd
  * copy_to_genbuf
8d2dcd
  */
8d2dcd
-static void copy_to_genbuf(sed_eval_t *eval, const char* sz)
8d2dcd
+static apr_status_t copy_to_genbuf(sed_eval_t *eval, const char* sz)
8d2dcd
 {
8d2dcd
     apr_size_t len = strlen(sz);
8d2dcd
     apr_size_t reqsize = len + 1;
8d2dcd
+    apr_status_t rc = APR_SUCCESS;;
8d2dcd
     if (eval->gsize < reqsize) {
8d2dcd
-        grow_gen_buffer(eval, reqsize, NULL);
8d2dcd
+        rc = grow_gen_buffer(eval, reqsize, NULL);
8d2dcd
+        if (rc != APR_SUCCESS) { 
8d2dcd
+            return rc;
8d2dcd
+        }
8d2dcd
     }
8d2dcd
     memcpy(eval->genbuf, sz, len + 1);
8d2dcd
+    return rc;
8d2dcd
 }
8d2dcd
 
8d2dcd
 /*
8d2dcd
@@ -397,6 +432,7 @@ apr_status_t sed_eval_buffer(sed_eval_t *eval, const char *buf, apr_size_t bufsz
8d2dcd
     }
8d2dcd
 
8d2dcd
     while (bufsz) {
8d2dcd
+        apr_status_t rc = 0;
8d2dcd
         char *n;
8d2dcd
         apr_size_t llen;
8d2dcd
 
8d2dcd
@@ -411,7 +447,10 @@ apr_status_t sed_eval_buffer(sed_eval_t *eval, const char *buf, apr_size_t bufsz
8d2dcd
             break;
8d2dcd
         }
8d2dcd
 
8d2dcd
-        appendmem_to_linebuf(eval, buf, llen + 1);
8d2dcd
+        rc = appendmem_to_linebuf(eval, buf, llen + 1);
8d2dcd
+        if (rc != APR_SUCCESS) { 
8d2dcd
+            return rc;
8d2dcd
+        }
8d2dcd
         --eval->lspend;
8d2dcd
         /* replace new line character with NULL */
8d2dcd
         *eval->lspend = '\0';
8d2dcd
@@ -426,7 +465,10 @@ apr_status_t sed_eval_buffer(sed_eval_t *eval, const char *buf, apr_size_t bufsz
8d2dcd
 
8d2dcd
     /* Save the leftovers for later */
8d2dcd
     if (bufsz) {
8d2dcd
-        appendmem_to_linebuf(eval, buf, bufsz);
8d2dcd
+        apr_status_t rc = appendmem_to_linebuf(eval, buf, bufsz);
8d2dcd
+        if (rc != APR_SUCCESS) { 
8d2dcd
+            return rc;
8d2dcd
+        }
8d2dcd
     }
8d2dcd
 
8d2dcd
     return APR_SUCCESS;
8d2dcd
@@ -448,6 +490,7 @@ apr_status_t sed_finalize_eval(sed_eval_t *eval, void *fout)
8d2dcd
     /* Process leftovers */
8d2dcd
     if (eval->lspend > eval->linebuf) {
8d2dcd
         apr_status_t rv;
8d2dcd
+        apr_status_t rc = 0;
8d2dcd
 
8d2dcd
         if (eval->lreadyflag) {
8d2dcd
             eval->lreadyflag = 0;
8d2dcd
@@ -457,7 +500,10 @@ apr_status_t sed_finalize_eval(sed_eval_t *eval, void *fout)
8d2dcd
              * buffer is not a newline.
8d2dcd
              */
8d2dcd
             /* Assure space for NULL */
8d2dcd
-            append_to_linebuf(eval, "", NULL);
8d2dcd
+            rc = append_to_linebuf(eval, "", NULL);
8d2dcd
+            if (rc != APR_SUCCESS) { 
8d2dcd
+                return rc;
8d2dcd
+            }
8d2dcd
         }
8d2dcd
 
8d2dcd
         *eval->lspend = '\0';
8d2dcd
@@ -655,11 +701,15 @@ static apr_status_t dosub(sed_eval_t *eval, char *rhsbuf, int n,
8d2dcd
     sp = eval->genbuf;
8d2dcd
     rp = rhsbuf;
8d2dcd
     sp = place(eval, sp, lp, step_vars->loc1);
8d2dcd
+    if (sp == NULL) { 
8d2dcd
+        return APR_EGENERAL;
8d2dcd
+    }
8d2dcd
     while ((c = *rp++) != 0) {
8d2dcd
         if (c == '&') {
8d2dcd
             sp = place(eval, sp, step_vars->loc1, step_vars->loc2);
8d2dcd
-            if (sp == NULL)
8d2dcd
+            if (sp == NULL) {
8d2dcd
                 return APR_EGENERAL;
8d2dcd
+            }
8d2dcd
         }
8d2dcd
         else if (c == '\\') {
8d2dcd
             c = *rp++;
8d2dcd
@@ -675,13 +725,19 @@ static apr_status_t dosub(sed_eval_t *eval, char *rhsbuf, int n,
8d2dcd
             *sp++ = c;
8d2dcd
         if (sp >= eval->genbuf + eval->gsize) {
8d2dcd
             /* expand genbuf and set the sp appropriately */
8d2dcd
-            grow_gen_buffer(eval, eval->gsize + 1024, &sp);
8d2dcd
+            rv = grow_gen_buffer(eval, eval->gsize + 1024, &sp);
8d2dcd
+            if (rv != APR_SUCCESS) { 
8d2dcd
+                return rv;
8d2dcd
+            }
8d2dcd
         }
8d2dcd
     }
8d2dcd
     lp = step_vars->loc2;
8d2dcd
     step_vars->loc2 = sp - eval->genbuf + eval->linebuf;
8d2dcd
-    append_to_genbuf(eval, lp, &sp);
8d2dcd
-    copy_to_linebuf(eval, eval->genbuf, step_vars);
8d2dcd
+    rv = append_to_genbuf(eval, lp, &sp);
8d2dcd
+    if (rv != APR_SUCCESS) { 
8d2dcd
+        return rv;
8d2dcd
+    }
8d2dcd
+    rv = copy_to_linebuf(eval, eval->genbuf, step_vars);
8d2dcd
     return rv;
8d2dcd
 }
8d2dcd
 
8d2dcd
@@ -695,7 +751,10 @@ static char *place(sed_eval_t *eval, char *asp, char *al1, char *al2)
8d2dcd
     apr_size_t reqsize = (sp - eval->genbuf) + n + 1;
8d2dcd
 
8d2dcd
     if (eval->gsize < reqsize) {
8d2dcd
-        grow_gen_buffer(eval, reqsize, &sp);
8d2dcd
+        apr_status_t rc = grow_gen_buffer(eval, reqsize, &sp);
8d2dcd
+        if (rc != APR_SUCCESS) { 
8d2dcd
+            return NULL;
8d2dcd
+        }
8d2dcd
     }
8d2dcd
     memcpy(sp, al1, n);
8d2dcd
     return sp + n;
8d2dcd
@@ -750,7 +809,8 @@ static apr_status_t command(sed_eval_t *eval, sed_reptr_t *ipc,
8d2dcd
             }
8d2dcd
 
8d2dcd
             p1++;
8d2dcd
-            copy_to_linebuf(eval, p1, step_vars);
8d2dcd
+            rv = copy_to_linebuf(eval, p1, step_vars);
8d2dcd
+            if (rv != APR_SUCCESS) return rv;
8d2dcd
             eval->jflag++;
8d2dcd
             break;
8d2dcd
 
8d2dcd
@@ -760,21 +820,27 @@ static apr_status_t command(sed_eval_t *eval, sed_reptr_t *ipc,
8d2dcd
             break;
8d2dcd
 
8d2dcd
         case GCOM:
8d2dcd
-            copy_to_linebuf(eval, eval->holdbuf, step_vars);
8d2dcd
+            rv = copy_to_linebuf(eval, eval->holdbuf, step_vars);
8d2dcd
+            if (rv != APR_SUCCESS) return rv;
8d2dcd
             break;
8d2dcd
 
8d2dcd
         case CGCOM:
8d2dcd
-            append_to_linebuf(eval, "\n", step_vars);
8d2dcd
-            append_to_linebuf(eval, eval->holdbuf, step_vars);
8d2dcd
+            rv = append_to_linebuf(eval, "\n", step_vars);
8d2dcd
+            if (rv != APR_SUCCESS) return rv;
8d2dcd
+            rv = append_to_linebuf(eval, eval->holdbuf, step_vars);
8d2dcd
+            if (rv != APR_SUCCESS) return rv;
8d2dcd
             break;
8d2dcd
 
8d2dcd
         case HCOM:
8d2dcd
-            copy_to_holdbuf(eval, eval->linebuf);
8d2dcd
+            rv = copy_to_holdbuf(eval, eval->linebuf);
8d2dcd
+            if (rv != APR_SUCCESS) return rv;
8d2dcd
             break;
8d2dcd
 
8d2dcd
         case CHCOM:
8d2dcd
-            append_to_holdbuf(eval, "\n");
8d2dcd
-            append_to_holdbuf(eval, eval->linebuf);
8d2dcd
+            rv = append_to_holdbuf(eval, "\n");
8d2dcd
+            if (rv != APR_SUCCESS) return rv;
8d2dcd
+            rv = append_to_holdbuf(eval, eval->linebuf);
8d2dcd
+            if (rv != APR_SUCCESS) return rv;
8d2dcd
             break;
8d2dcd
 
8d2dcd
         case ICOM:
8d2dcd
@@ -896,7 +962,8 @@ static apr_status_t command(sed_eval_t *eval, sed_reptr_t *ipc,
8d2dcd
                 if (rv != APR_SUCCESS)
8d2dcd
                     return rv;
8d2dcd
             }
8d2dcd
-            append_to_linebuf(eval, "\n", step_vars);
8d2dcd
+            rv = append_to_linebuf(eval, "\n", step_vars);
8d2dcd
+            if (rv != APR_SUCCESS) return rv;
8d2dcd
             eval->pending = ipc->next;
8d2dcd
             break;
8d2dcd
 
8d2dcd
@@ -970,9 +1037,12 @@ static apr_status_t command(sed_eval_t *eval, sed_reptr_t *ipc,
8d2dcd
             break;
8d2dcd
 
8d2dcd
         case XCOM:
8d2dcd
-            copy_to_genbuf(eval, eval->linebuf);
8d2dcd
-            copy_to_linebuf(eval, eval->holdbuf, step_vars);
8d2dcd
-            copy_to_holdbuf(eval, eval->genbuf);
8d2dcd
+            rv = copy_to_genbuf(eval, eval->linebuf);
8d2dcd
+            if (rv != APR_SUCCESS) return rv;
8d2dcd
+            rv = copy_to_linebuf(eval, eval->holdbuf, step_vars);
8d2dcd
+            if (rv != APR_SUCCESS) return rv;
8d2dcd
+            rv = copy_to_holdbuf(eval, eval->genbuf);
8d2dcd
+            if (rv != APR_SUCCESS) return rv;
8d2dcd
             break;
8d2dcd
 
8d2dcd
         case YCOM: