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