Skip to content

Commit

Permalink
STU-24170: Make sure that read length matches what was requested
Browse files Browse the repository at this point in the history
  • Loading branch information
mpartio committed Sep 11, 2024
1 parent af0cfa3 commit 34388ae
Showing 1 changed file with 25 additions and 4 deletions.
29 changes: 25 additions & 4 deletions himan-lib/source/s3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,24 @@ static S3Status getObjectDataCallback(int bufferSize, const char* buffer, void*
{
himan::buffer* ret = static_cast<himan::buffer*>(callbackData);

ret->data = static_cast<unsigned char*>(realloc(ret->data, ret->length + bufferSize));
memcpy(ret->data + ret->length, buffer, bufferSize);
ret->length += bufferSize;
// extend callback buffer to accomodate the newest chunk of data
void* ptr = realloc(ret->data, ret->length + bufferSize);

if (ptr == nullptr)
{
logger logr("s3");
logr.Error("getObjectDataCallback: Memory allocation failed");
return S3StatusAbortedByCallback;
}

ret->data = static_cast<unsigned char*>(ptr);

if (bufferSize > 0)
{
// copy the new data to the end of the buffer
memcpy(ret->data + ret->length, buffer, bufferSize);
ret->length += bufferSize;
}

return S3StatusOK;
}
Expand Down Expand Up @@ -316,6 +331,8 @@ buffer s3::ReadFile(const file_information& fileInformation)

S3BucketContext bucketContext = GetBucketContext(host, bucket, region);

const unsigned long length = fileInformation.length.value();

int count = 0;
do
{
Expand All @@ -324,7 +341,6 @@ buffer s3::ReadFile(const file_information& fileInformation)
sleep(2 * count);
}
const unsigned long offset = fileInformation.offset.value();
const unsigned long length = fileInformation.length.value();

#ifdef S3_DEFAULT_REGION
S3_get_object(&bucketContext, key.c_str(), NULL, offset, length, NULL, 0, &getObjectHandler, &ret);
Expand All @@ -348,6 +364,11 @@ buffer s3::ReadFile(const file_information& fileInformation)
throw himan::kFileDataNotFound;
}

if (length > 0 && ret.length != length)
{
logr.Error(fmt::format("Expected {} bytes, got {} bytes", fileInformation.length.value(), ret.length));
throw himan::kFileDataNotFound;
}
return ret;
}

Expand Down

0 comments on commit 34388ae

Please sign in to comment.