[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 14:42:47 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:
> 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. 


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