Repository navigation
Fix the transfer buffer floor: 2 ^ 23 is XOR, not 8MB - #17
totoantibes wants to merge 1 commit into
Conversation
The read path sizes its transfer buffer with
min(cbReportedDataSize, max(cbOptimalDataSize * 16, 2 ^ 23))
intending an 8MB floor, as the comment two lines above says. But `^` is XOR in
C++, not exponentiation, so `2 ^ 23` is 21 - the floor is twenty-one BYTES.
This is normally invisible, because cbOptimalDataSize * 16 is a few MB and wins
the max(). It only surfaces when cbOptimalDataSize is 0, and that is reachable:
the variable is initialised to 0 and filled by the
OPTIMAL_TRANSFER_BUFFER_SIZE query whose hr is immediately overwritten by the
TOTAL_DATA_SIZE query on the next line, so a device that does not answer it
fails silently. The floor is then the whole answer, and a 21-byte buffer turns
a 12MB image into roughly 600,000 READ_DATA round trips.
Replace the literal with a named 8MB constant, which both restores the intended
size and makes a missing optimal size harmless.
|
Hi @totoantibes, thank you for this fix. Your original commit The release provides x86/x64 native DLL and diagnostic-tool packages, complete matching native/dependency source, license notices and SHA256SUMS.txt. It is marked regular/Latest on GitHub; the original Manual installation and rollback instructions explain choosing the matching architecture, confirming the client's loaded DLL paths and backing up all three native files. These packages contain the native components and diagnostic tool; no ASCOM wrapper build or installer is included. Windows CI passed both architectures, DLL/export/dependency checks and seven buffer-calculation cases, including rejection of the old calculation. The complete source ZIP also rebuilds on Windows outside Git. Physical camera captures, WPD/USB operation and ASCOM/NINA integration remain unverified for this combined build. Device-removal PR #16 is not included in 1.0.1.22. Its original contribution and follow-up fixes are tracked separately in draft PR #3 in my fork, pending physical-camera validation. This release does not claim to fix USB-removal crashes. Please report results or problems with the maintained packages in the fork's issue tracker. |
The read path sizes its transfer buffer like this:
^is XOR in C++, not exponentiation, so2 ^ 23is 21. The intended 8 MB floor is a twenty-one byte floor.Why it has not bitten
cbOptimalDataSize * 16is a few MB in normal operation and wins themax(), so the broken floor is never reached and the buffer ends up around 4 MB instead of the intended 8 MB. Nothing visibly breaks.Why it can
cbOptimalDataSizeis initialised to 0 and filled by the query immediately above:The first
hris overwritten by the second call before anything tests it, so a device that does not answerOPTIMAL_TRANSFER_BUFFER_SIZEleaves the variable at 0 and the failure is silent.max(0, 21)is then the entire answer, and the read loop transfers 21 bytes per round trip — about 600,000READ_DATAcommands for a 12 MB image, which in practice is a hang rather than a slow download.I have not observed this in the wild, so this is a latent bug rather than a reported one. It is worth fixing because the guard that makes it unreachable is the accident, not the design.
The change
Replace the literal with a named constant:
That does two things at once: it restores the 8 MB the comment describes, and it makes a missing optimal size harmless, since the floor is now a sane buffer rather than 21 bytes.
Note this does change behaviour in the normal path — the buffer becomes 8 MB where it was ~4 MB. That is what the comment intends, and it is a transient allocation freed at the end of the same block, but it is a real change rather than a pure no-op and worth being aware of when merging.
Built clean (x64/Release, driver 1.0.1.21). Based on
workerThreadto match #16.