Merge "Fix a sign extension bug in stdio."

am: 172420e6a2

* commit '172420e6a2c5af527984a3a3a51f60a48b06ae9a':
  Fix a sign extension bug in stdio.
This commit is contained in:
Elliott Hughes 2016-01-27 02:42:09 +00:00 committed by android-build-merger
commit 0fb1e6bdac
2 changed files with 17 additions and 7 deletions

View file

@ -426,7 +426,12 @@ static off64_t __seek_unlocked(FILE* fp, off64_t offset, int whence) {
if (_EXT(fp)->_seek64 != nullptr) {
return (*_EXT(fp)->_seek64)(fp->_cookie, offset, whence);
} else if (fp->_seek != nullptr) {
return (*fp->_seek)(fp->_cookie, offset, whence);
off64_t result = (*fp->_seek)(fp->_cookie, offset, whence);
#if !defined(__LP64__)
// Avoid sign extension if off64_t is larger than off_t.
if (result != -1) result &= 0xffffffff;
#endif
return result;
} else {
errno = ESPIPE;
return -1;
@ -531,12 +536,12 @@ off64_t ftello64(FILE* fp) {
int fgetpos(FILE* fp, fpos_t* pos) {
*pos = ftello(fp);
return (*pos == -1);
return (*pos == -1) ? -1 : 0;
}
int fgetpos64(FILE* fp, fpos64_t* pos) {
*pos = ftello64(fp);
return (*pos == -1);
return (*pos == -1) ? -1 : 0;
}
static FILE* __funopen(const void* cookie,

View file

@ -1116,14 +1116,19 @@ TEST(STDIO_TEST, funopen_seek) {
FILE* fp = funopen(nullptr, read_fn, nullptr, seek_fn, nullptr);
ASSERT_TRUE(fp != nullptr);
fpos_t pos;
ASSERT_EQ(0, fgetpos(fp, &pos));
ASSERT_EQ(0xfedcba12LL, pos);
#if defined(__LP64__)
EXPECT_EQ(0, fgetpos(fp, &pos)) << strerror(errno);
EXPECT_EQ(0xfedcba12LL, pos);
#else
EXPECT_EQ(-1, fgetpos(fp, &pos)) << strerror(errno);
EXPECT_EQ(EOVERFLOW, errno);
#endif
FILE* fp64 = funopen64(nullptr, read_fn, nullptr, seek64_fn, nullptr);
ASSERT_TRUE(fp64 != nullptr);
fpos64_t pos64;
ASSERT_EQ(0, fgetpos64(fp64, &pos64));
ASSERT_EQ(0xfedcba12345678, pos64);
EXPECT_EQ(0, fgetpos64(fp64, &pos64)) << strerror(errno);
EXPECT_EQ(0xfedcba12345678, pos64);
#else
GTEST_LOG_(INFO) << "glibc uses fopencookie instead.\n";
#endif