From cd0f76a239d13713f6695e952c3eb68ec1014c66 Mon Sep 17 00:00:00 2001 From: Joel Klinghed Date: Sun, 25 Aug 2024 00:09:26 +0200 Subject: samba: Fix reading/writing large amounts of data Two bugs: * Didn't consider smb2_context max read/write size. * total was not increased when max was hit --- libs/samba/src/main/cpp/samba.cpp | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) (limited to 'libs/samba/src') diff --git a/libs/samba/src/main/cpp/samba.cpp b/libs/samba/src/main/cpp/samba.cpp index 4a8af51..302fb3a 100644 --- a/libs/samba/src/main/cpp/samba.cpp +++ b/libs/samba/src/main/cpp/samba.cpp @@ -63,13 +63,15 @@ class File { int32_t read(uint8_t* data, int32_t size) { if (size <= 0) return 0; + const uint32_t max = std::min(smb2_get_max_read_size(context_.get()), + static_cast(std::numeric_limits::max())); int32_t total = 0; - while (size > std::numeric_limits::max()) { - int ret = smb2_read(context_.get(), fh_, data, - std::numeric_limits::max()); + while (size > max) { + int ret = smb2_read(context_.get(), fh_, data, max); if (ret < 0) return total ? total : ret; - if (ret != std::numeric_limits::max()) - return total + ret; + total += ret; + if (ret != max) + return total; data += ret; size -= ret; } @@ -80,13 +82,15 @@ class File { int32_t write(const uint8_t* data, int32_t size) { if (size <= 0) return 0; + const uint32_t max = std::min(smb2_get_max_write_size(context_.get()), + static_cast(std::numeric_limits::max())); int32_t total = 0; - while (size > std::numeric_limits::max()) { - int ret = smb2_write(context_.get(), fh_, data, - std::numeric_limits::max()); + while (size > max) { + int ret = smb2_write(context_.get(), fh_, data, max); if (ret < 0) return total ? total : ret; - if (ret != std::numeric_limits::max()) - return total + ret; + total += ret; + if (ret != max) + return total; data += ret; size -= ret; } -- cgit v1.2.3-70-g09d2