mod_sed constant length buffer sizes fix :
Three buffers linebuf, genbuf and holdbuf are now reallocated dynamically.
Sed code has 3 buffers :
linebuf : Store a line.
holdbuf : store the intemediate hold data (sed h/H commands)
genbuf : Used in substitution intermediate results.
Original sed has the limitation on the fixed size buffers.
In this fix all these 3 buffers are adjusted dynamically. Initial buffer size
of these buffers is 1024 bytes. When these buffers needs reallocation, their
new buffer size is aligned to 4KB boundary.
List of affected files :
sed1.c libsed.h
Code has been updated and can be obtained by mercurial as :
$ hg clone ssh://[EMAIL PROTECTED]/hg/webstack/mod_sed
It should soon be visible at :
http://src.opensolaris.org/source/xref/webstack/mod_sed/
Diff is attached.
Regards,
Basant.
-
diff -r 32f5eb1dc14f libsed.h
--- a/libsed.h Wed Apr 30 11:52:57 2008 -0700
+++ b/libsed.h Sat Jun 28 20:35:06 2008 -0700
@@ -121,17 +121,14 @@
unsigned lsize;
char *linebuf;
-char *lbend;
char *lspend;
unsigned hsize;
char *holdbuf;
-char *hbend;
char *hspend;
unsigned gsize;
char *genbuf;
-char *gbend;
char *lcomend;
apr_file_t*fcode[NWFILES];
diff -r 32f5eb1dc14f sed1.c
--- a/sed1.cWed Apr 30 11:52:57 2008 -0700
+++ b/sed1.cSat Jun 28 20:35:06 2008 -0700
@@ -86,6 +86,151 @@
}
}
+#define INIT_BUF_SIZE 1024
+
+/*
+ * grow_buffer
+ */
+static void grow_buffer(apr_pool_t *pool, char **buffer,
+char **spend, unsigned int *cursize,
+unsigned int newsize)
+{
+char* newbuffer = NULL;
+int spendsize = 0;
+if (*cursize = newsize)
+return;
+/* Align it to 4 KB boundary */
+newsize = (newsize + ((1 12) - 1)) ~((1 12) -1);
+newbuffer = apr_pcalloc(pool, newsize);
+if (*spend *buffer (*cursize 0)) {
+spendsize = *spend - *buffer;
+}
+if ((*cursize 0) *buffer) {
+memcpy(newbuffer, *buffer, *cursize);
+}
+*buffer = newbuffer;
+*cursize = newsize;
+if (spend != buffer) {
+*spend = *buffer + spendsize;
+}
+}
+
+/*
+ * grow_line_buffer
+ */
+static void grow_line_buffer(sed_eval_t *eval, int newsize)
+{
+grow_buffer(eval-pool, eval-linebuf, eval-lspend,
+eval-lsize, newsize);
+}
+
+/*
+ * grow_hold_buffer
+ */
+static void grow_hold_buffer(sed_eval_t *eval, int newsize)
+{
+grow_buffer(eval-pool, eval-holdbuf, eval-hspend,
+eval-hsize, newsize);
+}
+
+/*
+ * grow_gen_buffer
+ */
+static void grow_gen_buffer(sed_eval_t *eval, int newsize,
+char **gspend)
+{
+if (gspend == NULL) {
+gspend = eval-genbuf;
+}
+grow_buffer(eval-pool, eval-genbuf, gspend,
+eval-gsize, newsize);
+eval-lcomend = eval-genbuf[71];
+}
+
+/*
+ * appendmem_to_linebuf
+ */
+static void appendmem_to_linebuf(sed_eval_t *eval, const char* sz, int len)
+{
+int reqsize = (eval-lspend - eval-linebuf) + len;
+if (eval-lsize reqsize) {
+grow_line_buffer(eval, reqsize);
+}
+memcpy(eval-lspend, sz, len);
+eval-lspend += len;
+}
+
+/*
+ * append_to_linebuf
+ */
+static void append_to_linebuf(sed_eval_t *eval, const char* sz)
+{
+int len = strlen(sz);
+/* Copy string including null character */
+appendmem_to_linebuf(eval, sz, len + 1);
+--eval-lspend; /* lspend will now point to NULL character */
+}
+
+/*
+ * copy_to_linebuf
+ */
+static void copy_to_linebuf(sed_eval_t *eval, const char* sz)
+{
+eval-lspend = eval-linebuf;
+append_to_linebuf(eval, sz);
+}
+
+/*
+ * append_to_holdbuf
+ */
+static void append_to_holdbuf(sed_eval_t *eval, const char* sz)
+{
+int len = strlen(sz);
+int reqsize = (eval-hspend - eval-holdbuf) + len + 1;
+if (eval-hsize = reqsize) {
+grow_hold_buffer(eval, reqsize);
+}
+strcpy(eval-hspend, sz);
+/* hspend will now point to NULL character */
+eval-hspend += len;
+}
+
+/*
+ * copy_to_holdbuf
+ */
+static void copy_to_holdbuf(sed_eval_t *eval, const char* sz)
+{
+eval-hspend = eval-holdbuf;
+append_to_holdbuf(eval, sz);
+}
+
+/*
+ * append_to_genbuf
+ */
+static void append_to_genbuf(sed_eval_t *eval, const char* sz, char **gspend)
+{
+int len = strlen(sz);
+int reqsize = (*gspend - eval-genbuf) + len + 1;
+if (eval-gsize reqsize) {
+grow_gen_buffer(eval, reqsize, gspend);
+}
+strcpy(*gspend, sz);
+/* *gspend will now point to NULL character */
+*gspend += len;
+}
+
+/*
+ * copy_to_genbuf
+ */
+static void copy_to_genbuf(sed_eval_t *eval, const char* sz)
+{
+int len = strlen(sz);
+int reqsize = len + 1;
+if (eval-gsize reqsize) {
+grow_gen_buffer(eval, reqsize, NULL);
+