[libc-commits] [PATCH] D137286: [libc] Add implementation of ungetc.

Siva Chandra via Phabricator via libc-commits libc-commits at lists.llvm.org
Wed Nov 2 15:44:56 PDT 2022


sivachandra added inline comments.


================
Comment at: libc/src/__support/File/file.cpp:265-266
+    // |pos|.
+    --pos;
+    bufref[pos] = static_cast<unsigned char>(c);
+  }
----------------
michaelrj wrote:
> sivachandra wrote:
> > michaelrj wrote:
> > > this could have issues in the case where `ungetc` is called twice in a row. If `pos` is 1 and ungetc is called, then `pos` is set to 0 and the char is written to `bufref[pos]`, but on the second call `pos` is already 0 so subtracting will cause an underflow and likely a segfault.
> > I have added a conditional now. 
> this is okay for now (since it guarantees a buffer size of at least 1) but in future I'd like to look into a way to increase the size of the pushback buffer.
The standards require a minimum unget buffer of 1 character. A program relying on anything else would be a relying on internals so I do not think trying to build larger push back buffer is required or worth the complication.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D137286/new/

https://reviews.llvm.org/D137286



More information about the libc-commits mailing list