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 string corruption in file finder #655

Merged
merged 1 commit into from
Jul 9, 2017

Conversation

rolandwalker
Copy link
Contributor

@rolandwalker rolandwalker commented Jul 8, 2017

Allocate one more byte for file-finder items [edited]

Allocate the full sizeof(struct file_finder_line) rather than the size of a pointer, so that there is enough memory for the NUL terminator (and so that the NUL is pre-filled).

Otherwise strncpy() will take up the entire space, and tig may later read past the end.

src/ui.c Outdated
@@ -64,7 +64,7 @@ file_finder_read(struct file_finder *finder, const char *commit)
break;
}

finder->file[files] = calloc(1, sizeof(*finder->file) + buf.size);
finder->file[files] = calloc(1, sizeof(*finder->file) + buf.size + 1);
Copy link
Owner

Choose a reason for hiding this comment

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

Did you see this default? The text field in file_finder_line has size 1 to account for the NUL byte.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh, I thought the first term was just to count the other member of the struct.

The issue is still there as described, because sizeof(*finder->file) returns constant sizeof(uintptr_t), coincidentally size_t, the size of matches, the other member of the struct.

It should be sizeof(**finder->file) or sizeof(struct file_finder_line). The second one is better because it's harder to typo. And either is better than my + 1 because of alignment.

@rolandwalker rolandwalker force-pushed the file-finder-string-termination branch from ca989aa to 8b10c69 Compare July 8, 2017 18:35
@jonas
Copy link
Owner

jonas commented Jul 9, 2017

Thanks a lot for fixing this!

@jonas jonas merged commit fabef22 into jonas:master Jul 9, 2017
@rolandwalker rolandwalker deleted the file-finder-string-termination branch July 10, 2017 11:08
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 this pull request may close these issues.

2 participants