1. 17 8月, 2020 1 次提交
  2. 17 9月, 2018 1 次提交
    • R
      fix null pointer subtraction and comparison in stdio · 849e7603
      Rich Felker 提交于
      morally, for null pointers a and b, a-b, a<b, and a>b should all be
      defined as 0; however, C does not define any of them.
      
      the stdio implementation makes heavy use of such pointer comparison
      and subtraction for buffer logic, and also uses null pos/base/end
      pointers to indicate that the FILE is not in the corresponding (read
      or write) mode ready for accesses through the buffer.
      
      all of the comparisons are fixed trivially by using != in place of the
      relational operators, since the opposite relation (e.g. pos>end) is
      logically impossible. the subtractions have been reviewed to check
      that they are conditional the stream being in the appropriate reading-
      or writing-through-buffer mode, with checks added where needed.
      
      in fgets and getdelim, the checks added should improve performance for
      unbuffered streams by avoiding a do-nothing call to memchr, and should
      be negligible for buffered streams.
      849e7603
  3. 13 9月, 2018 2 次提交
  4. 29 3月, 2016 1 次提交
    • R
      fix undefined pointer comparison in stdio-internal __toread · 6d1a3dfe
      Rich Felker 提交于
      the comparison f->wpos > f->buf has undefined behavior when f->wpos is
      a null pointer, despite the intuition (and actual compiler behavior,
      for all known compilers) being that NULL > ptr is false for all valid
      pointers ptr.
      
      the purpose of the comparison is to determine if the write buffer is
      non-empty, and the idiom used elsewhere for that is comparison against
      f->wbase, which is either a null pointer when not writing, or equal to
      f->buf when writing. in the former case, both f->wpos and f->wbase are
      null; in the latter they are both non-null and point into the same
      array.
      6d1a3dfe
  5. 29 5月, 2015 1 次提交
    • R
      fix failure of ungetc and ungetwc to work on files in eof status · 2b4fcfda
      Rich Felker 提交于
      these functions were written to handle clearing eof status, but failed
      to account for the __toread function's handling of eof. with this
      patch applied, __toread still returns EOF when the file is in eof
      status, so that read operations will fail, but it also sets up valid
      buffer pointers for read mode, which are set to the end of the buffer
      rather than the beginning in order to make the whole buffer available
      to ungetc/ungetwc.
      
      minor changes to __uflow were needed since it's now possible to have
      non-zero buffer pointers while in eof status. as made, these changes
      remove a 'fast path' bypassing the function call to __toread, which
      could be reintroduced with slightly different logic, but since
      ordinary files have a syscall in f->read, optimizing the code path
      does not seem worthwhile.
      
      the __stdio_read function is also updated not to zero the read buffer
      pointers on eof/error. while not necessary for correctness, this
      change avoids the overhead of calling __toread in ungetc after
      reaching eof, and it also reduces code size and increases consistency
      with the fmemopen read operation which does not zero the pointers.
      2b4fcfda
  6. 17 7月, 2014 1 次提交
    • R
      simplify __stdio_exit static linking logic · c463e11e
      Rich Felker 提交于
      the purpose of this logic is to avoid linking __stdio_exit unless any
      stdio reads (which might require repositioning the file offset at exit
      time) or writes (which might require flushing at exit time) could have
      been performed.
      
      previously, exit called two wrapper functions for __stdio_exit named
      __flush_on_exit and __seek_on_exit. both of these functions actually
      performed both tasks (seek and flushing) by calling the underlying
      __stdio_exit. in order to avoid doing this twice, an overridable data
      object __towrite_used was used to cause __seek_on_exit to act as a nop
      when __towrite was linked.
      
      now, exit only makes one call, directly to __stdio_exit. this is
      satisfiable by a weak dummy definition in exit.c, but the real
      definition is pulled in by either __toread.c or __towrite.c through
      their referencing a symbol which is defined only in __stdio_exit.c.
      c463e11e
  7. 03 7月, 2012 1 次提交
  8. 19 6月, 2012 1 次提交
    • R
      stdio: handle file position correctly at program exit · a71e0af2
      Rich Felker 提交于
      for seekable files, posix imposed requirements on the offset of the
      underlying open file description after a stream is closed. this was
      correctly handled (as a side effect of the unconditional fflush call)
      when streams were explicitly closed by fclose, but was not handled
      correctly at program exit time, where fflush(0) was being used.
      
      the weak symbol hackery is to pull in __stdio_exit if either of
      __toread or __towrite is used, but avoid calling it twice so we don't
      have to keep extra state. the new __stdio_exit is a streamlined fflush
      variant that avoids performing any unnecessary operations and which
      never unlocks the files or open file list, so we can be sure no other
      threads write new data to a stream's buffer after it's already
      flushed.
      a71e0af2
  9. 28 3月, 2011 1 次提交
    • R
      major stdio overhaul, using readv/writev, plus other changes · e3cd6c5c
      Rich Felker 提交于
      the biggest change in this commit is that stdio now uses readv to fill
      the caller's buffer and the FILE buffer with a single syscall, and
      likewise writev to flush the FILE buffer and write out the caller's
      buffer in a single syscall.
      
      making this change required fundamental architectural changes to
      stdio, so i also made a number of other improvements in the process:
      
      - the implementation no longer assumes that further io will fail
        following errors, and no longer blocks io when the error flag is set
        (though the latter could easily be changed back if desired)
      
      - unbuffered mode is no longer implemented as a one-byte buffer. as a
        consequence, scanf unreading has to use ungetc, to the unget buffer
        has been enlarged to hold at least 2 wide characters.
      
      - the FILE structure has been rearranged to maintain the locations of
        the fields that might be used in glibc getc/putc type macros, while
        shrinking the structure to save some space.
      
      - error cases for fflush, fseek, etc. should be more correct.
      
      - library-internal macros are used for getc_unlocked and putc_unlocked
        now, eliminating some ugly code duplication. __uflow and __overflow
        are no longer used anywhere but these macros. switch to read or
        write mode is also separated so the code can be better shared, e.g.
        with ungetc.
      
      - lots of other small things.
      e3cd6c5c