Avoid pathological behavior in OpenBSD's fread.

Bug: https://code.google.com/p/android/issues/detail?id=81155
Bug: 18556607
Change-Id: Idc60976b79610e2202cc42dc393dcb4ca6c42e05
This commit is contained in:
Elliott Hughes
2014-12-01 16:13:30 -08:00
parent 5cd127d3aa
commit 20841a137b
3 changed files with 49 additions and 2 deletions

View File

@@ -813,3 +813,34 @@ TEST(stdio, freopen_CLOEXEC) {
fclose(fp);
}
// https://code.google.com/p/android/issues/detail?id=81155
// http://b/18556607
TEST(stdio, fread_unbuffered_pathological_performance) {
FILE* fp = fopen("/dev/zero", "r");
ASSERT_TRUE(fp != NULL);
// Make this stream unbuffered.
setvbuf(fp, 0, _IONBF, 0);
char buf[65*1024];
memset(buf, 0xff, sizeof(buf));
time_t t0 = time(NULL);
for (size_t i = 0; i < 1024; ++i) {
fread(buf, 64*1024, 1, fp);
}
time_t t1 = time(NULL);
fclose(fp);
// 1024 64KiB reads should have been very quick.
ASSERT_LE(t1 - t0, 1);
for (size_t i = 0; i < 64*1024; ++i) {
ASSERT_EQ('\0', buf[i]);
}
for (size_t i = 64*1024; i < 65*1024; ++i) {
ASSERT_EQ('\xff', buf[i]);
}
}