DsFileReader Seek issue with values >= (2^63 - 2^10)

2 views (last 30 days)
I am using DsFileReader to read .mf4 files (ASAM MDF spec files).
One of my files has some corrupted values that return locations well outside the range. Strangely, when calling seek(), DSFileReader can handle them until the value exceeds (2^63-2^10-1). For reference, this file claimes the size is 141397456 (under 2^27). If I try to seek to (2^63 - 2^10) then it will fail.
When the error occurs, I get ByteStreamSeekFailure: iostream stream error. This error is not caught by any Try/Catch, and doesn't have a code, so tracking down the issue is much tougher. I have managed to track it to the call of fr.Stream.seek(bytes, originNum); within the seekWithParsedInputs() function, and I can see the Stream is created as a matlab.io.internal.vfs.stream object, but those files don't appear browsable.
While I know my file is corrupted, I wanted to understand when to expect this non-trackable error so I can make sure any fix for where I am seeking will work and send a proper error before this non-sourced error. (also it's odd that 2^64-2^10 is the magic error threshold, I think it has to do with the biggest integer below 64 bits that Double can represent)

Accepted Answer

Pratyush
Pratyush on 5 Feb 2024
Hi,
The issue you're facing with "DsFileReader" when seeking to a position "(2^63 - 2^10)" in a .mf4 file is likely due to limitations in MATLAB's floating-point representation and the underlying stream's capability. MATLAB can exactly represent integers up to "2^53 - 1" with double-precision floating-point numbers, and seeking beyond the limits of the stream can cause a "ByteStreamSeekFailure" error, which is not catchable with a try/catch block.
To handle this, you should:
  • Ensure the seek position is within the actual file size.
  • Use 64-bit integers (`int64`) for file positions to avoid floating-point inaccuracies.
  • Implement a wrapper function for the seek operation that checks the validity of the seek position before attempting to seek.
  • If necessary, contact the vendor for support or consult the documentation for proprietary functions like `DsFileReader`.
Hope this helps.
  1 Comment
Smattering_Applause
Smattering_Applause on 5 Feb 2024
Reading the file size from the reader object is easy:
fileReader = matlab.io.datastore.DsFileReader(filefullpath);
fileReader.size
Then the target memory length / position can be compared to the file start/size to determine if in bounds. this will also catch cases where file saving errors generated a false number tha's less that 2^53-1 but still outside file size.
Using int64 might work however it wouldn't protect for the values being outside the file size.
So basically the wrapper function is the way I went, put in a few protections before calling read().
Ideally Mathwortks replies and says "we'll make an error handling", but otherwise your answer is as basically as good as we'll get.

Sign in to comment.

More Answers (0)

Categories

Find more on Data Import and Export in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!