Skip to content

Commit 89e02be

Browse files
committed
Major issue:
- Fix the uncrustify check (removed https reference to Microsoft). New changes: - Added missing string related to new check - Added checker description for ftellTextModeFile - Updated copyright.
1 parent 1876e70 commit 89e02be

4 files changed

Lines changed: 55 additions & 2 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
*.gcno
33
*.gch
44
*.o
5+
*.a
56
*.pyc
67
/cppcheck
78
/cppcheck.exe

lib/checkio.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,6 @@ void CheckIO::checkFileUsage()
352352
case Filepointer::Operation::UNIMPORTANT:
353353
if (f.mode == OpenMode::CLOSED)
354354
useClosedFileError(tok);
355-
// See https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/fopen-wfopen?view=msvc-170
356355
if (isftell && f.read_mode == Filepointer::ReadMode::READ_TEXT && printPortability)
357356
ftellFileError(tok);
358357
break;

lib/checkio.h

Lines changed: 2 additions & 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-2025 Cppcheck team.
3+
* Copyright (C) 2007-2026 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
@@ -141,6 +141,7 @@ class CPPCHECKLIB CheckIO : public Check {
141141
"- Missing or wrong width specifiers in 'scanf' format string\n"
142142
"- Use a file that has been closed\n"
143143
"- File input/output without positioning results in undefined behaviour\n"
144+
"- Using 'ftell' on a file opened in text mode\n"
144145
"- Read to a file that has only been opened for writing (or vice versa)\n"
145146
"- Repositioning operation on a file opened in append mode\n"
146147
"- The same file can't be open for read and write at the same time on different streams\n"

man/checkers/ftellTextModeFile.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# ftellModeTextFile
2+
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/>
4+
**Category**: Portability<br/>
5+
**Severity**: Style<br/>
6+
**Language**: C/C++
7+
8+
## Description
9+
10+
This checker detects the use of ftell() on a file open in text (or translate) mode. The text mode is not consistent
11+
in between Linux and Windows system and may cause ftell() to return the wrong offset inside a text file.
12+
13+
This warning helps improve code quality by:
14+
- Making the intent clear that the use of ftell() in "t" mode may cause portability problem.
15+
16+
## Motivation
17+
18+
This checker improves portability accross system.
19+
20+
## How to fix
21+
22+
According to C11, the file must be opened in binary mode 'b' to prevent this problem.
23+
24+
Before:
25+
```cpp
26+
FILE *f = fopen("Example.txt", "rt");
27+
if (f)
28+
{
29+
fseek(f, 0, SEEK_END);
30+
printf( "Offset %d\n", ftell(f);
31+
fclose(f);
32+
}
33+
34+
```
35+
36+
After:
37+
```cpp
38+
39+
FILE *f = fopen("Example.txt", "rb");
40+
if (f)
41+
{
42+
fseek(f, 0, SEEK_END);
43+
printf( "Offset %d\n", ftell(f);
44+
fclose(f);
45+
}
46+
47+
```
48+
49+
## Notes
50+
51+
See https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/fopen-wfopen?view=msvc-170
52+

0 commit comments

Comments
 (0)