-
Notifications
You must be signed in to change notification settings - Fork 1.6k
ftell() not consistent in C11 #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,52 @@ | ||
| # ftellModeTextFile | ||
|
|
||
| **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/> | ||
|
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. It's not according to Microsoft. It's according to the C standard. Sorry I was confused before. I think it would make sense to quote the text from the C standard in the description in this document and highlight the part that says its file position indicator contains unspecified information.
|
||
| **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) | ||
| { | ||
| fseek(f, 0, SEEK_END); | ||
| printf( "Offset %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. Could we make the example code more "buggy"? If the program writes the offset for debugging purposes this output might be 100% fine. If the program writes "File size: %d\n" instead it would be a bit more clear it's a bug imho. If you have better suggestions to make it more "buggy" feel free to do it.. |
||
| 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 | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -44,6 +44,7 @@ class TestIO : public TestFixture { | |
| TEST_CASE(fileIOwithoutPositioning); | ||
| TEST_CASE(seekOnAppendedFile); | ||
| TEST_CASE(fflushOnInputStream); | ||
| TEST_CASE(ftellCompatibility); | ||
| TEST_CASE(incompatibleFileOpen); | ||
|
|
||
| TEST_CASE(testScanf1); // Scanf without field limiters | ||
|
|
@@ -704,6 +705,21 @@ class TestIO : public TestFixture { | |
| ASSERT_EQUALS("", errout_str()); // #6566 | ||
| } | ||
|
|
||
| void ftellCompatibility() { | ||
|
|
||
| check("void foo() {\n" | ||
| " FILE *f = fopen(\"\", \"rt\");\n" | ||
| " if (f)\n" | ||
| " {\n" | ||
| " fseek(f, 0, SEEK_END);\n" | ||
| " (void)ftell(f);\n" | ||
|
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 would think a warning here is a false positive. We can clearly see that nothing unexpected happens. The program will do what the developer expects. An assignment of an unknown or global variable |
||
| " fclose(f);\n" | ||
| " }\n" | ||
| "}\n", dinit(CheckOptions, $.portability = true)); | ||
| 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()); | ||
| } | ||
|
|
||
|
|
||
| void fflushOnInputStream() { | ||
| check("void foo()\n" | ||
| "{\n" | ||
|
|
||
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.
this diff is not needed. we don't tweak it manually.