-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Fix #14743 (ftell() not consistent 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
Open
damorinCan
wants to merge
4
commits into
cppcheck-opensource:main
Choose a base branch
from
damorinCan:CWE474_ftell
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+103
−0
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ | |
| *.gcno | ||
| *.gch | ||
| *.o | ||
| *.a | ||
| *.pyc | ||
| /cppcheck | ||
| /cppcheck.exe | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| printf( "File size %d\n, st.st_size); | ||
| fclose(f); | ||
| } | ||
|
|
||
| ``` | ||
|
|
||
| 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 | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.