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

Michael Jones via Phabricator via libc-commits libc-commits at lists.llvm.org
Wed Nov 2 14:14:49 PDT 2022


michaelrj added inline comments.


================
Comment at: libc/src/__support/File/file.cpp:265-266
+    // |pos|.
+    --pos;
+    bufref[pos] = static_cast<unsigned char>(c);
+  }
----------------
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.


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