From a76be11d86c90b26f9fcd9738262349784cd5b98 Mon Sep 17 00:00:00 2001 From: bansvaru Date: Fri, 1 Sep 2023 11:00:51 +0530 Subject: [PATCH] Fix: correct limit checks for S3Container refactoring Signed-off-by: bansvaru --- .../java/org/opensearch/repositories/s3/S3BlobContainer.java | 3 ++- .../java/org/opensearch/common/blobstore/BlobContainer.java | 3 +++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/plugins/repository-s3/src/main/java/org/opensearch/repositories/s3/S3BlobContainer.java b/plugins/repository-s3/src/main/java/org/opensearch/repositories/s3/S3BlobContainer.java index d8b3ebfd8d238..708e33548fbe8 100644 --- a/plugins/repository-s3/src/main/java/org/opensearch/repositories/s3/S3BlobContainer.java +++ b/plugins/repository-s3/src/main/java/org/opensearch/repositories/s3/S3BlobContainer.java @@ -356,10 +356,11 @@ public List listBlobsByPrefixInSortedOrder(String blobNamePrefix, } String prefix = blobNamePrefix == null ? keyPath : buildKey(blobNamePrefix); try (AmazonS3Reference clientReference = blobStore.clientReference()) { - return executeListing(clientReference, listObjectsRequest(prefix, limit), limit).stream() + List blobs = executeListing(clientReference, listObjectsRequest(prefix, limit), limit).stream() .flatMap(listing -> listing.contents().stream()) .map(s3Object -> new PlainBlobMetadata(s3Object.key().substring(keyPath.length()), s3Object.size())) .collect(Collectors.toList()); + return blobs.subList(0, Math.min(limit, blobs.size())); } catch (final Exception e) { throw new IOException("Exception when listing blobs by prefix [" + prefix + "]", e); } diff --git a/server/src/main/java/org/opensearch/common/blobstore/BlobContainer.java b/server/src/main/java/org/opensearch/common/blobstore/BlobContainer.java index 4bbe15fccc1bb..3cdb1ce30b68d 100644 --- a/server/src/main/java/org/opensearch/common/blobstore/BlobContainer.java +++ b/server/src/main/java/org/opensearch/common/blobstore/BlobContainer.java @@ -227,6 +227,9 @@ default void listBlobsByPrefixInSortedOrder( BlobNameSortOrder blobNameSortOrder, ActionListener> listener ) { + if (limit < 0) { + throw new IllegalArgumentException("limit should not be a negative value"); + } try { listener.onResponse(listBlobsByPrefixInSortedOrder(blobNamePrefix, limit, blobNameSortOrder)); } catch (Exception e) {