]> git.ipfire.org Git - thirdparty/glibc.git/commitdiff
libio: Convert __obstack_vprintf_internal to buffers (bug 27124)
authorFlorian Weimer <fweimer@redhat.com>
Mon, 19 Dec 2022 17:56:55 +0000 (18:56 +0100)
committerFlorian Weimer <fweimer@redhat.com>
Mon, 19 Dec 2022 17:56:55 +0000 (18:56 +0100)
This fixes bug 27124 because the problematic built-in vtable is gone.

Reviewed-by: Adhemerval Zanella <adhemerval.zanella@linaro.org>
elf/Makefile
include/printf_buffer.h
libio/obprintf.c
stdio-common/printf_buffer_flush.c

index 6d566a4ff4da5b1382ca584007807e889858d786..36901ac3aba701d27396105ab773ada48a3be326 100644 (file)
@@ -600,7 +600,6 @@ $(objpfx)tst-relro-libc.out: tst-relro-symbols.py $(..)/scripts/glibcelf.py \
            --required=_IO_file_jumps_maybe_mmap \
            --required=_IO_file_jumps_mmap \
            --required=_IO_mem_jumps \
-           --required=_IO_obstack_jumps \
            --required=_IO_printf_buffer_as_file_jumps \
            --required=_IO_proc_jumps \
            --required=_IO_str_jumps \
index e2ca2d72d59778aa1433ccc29e96f40beaab8e05..168555d8d32102f41cd51ab3dc2fe7b11f6a26ff 100644 (file)
@@ -55,6 +55,7 @@ enum __printf_buffer_mode
     __printf_buffer_mode_fp,         /* For __printf_fp_l_buffer.  */
     __printf_buffer_mode_fp_to_wide, /* For __wprintf_fp_l_buffer.  */
     __printf_buffer_mode_fphex_to_wide, /* For __wprintf_fphex_l_buffer.  */
+    __printf_buffer_mode_obstack,    /* For __printf_buffer_flush_obstack.  */
   };
 
 /* Buffer for fast character writing with overflow handling.
@@ -322,6 +323,9 @@ struct __printf_buffer_fphex_to_wide;
 void __printf_buffer_flush_fphex_to_wide (struct
                                           __printf_buffer_fphex_to_wide *)
   attribute_hidden;
+struct __printf_buffer_obstack;
+void __printf_buffer_flush_obstack (struct __printf_buffer_obstack *)
+  attribute_hidden;
 
 struct __wprintf_buffer_to_file;
 void __wprintf_buffer_flush_to_file (struct __wprintf_buffer_to_file *)
index c220be9adc170957138753ec7d6848cb0768788e..459d73ebf312495e6477c0cfd7ceb6c2cc6fc065 100644 (file)
    License along with the GNU C Library; if not, see
    <https://www.gnu.org/licenses/>.  */
 
-
-#include <stdlib.h>
-#include "libioP.h"
-#include "strfile.h"
 #include <assert.h>
-#include <string.h>
-#include <errno.h>
+#include <math_ldbl_opt.h>
 #include <obstack.h>
+#include <printf.h>
 #include <stdarg.h>
-#include <stdio_ext.h>
-
+#include <printf_buffer.h>
 
-struct _IO_obstack_file
+struct __printf_buffer_obstack
 {
-  struct _IO_FILE_plus file;
+  struct __printf_buffer base;
   struct obstack *obstack;
-};
-
-
-static int
-_IO_obstack_overflow (FILE *fp, int c)
-{
-  struct obstack *obstack = ((struct _IO_obstack_file *) fp)->obstack;
-  int size;
-
-  /* Make room for another character.  This might as well allocate a
-     new chunk a memory and moves the old contents over.  */
-  assert (c != EOF);
-  obstack_1grow (obstack, c);
-
-  /* Setup the buffer pointers again.  */
-  fp->_IO_write_base = obstack_base (obstack);
-  fp->_IO_write_ptr = obstack_next_free (obstack);
-  size = obstack_room (obstack);
-  fp->_IO_write_end = fp->_IO_write_ptr + size;
-  /* Now allocate the rest of the current chunk.  */
-  obstack_blank_fast (obstack, size);
-
-  return c;
-}
 
