From 39433be204197fbc023d2f9bf4b2b62c206874ed Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 8 Apr 2026 12:49:24 +0000 Subject: [PATCH] Fix uint32 overflow in binarymatrix read for large sample numbers In MATLAB, double * uint32 = uint32, which silently overflows when computing byte offsets for sample numbers exceeding ~2 billion (e.g., sample 15,321,600,000 in long Neuropixels recordings). Cast num_channels, bytes_per_sample, and channel skip counts to double so all intermediate arithmetic stays in double (53-bit mantissa, good to ~9e15). https://claude.ai/code/session_01VLnP9PJcs7q2zxej9aTJAe --- +ndr/+format/+binarymatrix/read.m | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/+ndr/+format/+binarymatrix/read.m b/+ndr/+format/+binarymatrix/read.m index edddb95..2380916 100644 --- a/+ndr/+format/+binarymatrix/read.m +++ b/+ndr/+format/+binarymatrix/read.m @@ -92,13 +92,13 @@ bytes_per_value = bits / 8; end; -total_samples = (d.bytes-double(options.headerSkip)) / (num_channels * bytes_per_value); +total_samples = (d.bytes-double(options.headerSkip)) / (double(num_channels) * bytes_per_value); if isinf(s1) & (s1>0), s1 = total_samples; end; -bytes_per_sample = bytes_per_value * num_channels; +bytes_per_sample = bytes_per_value * double(num_channels); channel_indexes = channel_indexes(:); % force column @@ -109,8 +109,8 @@ fid = fopen(filename_or_fileobj,'r',options.byteOrder); if ~options.force_single_channel_read & consecutive_channels_requested, - channels_to_skip_before_reading = chan_sort(1) - 1; - channels_to_skip_after_reading = num_channels - chan_sort(end); + channels_to_skip_before_reading = double(chan_sort(1) - 1); + channels_to_skip_after_reading = double(num_channels - chan_sort(end)); skip_point = double(options.headerSkip) + ... (s0-1)*bytes_per_sample + ... % skip to the sample ... @@ -130,8 +130,8 @@ else, data = zeros(s1-s0+1,numel(channel_indexes)); for c=1:numel(channel_indexes), - channels_to_skip_before_reading = channel_indexes(c) - 1; - channels_to_skip_after_reading = num_channels - channel_indexes(c); + channels_to_skip_before_reading = double(channel_indexes(c) - 1); + channels_to_skip_after_reading = double(num_channels - channel_indexes(c)); skip_point = double(options.headerSkip) + ... (s0-1)*bytes_per_sample + ... % skip to the sample ...