-
-
Notifications
You must be signed in to change notification settings - Fork 50
Attempt to address SPI SDCard speed limits #29
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 | ||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -65,6 +65,11 @@ bool AVIParser::open() | |||||||||||||||||||||||||||||||||||||||||||||||
| Serial.printf("Failed to open file.\n"); | ||||||||||||||||||||||||||||||||||||||||||||||||
| return false; | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
| #ifdef FILE_BUFFER_SIZE | ||||||||||||||||||||||||||||||||||||||||||||||||
| // Increase size of file buffer if needed. | ||||||||||||||||||||||||||||||||||||||||||||||||
| Serial.printf("Setting file buffer size to %d bytes.\n", FILE_BUFFER_SIZE); | ||||||||||||||||||||||||||||||||||||||||||||||||
| setvbuf(mFile, NULL, _IOFBF, FILE_BUFFER_SIZE); | ||||||||||||||||||||||||||||||||||||||||||||||||
| #endif | ||||||||||||||||||||||||||||||||||||||||||||||||
| // check the file is valid | ||||||||||||||||||||||||||||||||||||||||||||||||
| ChunkHeader header; | ||||||||||||||||||||||||||||||||||||||||||||||||
| // Read RIFF header | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -129,7 +134,7 @@ bool AVIParser::open() | |||||||||||||||||||||||||||||||||||||||||||||||
| return true; | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| size_t AVIParser::getNextChunk(uint8_t **buffer, size_t &bufferLength) | ||||||||||||||||||||||||||||||||||||||||||||||||
| size_t AVIParser::getNextChunk(uint8_t **buffer, size_t &bufferLength, bool skipRead) | ||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||
| // check if the file is open | ||||||||||||||||||||||||||||||||||||||||||||||||
| if (!mFile) | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -153,14 +158,22 @@ size_t AVIParser::getNextChunk(uint8_t **buffer, size_t &bufferLength) | |||||||||||||||||||||||||||||||||||||||||||||||
| if (mRequiredChunkType == AVIChunkType::VIDEO && isVideoChunk || | ||||||||||||||||||||||||||||||||||||||||||||||||
| mRequiredChunkType == AVIChunkType::AUDIO && isAudioChunk) | ||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||
| // we've got the required chunk - copy it into the provided buffer | ||||||||||||||||||||||||||||||||||||||||||||||||
| // reallocate the buffer if necessary | ||||||||||||||||||||||||||||||||||||||||||||||||
| if (header.chunkSize > bufferLength) | ||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||
| *buffer = (uint8_t *)realloc(*buffer, header.chunkSize); | ||||||||||||||||||||||||||||||||||||||||||||||||
| // we've got the required chunk | ||||||||||||||||||||||||||||||||||||||||||||||||
| if (skipRead) | ||||||||||||||||||||||||||||||||||||||||||||||||
| { // skip over this chunk | ||||||||||||||||||||||||||||||||||||||||||||||||
| fseek(mFile, header.chunkSize, SEEK_CUR); | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
| else | ||||||||||||||||||||||||||||||||||||||||||||||||
| { // copy this chunk into the provided buffer | ||||||||||||||||||||||||||||||||||||||||||||||||
| // reallocate the buffer if necessary | ||||||||||||||||||||||||||||||||||||||||||||||||
| if (header.chunkSize > bufferLength) | ||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||
| *buffer = (uint8_t *)realloc(*buffer, header.chunkSize); | ||||||||||||||||||||||||||||||||||||||||||||||||
| bufferLength = header.chunkSize; | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
| // copy the chunk data | ||||||||||||||||||||||||||||||||||||||||||||||||
| fread(*buffer, header.chunkSize, 1, mFile); | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+169
to
176
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. Unsafe realloc and unchecked fread can crash on OOM or I/O error.
Use a temporary pointer and verify bytes read: - if (header.chunkSize > bufferLength)
- {
- *buffer = (uint8_t *)realloc(*buffer, header.chunkSize);
- bufferLength = header.chunkSize;
- }
- // copy the chunk data
- fread(*buffer, header.chunkSize, 1, mFile);
+ if (header.chunkSize > bufferLength) {
+ uint8_t *newBuf = (uint8_t *)realloc(*buffer, header.chunkSize);
+ if (!newBuf) {
+ Serial.println("Out of memory reallocating frame buffer.");
+ return 0;
+ }
+ *buffer = newBuf;
+ bufferLength = header.chunkSize;
+ }
+ // copy the chunk data
+ size_t n = fread(*buffer, 1, header.chunkSize, mFile);
+ if (n != header.chunkSize) {
+ Serial.println("Short read while reading chunk.");
+ return 0;
+ }📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||
| // copy the chunk data | ||||||||||||||||||||||||||||||||||||||||||||||||
| fread(*buffer, header.chunkSize, 1, mFile); | ||||||||||||||||||||||||||||||||||||||||||||||||
| mMoviListLength -= header.chunkSize; | ||||||||||||||||||||||||||||||||||||||||||||||||
| // handle any padding bytes | ||||||||||||||||||||||||||||||||||||||||||||||||
| if (header.chunkSize % 2 != 0) | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
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.
🛠️ Refactor suggestion
Seek error handling when skipping chunks.
When skipRead is true, fseek could fail on a short file or I/O error. Check the return value and bail gracefully:
📝 Committable suggestion
🤖 Prompt for AI Agents