ungetc / ungetwc
and file position after discarding push back problemsThis issue has been automatically converted from the original issue lists and some formatting may not have been preserved.
Authors: Austin Group, Nick Stoughton (US)
Date: 2013-06-19
Reference document: N1720, Austin Group Defect #701
Submitted against: C11 / C17
Status: Fixed
Fixed in: C17
Converted from: n2396.htm
C11 (and C99 before it) state for both ungetc() and ungetwc() that
A successful intervening call (with the stream pointed to by stream) to a file positioning function (fseek, fsetpos, or rewind) discards any pushed-back characters for the stream. ... The value of the file position indicator for the stream after reading or discarding all pushed-back characters shall be the same as it was before the characters were pushed back.
(7.21.7.10 p2 & p5, with similar at 7.29.3.10 p2 & p5). The "or discarding" phrasing in p5 makes no sense: all of the listed functions which discard the push back also _set_ the file position. The file position will end up as whatever the function sets it to, not "the same as it was before the characters were pushed back".
Change
The value of the file position indicator for the stream after reading or discarding all pushed-back characters shall be the same as it was before the characters were pushed back.
to
The value of the file position indicator for the stream after all pushed-back characters have been read shall be the same as it was before the characters were pushed back.
Comment from WG14 on 2017-11-03:
Oct 2013 meeting
Apr 2014 meeting
The Standard is correct as written because the intent is that the specified file position indicator is an intermediate state inside the file positioning function after the pushed-back characters are discarded but before the actual seek. That gives you a reliable file position from which to do the seek. It's not intended that the file positioning function doesn't set the file position indicator.
Add a footnote to 7.21.7.10 paragraph 5, second sentence:
Note that a file positioning function may further modify the file position indicator after discarding any pushed-back characters.
Add a footnote to 7.29.3.10 paragraph 5, second sentence:
Note that a file positioning function may further modify the file position indicator after discarding any pushed-back wide characters.