Author: leo
Date: Wed Jul 13 11:28:36 2005
New Revision: 8617

Modified:
   trunk/io/io.c
   trunk/io/io_buf.c
Log:
fix a nasty bug where  malloced IO memory was freed,
albeit COW copies existed - never malloc string mem, 
its not properly handled by GC

Modified: trunk/io/io.c
==============================================================================
--- trunk/io/io.c       (original)
+++ trunk/io/io.c       Wed Jul 13 11:28:36 2005
@@ -98,30 +98,19 @@ gets allocated.
 */
 
 STRING *
-PIO_make_io_string(Interp *interpreter, STRING **buf, size_t default_len)
+PIO_make_io_string(Interp *interpreter, STRING **buf, size_t len)
 {
-    size_t len;
     STRING *s;
     /*
      * when we get a NULL string, we read a default len
      */
     if (*buf == NULL) {
-       *buf = new_string_header(interpreter, 0);
-        (*buf)->bufused = default_len;
+       *buf = string_make_empty(interpreter, enum_stringrep_one, len);
+        return *buf;
     }
     s = *buf;
-    len = s->bufused;
-    if (!s->strstart && len) {
-        PObj_bufstart(s) = s->strstart = mem_sys_allocate(len);
-        PObj_buflen(s) = len;
-        PObj_sysmem_SET(s);
-        PObj_external_SET(s);
-        s->charset = Parrot_iso_8859_1_charset_ptr;
-        s->encoding = Parrot_fixed_8_encoding_ptr;
-        /*
-         * TODO encoding = raw
-         */
-    }
+    if (s->bufused < len)
+        Parrot_allocate_string(interpreter, s, len);
     return s;
 }
 
@@ -839,7 +828,7 @@ PIO_reads(theINTERP, PMC *pmc, size_t le
         res->encoding = Parrot_fixed_8_encoding_ptr;
     }
     else
-        res = PIO_make_io_string(interpreter, &res, len );
+        res = PIO_make_io_string(interpreter, &res, len);
 
     res->bufused = len;
     PIO_read_down(interpreter, l, io, &res);
@@ -1010,6 +999,11 @@ PIO_putps(theINTERP, PMC *pmc, STRING *s
     ParrotIO *io = PMC_data0(pmc);
     assert((unsigned int)l != 0xdeadbeefU);
     assert(io != 0);
+#if ! DISABLE_GC_DEBUG
+    /* trigger GC for debug - but not during tests */
+    if (0 && GC_DEBUG(interpreter))
+        Parrot_do_dod_run(interpreter, DOD_trace_stack_FLAG);
+#endif
     return PIO_write_down(interpreter, l, io, s);
 }
 

Modified: trunk/io/io_buf.c
==============================================================================
--- trunk/io/io_buf.c   (original)
+++ trunk/io/io_buf.c   Wed Jul 13 11:28:36 2005
@@ -551,14 +551,12 @@ PIO_buf_readline(theINTERP, ParrotIOLaye
     ParrotIOBuf *b = &io->b;
     size_t len;
     STRING *s;
-    int may_realloc;
 
     if (*buf == NULL) {
         *buf = new_string_header(interpreter, 0);
     }
     s = *buf;
     s->strlen = 0;
-    may_realloc = s->strstart == NULL;
 
     /* fill empty buffer */
     if (!(b->flags & PIO_BF_READBUF)) {
@@ -581,17 +579,11 @@ PIO_buf_readline(theINTERP, ParrotIOLaye
         if (b->next == b->endb) {
             len = b->endb - buf_start;
             if (s->bufused < l) {
-                if (may_realloc) {
-                    s->charset = Parrot_iso_8859_1_charset_ptr;
-                    s->encoding = Parrot_fixed_8_encoding_ptr;
-                    if (s->strstart) {
-                        Parrot_reallocate_string(interpreter, s, l);
-                    } else {
-                        Parrot_allocate_string(interpreter, s, l);
-                    }
+                if (s->strstart) {
+                    Parrot_reallocate_string(interpreter, s, l);
+                } else {
+                    Parrot_allocate_string(interpreter, s, l);
                 }
-                else
-                    internal_exception(1, "readline: buffer too short");
             }
             out_buf = (unsigned char*)s->strstart + s->strlen;
             memcpy(out_buf, buf_start, len);
@@ -602,17 +594,11 @@ PIO_buf_readline(theINTERP, ParrotIOLaye
         }
     }
     if (s->bufused < l) {
-        if (may_realloc) {
-            s->charset = Parrot_iso_8859_1_charset_ptr;
-            s->encoding = Parrot_fixed_8_encoding_ptr;
-            if (s->strstart) {
-                Parrot_reallocate_string(interpreter, s, l);
-            } else {
-                Parrot_allocate_string(interpreter, s, l);
-            }
+        if (s->strstart) {
+            Parrot_reallocate_string(interpreter, s, l);
+        } else {
+            Parrot_allocate_string(interpreter, s, l);
         }
-        else
-            internal_exception(1, "readline: buffer too short");
     }
     out_buf = (unsigned char*)s->strstart + s->strlen;
     len = b->next - buf_start;

Reply via email to