ftell() not consistent in between Linux/Unix and Windows when file is opened in mode "t"#8360
ftell() not consistent in between Linux/Unix and Windows when file is opened in mode "t"#8360damorinCan wants to merge 4 commits into
Conversation
|
Thanks for your contribution. |
|
Title now including reference to C11 and removed reference to Windows 11.
I added a test in test/testio.cpp. Anything missing in it ? |
Thanks, I must have missed that. |
|
Ping @danmar |
danmar
left a comment
There was a problem hiding this comment.
I feel we need to have a ticket for this in trac
it feels like it should be added to the release notes as a "new check" ?
| case Filepointer::Operation::UNIMPORTANT: | ||
| if (f.mode == OpenMode::CLOSED) | ||
| useClosedFileError(tok); | ||
| if (isftell && windows && f.read_mode == Filepointer::ReadMode::READ_TEXT && printPortability) |
There was a problem hiding this comment.
the error message says something about the C11 standard. Nothing about windows.
There was a problem hiding this comment.
I will add a comment about this reference to this Microsoft page:
https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/ftell-ftelli64?view=msvc-170
An update for the error message with this, would it be OK ?
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.".
I will remove the reference to _wfopen(), it's not part of the C++ standard.
There was a problem hiding this comment.
Sorry.. my confusion was because you wrote && windows &&.
Now that I understand the issue better I think it's better to be clear that it's a standard problem.
|
I have tried to add some descriptions about some of our checkers here: Ideally that would contain descriptions for all our checkers but due to lack-of-resources it does not. If you can add a corresponding file for this checking that would be great. |
- 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.
|
|
We need a ticket in trac for this. Can you please tell me what you think the summary and description should be and we can create it.. |
| @@ -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/> | |||
There was a problem hiding this comment.
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.
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, ...
| /* -*- C++ -*- | ||
| * Cppcheck - A tool for static C/C++ code analysis | ||
| * Copyright (C) 2007-2025 Cppcheck team. | ||
| * Copyright (C) 2007-2026 Cppcheck team. |
There was a problem hiding this comment.
this diff is not needed. we don't tweak it manually.
| " if (f)\n" | ||
| " {\n" | ||
| " fseek(f, 0, SEEK_END);\n" | ||
| " (void)ftell(f);\n" |
There was a problem hiding this comment.
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 pos = ftell(f); would make it more dangerous.
| if (f) | ||
| { | ||
| fseek(f, 0, SEEK_END); | ||
| printf( "Offset %d\n", ftell(f); |
There was a problem hiding this comment.
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..
Thanks, I lost my login/password (last time was years ago). What about this: [ftell() is no more returning the correct offset on a text file with Windows 11 compared to Linux. |
do you remember your username? If you send me a new htpasswd hash via email I can update.. |
|
The PR title should be something like: Fix #14743 (ftell() not consistent in C11) I would like that the PR title and ticket title are consistent. and well imho the fundamental issue is not that linux/windows does not have the same behavior. the fundamental issue is that the behavior is implementation defined. it's possible you can have different behavior when using the same compiler but different volume type, compiler flags, etc.. |
|
Hi,
I tried to edit the ticket title but it's not possible so I made the PR
title the same as the ticket.
Robert
Le lun. 11 mai 2026 à 05:33, Daniel Marjamäki ***@***.***> a
écrit :
… *danmar* left a comment (cppcheck-opensource/cppcheck#8360)
<#8360 (comment)>
The PR title should be something like:
Fix #14743 (ftell() not consistent in C11)
I would like that the PR title and ticket title are consistent.
—
Reply to this email directly, view it on GitHub
<#8360 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AB2AXDMB3H3KXMOCKNN3DTD42GM7ZAVCNFSM6AAAAACW2EKWFWVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHM2DIMJZGMYDOMRVHE>
.
Triage notifications on the go with GitHub Mobile for iOS
<https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675>
or Android
<https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub>.
You are receiving this because you authored the thread.Message ID:
***@***.***>
|



Some legacy tools stopped suddenly working after 20+ years.
https://stackoverflow.com/questions/79762122/ftell-no-more-returning-the-correct-offset-on-a-text-file-with-windows-11-ente
Trac ticket: https://trac.cppcheck.net/ticket/14743#ticket