feat(kvs): enforce maximum key length of 32 bytes (C++)#308
Conversation
License Check Results🚀 The license check job ran with the Bazel command: bazel run --lockfile_mode=error //:license-checkStatus: Click to expand output |
|
The created documentation from the pull request is available at: docu-html |
arkjedrz
left a comment
There was a problem hiding this comment.
Replace PR description with a human-made one. Current is both inconcise and wrong.
| auto sv = element.first.GetAsStringView(); | ||
| std::string key(sv.data(), sv.size()); | ||
|
|
||
| /* FEAT_REQ__KVS__maximum_size: keys read from persistent storage are not |
There was a problem hiding this comment.
This is simply invalid. Files are either expected to satisfy the limit or are incorrect.
BTW comp_req__kvs__key_length is the right req.
There was a problem hiding this comment.
Your points have been resolved.
| logger->LogWarn() << "Skipping key with length " << key.length() | ||
| << " exceeding maximum allowed " << KVS_MAX_KEY_LENGTH_BYTES | ||
| << " bytes while loading"; | ||
| continue; |
There was a problem hiding this comment.
This is a quiet failure based on invalid assumption. Make it an error and halt the execution.
|
|
||
| /* FEAT_REQ__KVS__maximum_size: single check for the key-length rule. | ||
| std::string_view::length() counts bytes, matching the byte-based requirement. */ | ||
| bool Kvs::is_key_length_valid(std::string_view key) |
There was a problem hiding this comment.
This helper don't have to rely on anything Kvs class related. Rework it to a function, then place it in anonymous namespace/mark it as static.
| /* Set the value for a key*/ | ||
| score::ResultBlank Kvs::set_value(const std::string_view key, const KvsValue& value) | ||
| { | ||
| /* FEAT_REQ__KVS__maximum_size: reject keys that exceed the maximum length. */ |
There was a problem hiding this comment.
Invalid req, like in other places.
Summary
Enforces the maximum key length for the C++ KVS, implementing
comp_req__kvs__key_lengthChanges
KVS_MAX_KEY_LENGTH_BYTES(32) constant and a file-localis_key_length_valid()helper used as the single source of truth for the rule.set_value()rejects keys longer than the limit and returns the newErrorCode::KeyTooLong.parse_json_data()treats an over-length key found while loading as an invalidor corrupted file: it halts loading and returns
ErrorCode::KeyTooLongratherthan silently dropping the entry.
KeyTooLongerror code and its message.Tests
set_valueaccepts a key of exactly 32 bytes and rejects a 33-byte key withKeyTooLong.parse_json_datareturnsKeyTooLongwhen a loaded object contains anover-length key.
MessageForcovers the new error code.Notes
std::string_view::length()counts bytes, matchingthe requirement.