Skip to content
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

FS::read returning negative number on type size_t #7814

Closed
fcapano opened this issue Jan 6, 2021 · 7 comments · Fixed by #7817
Closed

FS::read returning negative number on type size_t #7814

fcapano opened this issue Jan 6, 2021 · 7 comments · Fixed by #7817

Comments

@fcapano
Copy link

fcapano commented Jan 6, 2021

in FS.cpp, function File::read should return an unsigned int (aka size_t), however it returns a negative value (-1).

return -1;

@igrr

@themindfactory
Copy link
Contributor

themindfactory commented Jan 6, 2021 via email

@TD-er
Copy link
Contributor

TD-er commented Jan 8, 2021

@themindfactory
I guess @fcapano tries to make a point -1 is not of the same type as size_t.
This means you may need to perform a bit more complex (or at least less intuitive) test to check the return value.

So you have 3 different return value states to check:

  • return value < size: Not all expected data was read.
  • return value == size: OK situation.
  • return value > size: The error value (-1)

@themindfactory
Copy link
Contributor

themindfactory commented Jan 8, 2021 via email

@fcapano
Copy link
Author

fcapano commented Jan 8, 2021

It's not a given the caller would check if the returned value is negative, given that it's returning an unsigned number.
Wouldn't it be more appropriate to return zero?

Just to drive the point further, this little program compiled with gcc will return 0:

#include <stddef.h>
size_t x(){ return -1; }

int main() {
    if (x()<0) return 1;
    return 0;
}

@earlephilhower
Copy link
Collaborator

This is a case that's probably not hit ever since it only happens when you have a naked File and not one returned by a {spiffs,sd,littlefs}.open() call, but you bring up a good point.

Returning MAXINT on a failed read is the wrong thing here IMO. Either the File::read type return type should have been ssize_t (i.e. signed size, just like POSIX where -1 indicates an error), or a 0 should be returned in this case.

I think I would just return 0 in this case, like in the File::write() calls.

earlephilhower added a commit to earlephilhower/Arduino that referenced this issue Jan 8, 2021
Fixes esp8266#7814.

Return 0, not MAXINT, when a read is called on a File without a backing
instance of a SPIFFS/LittleFS/SD File.
@themindfactory
Copy link
Contributor

themindfactory commented Jan 8, 2021 via email

@earlephilhower
Copy link
Collaborator

0 means EOF for read which seems like a reasonable answer. We don't have errno and I'm loathe to change so many function signatures for such a rare, uncommon case.

Again, this only happens if you have a fundamental, static source code problem (File x; x.read();) and not in the case of SPIFFS/LittleFS/SD file-not-found or other cases. Those cases do have an implementation pointer and already return 0 when the open()ed file isn't valid.

earlephilhower added a commit that referenced this issue Jan 9, 2021
Fixes #7814.

Return 0, not MAXINT, when a read is called on a File without a backing
instance of a SPIFFS/LittleFS/SD File.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants