Copilot commented on code in PR #3481:
URL: https://github.com/apache/kvrocks/pull/3481#discussion_r3367463100


##########
src/storage/redis_metadata.cc:
##########
@@ -644,3 +644,50 @@ rocksdb::Status TimeSeriesMetadata::Decode(Slice *input) {
 
   return rocksdb::Status::OK();
 }
+
+void CuckooChainMetadata::Encode(std::string *dst) const {
+  Metadata::Encode(dst);
+
+  PutFixed16(dst, n_filters);
+  PutFixed16(dst, expansion);
+  PutFixed64(dst, base_capacity);
+  PutFixed8(dst, bucket_size);
+  PutFixed16(dst, max_iterations);
+  PutFixed64(dst, num_deleted_items);
+  PutFixed32(dst, page_size);
+}
+
+rocksdb::Status CuckooChainMetadata::Decode(Slice *input) {
+  if (auto s = Metadata::Decode(input); !s.ok()) {
+    return s;
+  }
+
+  if (input->size() < sizeof(uint16_t) * 3 + sizeof(uint64_t) * 2 + 
sizeof(uint32_t) + sizeof(uint8_t)) {
+    return rocksdb::Status::InvalidArgument(kErrMetadataTooShort);
+  }
+
+  GetFixed16(input, &n_filters);
+  GetFixed16(input, &expansion);
+  GetFixed64(input, &base_capacity);
+  GetFixed8(input, &bucket_size);
+  GetFixed16(input, &max_iterations);
+  GetFixed64(input, &num_deleted_items);
+  GetFixed32(input, &page_size);
+
+  return rocksdb::Status::OK();
+}
+
+uint64_t CuckooChainMetadata::GetTotalCapacity() const {
+  if (expansion == 0 || n_filters == 1) {
+    return base_capacity;
+  }
+
+  // Calculate total capacity across all filters
+  uint64_t total = 0;
+  uint64_t filter_capacity = base_capacity;
+  for (uint16_t i = 0; i < n_filters; i++) {
+    total += filter_capacity;
+    filter_capacity *= expansion;
+  }
+  return total;

Review Comment:
   GetTotalCapacity multiplies and accumulates capacities without overflow 
checks. With large n_filters/expansion/base_capacity (or corrupted metadata), 
this can wrap uint64_t and return a smaller value, which can break any caller 
relying on this for sizing/limits.



##########
src/types/redis_cuckoo_chain.cc:
##########
@@ -0,0 +1,294 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+
+#include "redis_cuckoo_chain.h"
+
+#include "cuckoo_filter.h"
+#include "cuckoo_filter_sub_filter.h"
+#include "logging.h"
+
+namespace redis {
+
+rocksdb::Status CuckooChain::getCuckooChainMetadata(engine::Context &ctx, 
const Slice &ns_key,
+                                                    CuckooChainMetadata 
*metadata) {
+  return Database::GetMetadata(ctx, {kRedisCuckooFilter}, ns_key, metadata);
+}
+
+rocksdb::Status CuckooChain::validateMetadata(const CuckooChainMetadata 
&metadata) {
+  if (metadata.n_filters == 0) {
+    return rocksdb::Status::Corruption("invalid metadata: n_filters is 0");
+  }
+  if (metadata.base_capacity == 0) {
+    return rocksdb::Status::Corruption("invalid metadata: base_capacity is 0");
+  }
+  if (metadata.bucket_size == 0) {
+    return rocksdb::Status::Corruption("invalid metadata: bucket_size is 0");
+  }
+  if (metadata.max_iterations == 0) {
+    return rocksdb::Status::Corruption("invalid metadata: max_iterations is 
0");
+  }
+  if (metadata.page_size < metadata.bucket_size) {
+    return rocksdb::Status::Corruption("invalid metadata: page_size is smaller 
than bucket_size");
+  }
+  if (!CuckooFilterHelper::IsCapacitySupported(metadata.base_capacity, 
metadata.bucket_size)) {
+    return rocksdb::Status::Corruption("invalid metadata: base_capacity is too 
large");
+  }
+  return rocksdb::Status::OK();
+}

Review Comment:
   validateMetadata doesn't sanity-check the relationship between expansion and 
n_filters (e.g., expansion=0 but n_filters>1) or cap expansion to the supported 
max. Those inconsistent states can lead to nonsensical capacity calculations 
(e.g., later sub-filters getting capacity 0) and hard-to-diagnose corruption at 
runtime.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to