-
Notifications
You must be signed in to change notification settings - Fork 13.3k
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
Fix SD info64 : info.usedBytes calculation giving weird result #8445
Conversation
@@ -101,7 +101,7 @@ class SDFSImpl : public FSImpl | |||
info.pageSize = 0; // TODO ? | |||
info.maxPathLength = 255; // TODO ? | |||
info.totalBytes =_fs.vol()->clusterCount() * info.blockSize; | |||
info.usedBytes = info.totalBytes - (_fs.vol()->freeClusterCount() * _fs.vol()->bytesPerCluster()); | |||
info.usedBytes = (_fs.vol()->clusterCount() - _fs.vol()->freeClusterCount()) * info.blockSize; |
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.
When I manually factor things I think this change is mathematically the same as the existing code.
info.usedBytes = info.totalBytes - (_fs.vol()->freeClusterCount() * _fs.vol()->bytesPerCluster());
info.totalBytes =_fs.vol()->clusterCount() * info.blockSize;
info.blockSize = _fs.vol()->bytesPerCluster();
So,
info.usedBytes = (_fs.vol()->clusterCount() * _fs.vol()->bytesPerCluster()) - (_fs.vol()->freeClusterCount() * _fs.vol()->bytesPerCluster());
Factor out the bpc and you get
info.usedBytes = (_fs.vol()->clusterCount() - _fs.vol()->freeClusterCount() ) * _fs.vol()->bytesPerCluster();
which is the patch's rewrite, no?
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.
I do agree about calculation , but the fact is the current does not give the correct result.
Seems overflow happen.
Could be a type issue when doing calculation ?
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.
with figures it is may be more clear:
#include <SPI.h>
#include <SD.h>
#include <SDFS.h>
void setup(){
Serial.begin(115200);
if(!SD.begin(5)){
Serial.println("Card Mount Failed");
return;
}
FSInfo64 info;
if (!SDFS.info64(info)) {
Serial.println("Size unkown");
return ;
}
Serial.printf("\nTotal space: %lluMB\n", info.totalBytes / (1024 * 1024));
Serial.printf("Used space: %lluMB\n", info.usedBytes / (1024 * 1024));
}
void loop(){}
Without PR
Total space: 3760MB
Used space: 4929MB =>Wrong
With PR
Total space: 3760MB
Used space: 833MB =>Correct
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.
Thanks for giving the example. You're probably right, there is some type promotion issue and 32b overflow in the existing code.
Maybe we would want explicit casts to u64? There are mixed i32, u16 and u32, while we expect the result to be u64-wide and not u32 cast into one in the end |
No description provided.