Skip to content

DiffLine mangled memory #1135

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

Closed
FintanH opened this issue Mar 4, 2025 · 5 comments · Fixed by #1141
Closed

DiffLine mangled memory #1135

FintanH opened this issue Mar 4, 2025 · 5 comments · Fixed by #1141

Comments

@FintanH
Copy link

FintanH commented Mar 4, 2025

I've come across a strange issue where the use of a DiffLine in two different locations ends up with some mangled looking output. I've created a reproducible in this repository https://github.com/FintanH/git2-diff-memory. If you cargo run you should see the following output:

PRINT FROM INSIDE find_lines
- h2 {
+ .event {
    margin-bottom: 1rem;
  }
- .event {
+ h2 {
    margin-bottom: 1rem;
+   padding: 1px;
  }
  hr {
    margin: 1rem 0;
PRINT FROM OUTSIDE find_lines
�H��U  >�r��Sn-bottom: 1rem;
  }
- .event {
+ h2 {
    margin-bottom: 1rem;
+   padding: 1px;
  }
  hr {
    margin: 1rem 0;

Afaict, the outputs should be equal – the range of lines is not different so it should not be selecting other data. My best guess is that the raw pointer within the DiffLine is getting corrupted somehow by returning it within the Vec.

Let me know if I can provide anymore info to help 😊

@FintanH
Copy link
Author

FintanH commented Mar 4, 2025

I've added another commit where only the content is returned as &'a [u8], and it still ends up with the same output. However, if that's changed to a Vec<u8> it's safe and the outputs are the same.

@ehuss
Copy link
Contributor

ehuss commented Mar 12, 2025

Thanks for the report! I have opened a fix at #1141.

@FintanH
Copy link
Author

FintanH commented Mar 12, 2025

Nice 🙏 Have you tested it on the example repo I posted or would you like me to? 😊

@ehuss
Copy link
Contributor

ehuss commented Mar 12, 2025

Yea, I tested it. It will now fail to compile because of the lifetimes. You'll need to copy the buffers if you want it to outlive the Patch, or change it so that the Patch is not dropped before the buffers.

@FintanH
Copy link
Author

FintanH commented Mar 12, 2025

Ah, that makes sense 👌 Thanks for the explanation!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants