qemacs-commit
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Qemacs-commit] qemacs buffer.c


From: Charlie Gordon
Subject: [Qemacs-commit] qemacs buffer.c
Date: Mon, 11 Dec 2006 23:10:33 +0000

CVSROOT:        /cvsroot/qemacs
Module name:    qemacs
Changes by:     Charlie Gordon <chqrlie>        06/12/11 23:10:33

Modified files:
        .              : buffer.c 

Log message:
        added eb_find_window()
        added eb_trace_bytes()
        improved eb_printf(): removed potential buffer overflow

CVSWeb URLs:
http://cvs.savannah.gnu.org/viewcvs/qemacs/buffer.c?cvsroot=qemacs&r1=1.10&r2=1.11

Patches:
Index: buffer.c
===================================================================
RCS file: /cvsroot/qemacs/qemacs/buffer.c,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -b -r1.10 -r1.11
--- buffer.c    9 Dec 2006 19:02:07 -0000       1.10
+++ buffer.c    11 Dec 2006 23:10:33 -0000      1.11
@@ -531,6 +531,50 @@
     return NULL;
 }
 
+/* Find the next window showing a given buffer */
+EditState *eb_find_window(EditBuffer *b, EditState *e)
+{
+    QEmacsState *qs = &qe_state;
+
+    for (e = e ? e->next_window : qs->first_window;
+         e != NULL;
+         e = e->next_window)
+    {
+            if (e->b == b)
+                return e;
+    }
+    return NULL;
+}
+
+void eb_trace_bytes(void *buf, int size, int state)
+{
+    EditBuffer *b = trace_buffer;
+    EditState *e;
+    int point;
+
+    if (b) {
+        point = b->total_size;
+        if (trace_buffer_state != state) {
+            trace_buffer_state = state;
+            eb_write(b, b->total_size,
+                     state == EB_TRACE_TTY ? "\n--|" : "|--\n", 4);
+        }
+#if 0
+        /* CG: could make traces more readable: */
+        if (ch < 32 || ch == 127)
+            fprintf(stderr, "got %d '^%c'\n", ch, ('@' + ch) & 127);
+        else
+            fprintf(stderr, "got %d '%c'\n", ch, ch);
+#endif
+        eb_write(b, b->total_size, buf, size);
+
+        /* If point is visible in window, should keep it so */
+        e = eb_find_window(b, NULL);
+        if (e && e->offset == point)
+            e->offset = b->total_size;
+    }
+}
+
 /************************************************************/
 /* callbacks */
 
@@ -1289,13 +1333,23 @@
 
 void eb_printf(EditBuffer *b, const char *fmt, ...)
 {
+    char buf0[1024];
+    char *buf;
+    int len, size;
     va_list ap;
-    char buf[1024];
-    int len;
 
     va_start(ap, fmt);
-    len = vsnprintf(buf, sizeof(buf), fmt, ap);
+    size = sizeof(buf0);
+    buf = buf0;
+    len = vsnprintf(buf, size, fmt, ap);
+    va_end(ap);
+    if (len >= size) {
+        va_start(ap, fmt);
+        size = len + 1;
+        buf = alloca(size);
+        vsnprintf(buf, size, fmt, ap);
     va_end(ap);
+    }
     eb_insert(b, b->total_size, buf, len);
 }
 




reply via email to

[Prev in Thread] Current Thread [Next in Thread]