1. 15 9月, 2018 1 次提交
    • R
      fix undefined behavior in strto* via FILE buffer pointer abuse · d6c855ca
      Rich Felker 提交于
      in order to produce FILE objects to pass to the intscan/floatscan
      backends without any (prohibitively costly) extra buffering layer, the
      strto* functions set the FILE's rend (read end) buffer pointer to an
      invalid value at the end of the address space, or SIZE_MAX/2 past the
      beginning of the string. this led to undefined behavior comparing and
      subtracting the end pointer with the buffer position pointer (rpos).
      
      the comparison issue is easily eliminated by using != instead of <.
      however the subtractions require nontrivial changes:
      
      previously, f->shcnt stored the count that would have been read if
      consuming the whole buffer, which required an end pointer for the
      buffer. the purpose for this was that it allowed reading it and adding
      rpos-rend at any time to get the actual count so far, and required no
      adjustment at the time of __shgetc (actual function call) since the
      call would only happen when reaching the end of the buffer.
      
      to get rid of the dependency on rend, instead offset shcnt by buf-rpos
      (start of buffer) at the time of last __shlim/__shgetc call. this
      makes for slightly more work in __shgetc the function, but for the
      inline macro it's still just as easy to compute the current count.
      
      since the scan helper interfaces used here are a big hack, comments
      are added to document their contracts and what's going on with their
      implementations.
      d6c855ca
  2. 23 6月, 2013 1 次提交
    • R
      fix major scanf breakage with unbuffered streams, fmemopen, etc. · c2080450
      Rich Felker 提交于
      the shgetc api, used internally in scanf and int/float scanning code
      to handle field width limiting and pushback, was designed assuming
      that pushback could be achieved via a simple decrement on the file
      buffer pointer. this only worked by chance for regular FILE streams,
      due to the linux readv bug workaround in __stdio_read which moves the
      last requested byte through the buffer rather than directly back to
      the caller. for unbuffered streams and streams not using __stdio_read
      but some other underlying read function, the first character read
      could be completely lost, and replaced by whatever junk happened to be
      in the unget buffer.
      
      to fix this, simply have shgetc, when it performs an underlying read
      operation on the stream, store the character read at the -1 offset
      from the read buffer pointer. this is valid even for unbuffered
      streams, as they have an unget buffer located just below the start of
      the zero-length buffer. the check to avoid storing the character when
      it is already there is to handle the possibility of read-only buffers.
      no application-exposed FILE types are allowed to use read-only
      buffers, but sscanf and strto* may use them internally when calling
      functions which use the shgetc api.
      c2080450
  3. 17 4月, 2012 1 次提交
  4. 16 4月, 2012 1 次提交
  5. 11 4月, 2012 2 次提交
    • R
      7ef1a9bb
    • R
      add "scan helper getc" and rework strtod, etc. to use it · 2162541f
      Rich Felker 提交于
      the immediate benefit is a significant debloating of the float parsing
      code by moving the responsibility for keeping track of the number of
      characters read to a different module.
      
      by linking shgetc with the stdio buffer logic, counting logic is
      defered to buffer refill time, keeping the calls to shgetc fast and
      light.
      
      in the future, shgetc will also be useful for integrating the new
      float code with scanf, which needs to not only count the characters
      consumed, but also limit the number of characters read based on field
      width specifiers.
      
      shgetc may also become a useful tool for simplifying the integer
      parsing code.
      2162541f