Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix invalid memory access bug in tidbGreatestString #8613

Merged

Conversation

windtalker
Copy link
Contributor

@windtalker windtalker commented Dec 27, 2023

What problem does this PR solve?

Issue Number: close #8604

Problem Summary:
In LeastGreatestStringImpl::mergeImpl, it use memcpy to copy data to the target column, however, it does not reserve enough space before LeastGreatestStringImpl::mergeImpl, so there maybe some invalid memory read/write in this function, and will cause issue randomly.

What is changed and how it works?

Check List

Tests

  • Unit test
  • Integration test
  • Manual test (add detailed scripts or steps below)
  • No code

Side effects

  • Performance regression: Consumes more CPU
  • Performance regression: Consumes more Memory
  • Breaking backward compatibility

Documentation

  • Affects user behaviors
  • Contains syntax changes
  • Contains variable changes
  • Contains experimental features
  • Changes MySQL compatibility

Release note

Fix random invalid memory access in greatest/leatest function containing constant string arguments

@ti-chi-bot ti-chi-bot bot added do-not-merge/needs-triage-completed release-note-none Denotes a PR that doesn't merit a release note. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. needs-cherry-pick-release-7.1 Should cherry pick this PR to release-7.1 branch. needs-cherry-pick-release-7.5 Should cherry pick this PR to release-7.5 branch. and removed do-not-merge/needs-triage-completed labels Dec 27, 2023
@windtalker windtalker changed the title fix bug fix invalid memory access bug in tidbGreatestString Dec 27, 2023
@windtalker
Copy link
Contributor Author

/hold

@ti-chi-bot ti-chi-bot bot added the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Dec 27, 2023
Copy link
Contributor

@gengliqi gengliqi left a comment

Choose a reason for hiding this comment

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

Rest LGTM

size_t size = std::min(a_size, b_size);
memcpy(&c_data[pre_offset], &b_data[0], size);
c_offsets[i] = pre_offset + size + 1;
append_data(&a_data[0], a_size);
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
append_data(&a_data[0], a_size);
append_data(a_data, a_size);

memcpy(&c_data[pre_offset], &b_data[0], b_size);
c_offsets[i] = pre_offset + b_size + 1;
assert(res > 0 || a_size == b_size);
append_data(&b_data[0], b_size);
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
append_data(&b_data[0], b_size);
append_data(b_data, b_size);

memcpy(&c_data[pre_offset], &b_data[0], size);
c_offsets[i] = pre_offset + size + 1;
}
append_data(&b_data[0], b_size);
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
append_data(&b_data[0], b_size);
append_data(b_data, b_size);

memcpy(&c_data[pre_offset], &a_data[0], a_size);
c_offsets[i] = pre_offset + a_size + 1;
assert(res > 0 || a_size == b_size);
append_data(&a_data[0], a_size);
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
append_data(&a_data[0], a_size);
append_data(a_data, a_size);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

@ti-chi-bot ti-chi-bot bot added needs-1-more-lgtm Indicates a PR needs 1 more LGTM. approved labels Dec 27, 2023
Copy link
Contributor

@yibin87 yibin87 left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Contributor

ti-chi-bot bot commented Dec 28, 2023

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: gengliqi, yibin87

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@ti-chi-bot ti-chi-bot bot added lgtm and removed needs-1-more-lgtm Indicates a PR needs 1 more LGTM. labels Dec 28, 2023
Copy link
Contributor

ti-chi-bot bot commented Dec 28, 2023

[LGTM Timeline notifier]

Timeline:

  • 2023-12-27 10:50:02.319327827 +0000 UTC m=+1649293.356554740: ☑️ agreed by gengliqi.
  • 2023-12-28 01:42:05.645555848 +0000 UTC m=+1702816.682782774: ☑️ agreed by yibin87.

@windtalker windtalker force-pushed the fix_memory_issue_in_greatest_string branch from 250ff76 to b704275 Compare December 28, 2023 02:14
@windtalker
Copy link
Contributor Author

/hold cancel

@ti-chi-bot ti-chi-bot bot removed the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Dec 28, 2023
@windtalker
Copy link
Contributor Author

/run-all-tests

Copy link
Contributor

ti-chi-bot bot commented Dec 28, 2023

@windtalker: Your PR was out of date, I have automatically updated it for you.

At the same time I will also trigger all tests for you:

/run-all-tests

trigger some heavy tests which will not run always when PR updated.

If the CI test fails, you just re-trigger the test that failed and the bot will merge the PR for you after the CI passes.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the ti-community-infra/tichi repository.

Signed-off-by: xufei <[email protected]>
Signed-off-by: xufei <[email protected]>
@windtalker windtalker force-pushed the fix_memory_issue_in_greatest_string branch from 5ab4ff3 to bb70050 Compare December 28, 2023 08:16
@windtalker
Copy link
Contributor Author

/run-all-tests

@ti-chi-bot ti-chi-bot bot merged commit 10f9542 into pingcap:master Dec 28, 2023
6 checks passed
@ti-chi-bot
Copy link
Member

In response to a cherrypick label: new pull request created to branch release-7.1: #8619.

@ti-chi-bot
Copy link
Member

In response to a cherrypick label: new pull request created to branch release-7.5: #8620.

@windtalker windtalker deleted the fix_memory_issue_in_greatest_string branch December 28, 2023 09:26
ti-chi-bot bot pushed a commit that referenced this pull request Jan 4, 2024
ti-chi-bot bot pushed a commit that referenced this pull request Jan 4, 2024
@ti-chi-bot ti-chi-bot bot added release-note Denotes a PR that will be considered when it comes time to generate release notes. and removed release-note-none Denotes a PR that doesn't merit a release note. labels Jan 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved lgtm needs-cherry-pick-release-7.1 Should cherry pick this PR to release-7.1 branch. needs-cherry-pick-release-7.5 Should cherry pick this PR to release-7.5 branch. release-note Denotes a PR that will be considered when it comes time to generate release notes. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Memory control overestimates memory usage when executing SQL with LEAST(...) or GREATEST(...)
4 participants