Skip to content

Commit c103823

Browse files
committed
Applied latest comment from danmar.
1 parent 89e02be commit c103823

4 files changed

Lines changed: 19 additions & 7 deletions

File tree

lib/checkio.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,13 @@ void CheckIO::seekOnAppendedFileError(const Token *tok)
415415
void CheckIO::ftellFileError(const Token *tok)
416416
{
417417
reportError(tok, Severity::portability,
418-
"ftellTextModeFile", "According to Microsoft, the value returned by ftell may not reflect the physical byte offset for streams opened in text mode, because text mode causes carriage return-line feed translation. See also 7.21.9.4 in C11 standard.", CWE474, Certainty::normal);
418+
"ftellTextModeFile", "The ftell function obtains the current value of the file position indicator"
419+
" for the stream pointed to by stream. For a binary stream, the value is the number of characters"
420+
" from the beginning of the file. For a text stream, its file position indicator contains unspecified"
421+
" information, usable by the fseek function for returning the file position indicator for the stream"
422+
" to its position at the time of the ftell call; the difference between two such return values is"
423+
" not necessarily a meaningful measure of the number of characters written or read. See 7.21.9.4"
424+
" in C11 standard.", CWE474, Certainty::normal);
419425
}
420426

421427
void CheckIO::incompatibleFileOpenError(const Token *tok, const std::string &filename)

lib/checkio.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* -*- C++ -*-
22
* Cppcheck - A tool for static C/C++ code analysis
3-
* Copyright (C) 2007-2026 Cppcheck team.
3+
* Copyright (C) 2007-2025 Cppcheck team.
44
*
55
* This program is free software: you can redistribute it and/or modify
66
* it under the terms of the GNU General Public License as published by

man/checkers/ftellTextModeFile.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# ftellModeTextFile
22

3-
**Message**: According to Microsoft, the value returned by ftell may not reflect the physical byte offset for streams opened in text mode, because text mode causes carriage return-line feed translation. See also 7.21.9.4 in C11 standard.<br/>
3+
**Message**: The ftell function obtains the current value of the file position indicator for the stream pointed to by stream. For a binary stream, the value is the number of characters from the beginning of the file. For a text stream, its file position indicator contains unspecified information, usable by the fseek function for returning the file position indicator for the stream to its position at the time of the ftell call; the difference between two such return values is not necessarily a meaningful measure of the number of characters written or read. See a7.21.9.4 in C11 standard.<br/>
44
**Category**: Portability<br/>
55
**Severity**: Style<br/>
66
**Language**: C/C++
@@ -26,8 +26,13 @@ Before:
2626
FILE *f = fopen("Example.txt", "rt");
2727
if (f)
2828
{
29-
fseek(f, 0, SEEK_END);
30-
printf( "Offset %d\n", ftell(f);
29+
int position;
30+
struct stat st;
31+
32+
position = fseek(f, 0, SEEK_END);
33+
fstat(f, &st);
34+
printf( "Position %d\n", ftell(f);
35+
printf( "File size %d\n, st.st_size);
3136
fclose(f);
3237
}
3338

test/testio.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -711,12 +711,13 @@ class TestIO : public TestFixture {
711711
" FILE *f = fopen(\"\", \"rt\");\n"
712712
" if (f)\n"
713713
" {\n"
714+
" extern long position;\n"
714715
" fseek(f, 0, SEEK_END);\n"
715-
" (void)ftell(f);\n"
716+
" position = ftell(f);\n"
716717
" fclose(f);\n"
717718
" }\n"
718719
"}\n", dinit(CheckOptions, $.portability = true));
719-
ASSERT_EQUALS("[test.cpp:6:16]: (portability) According to Microsoft, the value returned by ftell may not reflect the physical byte offset for streams opened in text mode, because text mode causes carriage return-line feed translation. See also 7.21.9.4 in C11 standard. [ftellTextModeFile]\n", errout_str());
720+
ASSERT_EQUALS("[test.cpp:7:21]: (portability) The ftell function obtains the current value of the file position indicator for the stream pointed to by stream. For a binary stream, the value is the number of characters from the beginning of the file. For a text stream, its file position indicator contains unspecified information, usable by the fseek function for returning the file position indicator for the stream to its position at the time of the ftell call; the difference between two such return values is not necessarily a meaningful measure of the number of characters written or read. See 7.21.9.4 in C11 standard. [ftellTextModeFile]\n", errout_str());
720721
}
721722

722723

0 commit comments

Comments
 (0)