Browse Source

[LibOS] Fix buffer overflow in debug_fputch

When debug buf reached to the end, it should be reset to the start after
output.
This can happen with
debug_printf("%s",
"string-without-newline-longer-than-debug_buf-DEBUGBUF_SIZE=255"...);

Signed-off-by: Isaku Yamahata <isaku.yamahata@gmail.com>
Isaku Yamahata 6 years ago
parent
commit
19fa7b04cd
1 changed files with 3 additions and 3 deletions
  1. 3 3
      LibOS/shim/src/utils/printf.c

+ 3 - 3
LibOS/shim/src/utils/printf.c

@@ -50,10 +50,9 @@ debug_fputch (void * f, int ch, void * b)
     buf->buf[buf->end++] = ch;
 
     if (ch == '\n') {
-        if (debug_fputs(NULL, buf->buf, buf->end) == -1)
-            return -1;
+        int ret = debug_fputs(NULL, buf->buf, buf->end);
         buf->end = buf->start;
-        return 0;
+        return ret;
     }
 
 #if DEBUGBUF_BREAK == 1
@@ -69,6 +68,7 @@ debug_fputch (void * f, int ch, void * b)
 #else
     if (buf->end == DEBUGBUF_SIZE) {
         debug_fputs(NULL, buf->buf, buf->end);
+        buf->end = buf->start;
     }
 #endif