fix cgi stderr logger + reuse stderr tmp buffer

Sun, 04 Dec 2022 10:20:21 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 04 Dec 2022 10:20:21 +0100
changeset 451
edbbb3000494
parent 450
d7b276de183b
child 452
ce359a2b51fe

fix cgi stderr logger + reuse stderr tmp buffer

src/server/safs/cgi.c file | annotate | diff | comparison | revisions
src/server/safs/cgi.h file | annotate | diff | comparison | revisions
--- a/src/server/safs/cgi.c	Sun Dec 04 09:09:55 2022 +0100
+++ b/src/server/safs/cgi.c	Sun Dec 04 10:20:21 2022 +0100
@@ -372,13 +372,14 @@
     
     char  buf[4096];
     char *line = buf;
-    int line_start = 0;
+    int line_start;
     ssize_t r;
     while((r = read(handler->process.err[0], buf, 4096)) > 0) {
+        line_start = 0;
         int pos = 0;
         // log stderr output lines
         for(int i=0;i<r;i++) {
-            if(buf[i] == '\n') {
+            if(buf[i] == '\n') {     
                 log_ereport(
                         LOG_INFORM,
                         "cgi pid %d %s stderr: %.*s%.*s",
@@ -392,8 +393,6 @@
                 pos = i+1;
                 
                 if(handler->stderr_tmp) {
-                    pool_free(pool, handler->stderr_tmp);
-                    handler->stderr_tmp = NULL;
                     handler->stderr_tmplen = 0;
                 }
             }
@@ -402,18 +401,44 @@
         // check for incomplete line
         if(pos < r) {
             int tmplen = r-pos;
-            if(handler->stderr_tmp) {
-                handler->stderr_tmp = pool_realloc(pool, handler->stderr_tmp, handler->stderr_tmplen + tmplen);
+            if(handler->stderr_tmplen > 0) {
+                // append new text to the temp buffer
+                if(handler->stderr_tmplen + tmplen > handler->stderr_tmpalloc) {
+                    handler->stderr_tmpalloc = handler->stderr_tmplen + tmplen;
+                    handler->stderr_tmp = pool_realloc(pool, handler->stderr_tmp, handler->stderr_tmpalloc);
+                    if(!handler->stderr_tmp) {
+                        log_ereport(LOG_FAILURE, "send-cgi: cannot create tmp buffer for parsing stderr");
+                        handler->stderr_tmpalloc = 0;
+                        handler->stderr_tmplen = 0;
+                        continue;
+                    }
+                }
                 memcpy(handler->stderr_tmp + handler->stderr_tmplen, line + line_start, tmplen);
                 handler->stderr_tmplen += tmplen;
             } else {
-                handler->stderr_tmp = pool_malloc(pool, tmplen);
+                if(handler->stderr_tmpalloc < tmplen) {
+                    // tmp buffer too small or not allocated
+                    handler->stderr_tmpalloc = tmplen < 256 ? 256 : tmplen;
+                    if(handler->stderr_tmp) {
+                        // free old tmp buf
+                        // pool_realloc doesn't make sense here, because it
+                        // is just free+malloc+memcpy and we don't need the
+                        // memcpy part, because we are just reusing the buffer
+                        // and the previous content doesn't matter
+                        pool_free(pool, handler->stderr_tmp);
+                    }
+                    handler->stderr_tmp = pool_malloc(pool, handler->stderr_tmpalloc);
+                    if(!handler->stderr_tmp) {
+                        log_ereport(LOG_FAILURE, "send-cgi: cannot create tmp buffer for parsing stderr");
+                        handler->stderr_tmpalloc = 0;
+                        handler->stderr_tmplen = 0;
+                        continue;
+                    }
+                }
                 memcpy(handler->stderr_tmp, line + line_start, tmplen);
                 handler->stderr_tmplen = tmplen;
             }
         } else {
-            pool_free(pool, handler->stderr_tmp);
-            handler->stderr_tmp = NULL;
             handler->stderr_tmplen = 0;
         } 
     }
--- a/src/server/safs/cgi.h	Sun Dec 04 09:09:55 2022 +0100
+++ b/src/server/safs/cgi.h	Sun Dec 04 10:20:21 2022 +0100
@@ -62,6 +62,7 @@
     Event *stderrev;
     char *stderr_tmp;
     int stderr_tmplen;
+    int stderr_tmpalloc;
     char *writebuf;
     size_t writebuf_alloc;
     size_t writebuf_size;

mercurial