Tasmota icon indicating copy to clipboard operation
Tasmota copied to clipboard

Berry: fix file.readbytes() for large files

Open Staars opened this issue 1 year ago • 2 comments

Description:

Reading binary files into bytes in Berry leads to data corruption for large files (I did not figure out the threshold) by creating a bytes buffer of the correct size, that has correct data at the front but only holds zeros at the tail end. This fails silently and does not throw any error.

This seems to be related to the resize operation after the actual data read from the file system.

To reproduce we need a test file on the ESP, that is not too small. I used this one: https://github.com/Staars/MockUp/blob/main/watermeter.jpg

Then load in the Berry web console:

f = open("watermeter.jpg","r")
b =f.readbytes()
f.close()

Check the terminating bytes, which should be "FFD9" for a JPG file with:

b[-2..]

The current version shows "0000". Tested on ESP32 and ESP32-S3.

Unrelated to this PR the C6 crashes with heap corruption: CORRUPT HEAP: Bad head at 0x408407e8. Expected 0xabba1234 got 0x52844590

Checklist:

  • [x] The pull request is done against the latest development branch
  • [x] Only relevant files were touched
  • [x] Only one feature/fix was added per PR and the code change compiles without warnings
  • [x] The code change is tested and works with Tasmota core ESP8266 V.2.7.6
  • [x] The code change is tested and works with Tasmota core ESP32 V.3.0.1
  • [x] I accept the CLA.

NOTE: The code change must pass CI tests. Your PR cannot be merged unless tests pass

Staars avatar Jun 19 '24 12:06 Staars

Looks like some much wider issue with non-small files. I tried to transfer a moderately sized file (ftp.be) with both Manage File system upload (web or via curl), or the UrlFetch command, on a board with PSRAM available, and this failed. Closest to a symptom was "file too large". I did manage to transfer a somewhat smaller .be file, 11 KB instead of failing with 17 KB.

sfromis avatar Jun 19 '24 14:06 sfromis

I'm afraid there is a deeper bug in the bytes.resize function. Let's put on hold until I see more clearly what is happening

s-hadinger avatar Jun 19 '24 16:06 s-hadinger

Closing this one, the actual fix is https://github.com/arendst/Tasmota/pull/21716

There was a bug in bytes.resize()

s-hadinger avatar Jul 01 '24 20:07 s-hadinger

Nice! (But I still do not really see the value of the second resize procedure.)

Staars avatar Jul 01 '24 20:07 Staars

The last resize is only here in case the number of bytes read is different from what was expected. It should not happen with Tasmota, but you never know. There are many reason why a read could return less bytes than the size of the file.

s-hadinger avatar Jul 02 '24 07:07 s-hadinger

@s-hadinger The crash with the C6 is still there

Jason2866 avatar Jul 05 '24 12:07 Jason2866