+  /* obstack_1grow is called for compatibility reasons.  This needs
+     one extra character, and this is the backing store for it.  */
+  char ch;
+};
 
-static size_t
-_IO_obstack_xsputn (FILE *fp, const void *data, size_t n)
+void
+__printf_buffer_flush_obstack (struct __printf_buffer_obstack *buf)
 {
-  struct obstack *obstack = ((struct _IO_obstack_file *) fp)->obstack;
+  /* About to switch buffers, so record the bytes written so far.  */
+  buf->base.written += buf->base.write_ptr - buf->base.write_base;
 
-  if (fp->_IO_write_ptr + n > fp->_IO_write_end)
+  if (buf->base.write_ptr == &buf->ch + 1)
     {
-      int size;
-
-      /* We need some more memory.  First shrink the buffer to the
-        space we really currently need.  */
-      obstack_blank_fast (obstack, fp->_IO_write_ptr - fp->_IO_write_end);
-
-      /* Now grow for N bytes, and put the data there.  */
-      obstack_grow (obstack, data, n);
-
-      /* Setup the buffer pointers again.  */
-      fp->_IO_write_base = obstack_base (obstack);
-      fp->_IO_write_ptr = obstack_next_free (obstack);
-      size = obstack_room (obstack);
-      fp->_IO_write_end = fp->_IO_write_ptr + size;
-      /* Now allocate the rest of the current chunk.  */
-      obstack_blank_fast (obstack, size);
+      /* Errors are reported via a callback mechanism (presumably for
+        process termination).  */
+      obstack_1grow (buf->obstack, buf->ch);
+      buf->base.write_base = obstack_next_free (buf->obstack);
+      buf->base.write_ptr = buf->base.write_base;
+      size_t size = obstack_room (buf->obstack);
+      buf->base.write_end = buf->base.write_ptr + size;
+      /* Reserve the space on the obstack size.  */
+      obstack_blank_fast (buf->obstack, size);
     }
   else
-    fp->_IO_write_ptr = __mempcpy (fp->_IO_write_ptr, data, n);
-
-  return n;
+    {
+      /* Obtain the extra character.  */
+      buf->base.write_base = &buf->ch;
+      buf->base.write_ptr = &buf->ch;
+      buf->base.write_end = &buf->ch + 1;
+    }
 }
 
