Skip to content

fix: the error bugs in getrange and setrange in pika 3.5 version#3106

Merged
wangshao1 merged 2 commits into
OpenAtomFoundation:3.5from
YuCai18:hotfix3.5/fix_getsetrange
Jun 13, 2025
Merged

fix: the error bugs in getrange and setrange in pika 3.5 version#3106
wangshao1 merged 2 commits into
OpenAtomFoundation:3.5from
YuCai18:hotfix3.5/fix_getsetrange

Conversation

@YuCai18
Copy link
Copy Markdown
Collaborator

@YuCai18 YuCai18 commented Jun 12, 2025

修复bug #3092:解决导致pika服务崩溃的错误漏洞

执行GETRANGE key1 1 4294967296两次后服务崩溃的原因

  • 问题分析:大的end值在传递给Redis缓存层之前没有进行适当的验证;PikaCache::GetRange直接委托给缓存而不进行边界检查;尝试创建超大字符串时内存分配失败。
  • 下图是连续执行两次GETRANGE key1 1 4294967296之后pika服务挂掉的堆栈信息:

image

执行SETRANGE key1 9223372036854775757 value2后服务崩溃的原因

  • 问题分析:偏移值9223372036854775757接近LLONG_MAX,与值长度计算结合时导致整数溢出和内存损坏。
  • 下图是执行SETRANGE key1 9223372036854775757 value2之后pika服务挂掉的堆栈信息:

image

修改文件部分

  • 重构了src/pika_cache.cc中的GetRange方法,添加了对start和end参数的边界检查;使用full_value变量存储string类型的实际长度,避免过度内存分配。
  • 在conf/pika.conf中新增参数proto_max_bulk_len,即单条数据最大长度限制,其默认大小为512MB(与Redis一致)。
  • 在include/pika_conf.h中新增了int64_t proto_max_bulk_len_ = 0;以及函数int64_t proto_max_bulk_len();和void SetRsyncTimeoutMs(int64_t value)函数。
  • 在src/pika_conf.cc中新增了GetConfInt64Human("proto-max-bulk-len",xxx)函数。
  • 在src/pika_kv.cc的SetrangeCmd::DoInitial中新增读取配置文件pika_conf中proto-max-bulk-len参数的功能;处理了offset_的溢出问题,当offset_大小超过设定值抛出Error:"string exceeds maximum allowed size (proto-max-bulk-len)";当offset大小小于0则抛出Error:"offset is out of range"。
  • 在string_test.go文件中追加了测试用例并通过了测试。

image-20250612094248664

Summary by CodeRabbit

  • New Features

    • Introduced a configuration option to set the maximum allowed size for a single bulk string in the protocol.
  • Bug Fixes

    • Improved error handling for string range commands to prevent crashes and ensure correct behavior with large indices or offsets.
    • Enhanced validation to prevent setting strings that exceed the configured maximum size.
    • Optimized substring retrieval to handle negative indices and avoid excessive memory allocation.
  • Tests

    • Added integration tests to verify correct handling of edge cases for GETRANGE and SETRANGE commands.

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Jun 12, 2025

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@github-actions github-actions Bot added the ☢️ Bug Something isn't working label Jun 12, 2025
Comment thread src/pika_cache.cc
}
int64_t strlen = full_value.size();

if (start < 0) {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

redis官方的实现也支持传负值吗?

Comment thread include/pika_conf.h
void SetProtoMaxBulkLen(const int64_t value) {
std::lock_guard l(rwlock_);
TryPushDiffCommands("proto-max-bulk-len", std::to_string(value));
proto_max_bulk_len_ = value;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个参数我理解其实就是限制了string类型的value大小,是不是换个别的名字更好理解?

Comment thread src/pika_kv.cc
}
value_ = argv_[3];
// Read the proto-max-bulk-len parameter settings in the pika configuration file pika_conf
const int64_t PROTO_MAX_BULK_LEN = g_pika_conf->proto_max_bulk_len();
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

临时变量不要全大写

@wangshao1 wangshao1 merged commit 09424b2 into OpenAtomFoundation:3.5 Jun 13, 2025
12 checks passed
byseea11 pushed a commit to byseea11/pikiwidb that referenced this pull request Sep 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

☢️ Bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants