Skip to content

Commit 0873197

Browse files
authored
Fix #14704: False positive: IOWithoutPositioning reported for user functions fread and fwrite (#8726)
1 parent ae6a77d commit 0873197

2 files changed

Lines changed: 19 additions & 0 deletions

File tree

lib/checkio.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,8 @@ void CheckIOImpl::checkFileUsage()
155155
tok = tok->linkAt(1);
156156
continue;
157157
}
158+
if (tok->function() && tok->function()->nestedIn)
159+
continue;
158160
if (tok->str() == "{")
159161
indent++;
160162
else if (tok->str() == "}") {

test/testio.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -694,6 +694,23 @@ class TestIO : public TestFixture {
694694
" fwrite(X::data(), sizeof(char), buffer.size(), d->file);\n"
695695
"}");
696696
ASSERT_EQUALS("[test.cpp:9:5]: (error) Read and write operations without a call to a positioning function (fseek, fsetpos or rewind) or fflush in between result in undefined behaviour. [IOWithoutPositioning]\n", errout_str());
697+
698+
check("struct MemStream {\n"
699+
" char buf[1024];\n"
700+
" int pos = 0;\n"
701+
" void fwrite(const void *ptr, size_t n) { memcpy(buf + pos, ptr, n); pos += (int)n; }\n"
702+
"};\n"
703+
"struct FileStream {\n"
704+
" FILE *fp;\n"
705+
" size_t _fread(void *ptr, size_t n) { return ::fread(ptr, 1, n, fp); }\n"
706+
" size_t fread(void *ptr, size_t n) { return _fread(ptr, n); }\n"
707+
" void copy_to(MemStream *dst, size_t n) {\n"
708+
" char tmp[256];\n"
709+
" fread(tmp, n);\n"
710+
" dst->fwrite(tmp, n);\n"
711+
" }\n"
712+
"};\n");
713+
ASSERT_EQUALS("", errout_str());
697714
}
698715

699716
void seekOnAppendedFile() {

0 commit comments

Comments
 (0)