-
-/* the jump table.  */
-const struct _IO_jump_t _IO_obstack_jumps libio_vtable attribute_hidden =
-{
-  JUMP_INIT_DUMMY,
-  JUMP_INIT(finish, NULL),
-  JUMP_INIT(overflow, _IO_obstack_overflow),
-  JUMP_INIT(underflow, NULL),
-  JUMP_INIT(uflow, NULL),
-  JUMP_INIT(pbackfail, NULL),
-  JUMP_INIT(xsputn, _IO_obstack_xsputn),
-  JUMP_INIT(xsgetn, NULL),
-  JUMP_INIT(seekoff, NULL),
-  JUMP_INIT(seekpos, NULL),
-  JUMP_INIT(setbuf, NULL),
-  JUMP_INIT(sync, NULL),
-  JUMP_INIT(doallocate, NULL),
-  JUMP_INIT(read, NULL),
-  JUMP_INIT(write, NULL),
-  JUMP_INIT(seek, NULL),
-  JUMP_INIT(close, NULL),
-  JUMP_INIT(stat, NULL),
-  JUMP_INIT(showmanyc, NULL),
-  JUMP_INIT(imbue, NULL)
-};
-
-
 int
 __obstack_vprintf_internal (struct obstack *obstack, const char *format,
                            va_list args, unsigned int mode_flags)
 {
-  struct obstack_FILE
-    {
-      struct _IO_obstack_file ofile;
-  } new_f;
-  int result;
-  int size;
-  int room;
-
-#ifdef _IO_MTSAFE_IO
-  new_f.ofile.file.file._lock = NULL;
-#endif
-
-  _IO_no_init (&new_f.ofile.file.file, _IO_USER_LOCK, -1, NULL, NULL);
-  _IO_JUMPS (&new_f.ofile.file) = &_IO_obstack_jumps;
-  room = obstack_room (obstack);
-  size = obstack_object_size (obstack) + room;
+  /* Legacy setup code for compatibility.  */
+  size_t room = obstack_room (obstack);
+  size_t size = obstack_object_size (obstack) + room;
   if (size == 0)
     {
-      /* We have to handle the allocation a bit different since the
-        `_IO_str_init_static' function would handle a size of zero
-        different from what we expect.  */
-
       /* Get more memory.  */
       obstack_make_room (obstack, 64);
 
@@ -151,27 +79,29 @@ __obstack_vprintf_internal (struct obstack *obstack, const char *format,
       assert (size != 0);
     }
 
-  _IO_str_init_static_internal ((struct _IO_strfile_ *) &new_f.ofile,
-                               obstack_base (obstack),
-                               size, obstack_next_free (obstack));
+  struct __printf_buffer_obstack buf;
+  {
+    /* The obstack write location might be in the middle of an object.  */
+    char *ptr = obstack_next_free (obstack);
+    char *end = obstack_base (obstack) + size;
+    __printf_buffer_init (&buf.base, ptr, end - ptr,
+                         __printf_buffer_mode_obstack);
+  }
+  buf.obstack = obstack;
+
   /* Now allocate the rest of the current chunk.  */
-  assert (size == (new_f.ofile.file.file._IO_write_end
-                  - new_f.ofile.file.file._IO_write_base));
-  assert (new_f.ofile.file.file._IO_write_ptr
-         == (new_f.ofile.file.file._IO_write_base
-             + obstack_object_size (obstack)));
   obstack_blank_fast (obstack, room);
 
-  new_f.ofile.obstack = obstack;
-
-  result = __vfprintf_internal (&new_f.ofile.file.file, format, args,
-                               mode_flags);
+  __printf_buffer (&buf.base, format, args, mode_flags);
 
-  /* Shrink the buffer to the space we really currently need.  */
-  obstack_blank_fast (obstack, (new_f.ofile.file.file._IO_write_ptr
-                               - new_f.ofile.file.file._IO_write_end));
+  if (buf.base.write_ptr == &buf.ch + 1)
+    /* buf.ch is in use.  Put it into the obstack.  */
+    obstack_1grow (buf.obstack, buf.ch);
+  else if (buf.base.write_ptr != &buf.ch)
+    /* Shrink the buffer to the space we really currently need.  */
+    obstack_blank_fast (buf.obstack, buf.base.write_ptr - buf.base.write_end);
 
-  return result;
+  return __printf_buffer_done (&buf.base);
 }
 
 int
index 922340cc54667e7996f9541796e8d2d8bd063d9b..42afe4941393b0d5ae4f31af308e727c5a6f6a3e 100644 (file)
@@ -32,6 +32,7 @@
 # pragma weak __printf_buffer_flush_fp
 # pragma weak __printf_buffer_flush_fp_to_wide
 # pragma weak __printf_buffer_flush_fphex_to_wide
+# pragma weak __printf_buffer_flush_obstack
 #endif /* !SHARED */
 
 static void
@@ -72,6 +73,9 @@ __printf_buffer_do_flush (struct __printf_buffer *buf)
       __printf_buffer_flush_fphex_to_wide
         ((struct __printf_buffer_fphex_to_wide *) buf);
       return;
+    case __printf_buffer_mode_obstack:
+      __printf_buffer_flush_obstack ((struct __printf_buffer_obstack *) buf);
+      return;
     }
   __builtin_trap ();
 }