-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Fix #14743 (New check: ftell() result is unspecified when file is opened in mode "t") #8360
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ | |
| *.gcno | ||
| *.gch | ||
| *.o | ||
| *.a | ||
| *.pyc | ||
| /cppcheck | ||
| /cppcheck.exe | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,57 @@ | ||||||||||||||
| # ftellModeTextFile | ||||||||||||||
|
|
||||||||||||||
| **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/> | ||||||||||||||
| **Category**: Portability<br/> | ||||||||||||||
| **Severity**: Style<br/> | ||||||||||||||
| **Language**: C/C++ | ||||||||||||||
|
|
||||||||||||||
| ## Description | ||||||||||||||
|
|
||||||||||||||
| This checker detects the use of ftell() on a file open in text (or translate) mode. The text mode is not consistent | ||||||||||||||
| in between Linux and Windows system and may cause ftell() to return the wrong offset inside a text file. | ||||||||||||||
|
|
||||||||||||||
| This warning helps improve code quality by: | ||||||||||||||
| - Making the intent clear that the use of ftell() in "t" mode may cause portability problem. | ||||||||||||||
|
|
||||||||||||||
| ## Motivation | ||||||||||||||
|
|
||||||||||||||
| This checker improves portability accross system. | ||||||||||||||
|
|
||||||||||||||
| ## How to fix | ||||||||||||||
|
|
||||||||||||||
| According to C11, the file must be opened in binary mode 'b' to prevent this problem. | ||||||||||||||
|
|
||||||||||||||
| Before: | ||||||||||||||
| ```cpp | ||||||||||||||
| FILE *f = fopen("Example.txt", "rt"); | ||||||||||||||
| if (f) | ||||||||||||||
| { | ||||||||||||||
| int position; | ||||||||||||||
| struct stat st; | ||||||||||||||
|
|
||||||||||||||
| position = fseek(f, 0, SEEK_END); | ||||||||||||||
| fstat(f, &st); | ||||||||||||||
| printf( "Position %d\n", ftell(f); | ||||||||||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I want to have a buggy code that shows why we should warn. There should be a mistake. It seems clear to me that the developer of this code understands that the output of ftell() is a position and that it will not necessarily be the size. |
||||||||||||||
| printf( "File size %d\n, st.st_size); | ||||||||||||||
| fclose(f); | ||||||||||||||
| } | ||||||||||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suggest such bad code:
Suggested change
|
||||||||||||||
|
|
||||||||||||||
| ``` | ||||||||||||||
|
|
||||||||||||||
| After: | ||||||||||||||
| ```cpp | ||||||||||||||
|
|
||||||||||||||
| FILE *f = fopen("Example.txt", "rb"); | ||||||||||||||
| if (f) | ||||||||||||||
| { | ||||||||||||||
| fseek(f, 0, SEEK_END); | ||||||||||||||
| printf( "Offset %d\n", ftell(f); | ||||||||||||||
| fclose(f); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| ``` | ||||||||||||||
|
|
||||||||||||||
| ## Notes | ||||||||||||||
|
|
||||||||||||||
| See https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/fopen-wfopen?view=msvc-170 | ||||||||||||||
|
|
||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sorry but I want to change...
This is not a good warning message. How about:
I want that the standard text is quoted in the ftellTextModeFile.md file.