public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug libstdc++/64064] New: basic_filebuf seekoff return value is unusable for files opened in text mode on Windows
@ 2014-11-25  0:50 lukeallardyce at gmail dot com
  2024-01-19  7:51 ` [Bug libstdc++/64064] " lh_mouse at 126 dot com
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: lukeallardyce at gmail dot com @ 2014-11-25  0:50 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=64064

            Bug ID: 64064
           Summary: basic_filebuf seekoff return value is unusable for
                    files opened in text mode on Windows
           Product: gcc
           Version: 4.9.2
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: lukeallardyce at gmail dot com

Discussion here

https://gcc.gnu.org/ml/libstdc++/2014-11/msg00145.html

#include <fstream>
#include <iostream>

int main(int, char* argv[])
{
  using traits   = std::filebuf::traits_type;
  using int_type = std::filebuf::int_type;

  std::filebuf fb;
  fb.open(argv[1], std::ios::in);

  while (!traits::eq_int_type(fb.sbumpc(), traits::eof()))
    std::cout << fb.pubseekoff(0, std::ios::cur, std::ios::in) << ' ';

  std::cout << '\n';

  fb.close();
  fb.pubsetbuf(nullptr, 0);
  fb.open(argv[1], std::ios::in);

  while (!traits::eq_int_type(fb.sbumpc(), traits::eof()))
    std::cout << fb.pubseekoff(0, std::ios::cur, std::ios::in) << ' ';
}

With the following 3-line Windows-style text file:
hello
world

Produces the following:
4 5 6 7 8 9 10 11 12 13 14 15 16
1 2 3 4 5 7 8 9 10 11 12 14 16

On a buffered stream, the value returned by pubseekoff cannot be used to seek
back to that point due to the way Windows implements the POSIX read and lseek64
functions (i.e. read performs end of line conversion, lseek64 doesn't). The
value is off by one for each unconsumed end of line in the buffer.

It is still unclear whether this should be fixed, if possible, by libstdc++ or
mingw-w64.


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [Bug libstdc++/64064] basic_filebuf seekoff return value is unusable for files opened in text mode on Windows
  2014-11-25  0:50 [Bug libstdc++/64064] New: basic_filebuf seekoff return value is unusable for files opened in text mode on Windows lukeallardyce at gmail dot com
@ 2024-01-19  7:51 ` lh_mouse at 126 dot com
  2024-01-19  8:18 ` lh_mouse at 126 dot com
  2024-02-12 10:12 ` redi at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: lh_mouse at 126 dot com @ 2024-01-19  7:51 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=64064

LIU Hao <lh_mouse at 126 dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |lh_mouse at 126 dot com

--- Comment #1 from LIU Hao <lh_mouse at 126 dot com> ---
AFAICT the only issue here is that one can't pass the result of `pubseekoff()`
back to `pubseekpos()`. Both MSVCRT GCC and UCRT GCC are affected, but MSVC
isn't.


```
#include <fstream>
#include <iostream>

int main(int, char* argv[])
{
  using traits   = std::filebuf::traits_type;
  using int_type = std::filebuf::int_type;

  std::filebuf fb;
  fb.open(argv[1], std::ios::in);

  int_type c;
  while (!traits::eq_int_type(c = fb.sbumpc(), traits::eof()))
  {
    std::cout << static_cast<char>(c);
    auto pos = fb.pubseekoff(0, std::ios::cur, std::ios::in);
    fb.pubseekpos(pos, std::ios::in);
  }

  std::cout << '\n';
}
```

This outputs
```
hl
ol
```

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [Bug libstdc++/64064] basic_filebuf seekoff return value is unusable for files opened in text mode on Windows
  2014-11-25  0:50 [Bug libstdc++/64064] New: basic_filebuf seekoff return value is unusable for files opened in text mode on Windows lukeallardyce at gmail dot com
  2024-01-19  7:51 ` [Bug libstdc++/64064] " lh_mouse at 126 dot com
@ 2024-01-19  8:18 ` lh_mouse at 126 dot com
  2024-02-12 10:12 ` redi at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: lh_mouse at 126 dot com @ 2024-01-19  8:18 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=64064

--- Comment #2 from LIU Hao <lh_mouse at 126 dot com> ---
It looks that MS STL calls `_fseeki64(fp, ...)` [1], while libstdc++ calls
`lseek(_fileno(fp), ...)` [2].

The seek operation shall take the buffering of `FILE` struct into account,
hence I think the libstdc++ implementation is incorrect.


[1]
https://github.com/microsoft/STL/blob/442029c6fa37f1b6f9203357de09672d5704077c/stl/inc/__msvc_filebuf.hpp#L624
[2]
https://github.com/gcc-mirror/gcc/blob/9693459e030977d6e906ea7eb587ed09ee4fddbd/libstdc%2B%2B-v3/config/io/basic_file_stdio.cc#L425

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [Bug libstdc++/64064] basic_filebuf seekoff return value is unusable for files opened in text mode on Windows
  2014-11-25  0:50 [Bug libstdc++/64064] New: basic_filebuf seekoff return value is unusable for files opened in text mode on Windows lukeallardyce at gmail dot com
  2024-01-19  7:51 ` [Bug libstdc++/64064] " lh_mouse at 126 dot com
  2024-01-19  8:18 ` lh_mouse at 126 dot com
@ 2024-02-12 10:12 ` redi at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: redi at gcc dot gnu.org @ 2024-02-12 10:12 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=64064

--- Comment #3 from Jonathan Wakely <redi at gcc dot gnu.org> ---
https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/fseek-fseeki64?view=msvc-170#remarks

The only way to use _fseeki64 for text mode files is with __off=0 (and any
value of __way) or __way=beg and __off obtained from _ftelli64.

So I think for __way=cur or __way=end we would need to calculate a file offset
relative to the start of the file and then use that.

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2024-02-12 10:12 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-11-25  0:50 [Bug libstdc++/64064] New: basic_filebuf seekoff return value is unusable for files opened in text mode on Windows lukeallardyce at gmail dot com
2024-01-19  7:51 ` [Bug libstdc++/64064] " lh_mouse at 126 dot com
2024-01-19  8:18 ` lh_mouse at 126 dot com
2024-02-12 10:12 ` redi at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).