llvm / llvm/llvm-project

[libc++] `fstream`'s `xsputn()` always skips the `streambuf` on the first insert to the stream

Open
#189,349 1 comment 0 reactions 0 assignees View on GitHub
libc++ performance
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

Here is an example comparing clang trunk (with libc++) to libstdc++: https://godbolt.org/z/9b9zff3Me

In the older version, both words 'test' are written to the buffer. However, in trunk only the second 'test' is written.

We suspect that this can be caused by the new condition in `fstream`'s [`xsputn()`](https://github.com/llvm/llvm-project/blob/a81b7dead6bd0cbd9b63ec93ee6e22fbc034be9f/libcxx/include/fstream#L301) implementation, since it appears during debugging that `fwrite()` is being called. This would only be possible if the following condition is true: `if (__always_noconv_ && __len >= (this->epptr() - this->pbase()))`. However, this shouldn't happen, as the length of the written string (4 characters) should easily fit in the buffer's put area.

We speculate that this is only possible if the put area is not initialized before the first stream insert and call to `xsputn()`, meaning both pointers are null. When the insert is called a second time, the pointers are initialized by then, and the correct path is taken.

This issue was raised in discussion at https://github.com/llvm/llvm-project/issues/180495

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.