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