This is an archive of the discontinued LLVM Phabricator instance.

[libc] Add implementation of ungetc.
ClosedPublic

Authored by sivachandra on Nov 2 2022, 1:23 PM.

Details

Summary

A bug in the file read logic has also been fixed along the way. Parts
of the ungetc tests will fail without that bug fixed.

Diff Detail

Event Timeline

sivachandra created this revision.Nov 2 2022, 1:23 PM
Herald added projects: Restricted Project, Restricted Project. · View Herald TranscriptNov 2 2022, 1:23 PM
sivachandra requested review of this revision.Nov 2 2022, 1:23 PM
michaelrj added inline comments.Nov 2 2022, 2:14 PM
libc/src/__support/File/file.cpp
265–266

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.

Address comments.

libc/src/__support/File/file.cpp
265–266

I have added a conditional now.

michaelrj accepted this revision.Nov 2 2022, 3:17 PM
michaelrj added inline comments.
libc/src/__support/File/file.cpp
265–266

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.

This revision is now accepted and ready to land.Nov 2 2022, 3:17 PM
sivachandra added inline comments.Nov 2 2022, 3:44 PM
libc/src/__support/File/file.cpp
265–266

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.

This revision was automatically updated to reflect the committed changes.