tech-userlevel archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

No reads (0 bytes read) when using `std::ios_base::sync_with_stdio(false)'



Hello folks,
a couple of days ago when trying to use git-crypt I have noticed that
it was not able to `std::cin.read()' (i.e. read 0 bytes) because a bit
earlier `std::ios_base::sync_with_stdio(false)' was called (I have
tested on a NetBSD/amd64 -current some weeks old):

Here a minimal reproducer:

```
#include <iostream>

int
main(int argc, char *argv[])
{
	char buffer[64];

#ifndef SYNC_WITH_STDIO
	std::ios_base::sync_with_stdio(false);
#endif
	std::cin.read(buffer, sizeof(buffer));
	std::cerr << "Length: " << std::cin.gcount() << std::endl;

        return 0;
}
```

When `std::ios_base::sync_with_stdio(false)' is called the program
suddendly exit without reading anything:

```
% ./a.out
Length: 0
```

If it is not called (i.e. the reproducer is built via
`-DSYNC_WITH_STDIO') the program does what I would expect (and what
`git-crypt' authors probably expected):

```
% ./a.out
foo bar baz
Length: 12
```

I'm completely unfamiliar with C++ but it seems that calling
`std::ios_base::sync_with_stdio(false)' leads to problems if C and C++
I/O are mixed and I think that calling it after I/O has occurred it is
an undefined behaviour (but also in git-crypt it is called very early
before any I/O happened).


Anyone know why that happens?


Thanks!


Home | Main Index | Thread Index | Old Index