From b000c59b0c80fc187e5e0e48dc9396cd60576c4e Mon Sep 17 00:00:00 2001 From: Jeff King Date: Tue, 10 Jun 2014 17:39:30 -0400 Subject: logmsg_reencode: return const buffer The return value from logmsg_reencode may be either a newly allocated buffer or a pointer to the existing commit->buffer. We would not want the caller to accidentally free() or modify the latter, so let's mark it as const. We can cast away the constness in logmsg_free, but only once we have determined that it is a free-able buffer. Signed-off-by: Jeff King Signed-off-by: Junio C Hamano --- revision.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) (limited to 'revision.c') diff --git a/revision.c b/revision.c index 71e2337423..be151ef462 100644 --- a/revision.c +++ b/revision.c @@ -2788,7 +2788,7 @@ static int commit_match(struct commit *commit, struct rev_info *opt) { int retval; const char *encoding; - char *message; + const char *message; struct strbuf buf = STRBUF_INIT; if (!opt->grep_filter.pattern_list && !opt->grep_filter.header_list) @@ -2830,12 +2830,19 @@ static int commit_match(struct commit *commit, struct rev_info *opt) format_display_notes(commit->object.sha1, &buf, encoding, 1); } - /* Find either in the original commit message, or in the temporary */ + /* + * Find either in the original commit message, or in the temporary. + * Note that we cast away the constness of "message" here. It is + * const because it may come from the cached commit buffer. That's OK, + * because we know that it is modifiable heap memory, and that while + * grep_buffer may modify it for speed, it will restore any + * changes before returning. + */ if (buf.len) retval = grep_buffer(&opt->grep_filter, buf.buf, buf.len); else retval = grep_buffer(&opt->grep_filter, - message, strlen(message)); + (char *)message, strlen(message)); strbuf_release(&buf); logmsg_free(message, commit); return retval; -- cgit v1.2.3 From b66103c3baa593a39b8b0751213b9fce60e94de4 Mon Sep 17 00:00:00 2001 From: Jeff King Date: Tue, 10 Jun 2014 17:41:39 -0400 Subject: convert logmsg_reencode to get_commit_buffer Like the callsites in the previous commit, logmsg_reencode already falls back to read_sha1_file when necessary. However, I split its conversion out into its own commit because it's a bit more complex. We return either: 1. The original commit->buffer 2. A newly allocated buffer from read_sha1_file 3. A reencoded buffer (based on either 1 or 2 above). while trying to do as few extra reads/allocations as possible. Callers currently free the result with logmsg_free, but we can simplify this by pointing them straight to unuse_commit_buffer. This is a slight layering violation, in that we may be passing a buffer from (3). However, since the end result is to free() anything except (1), which is unlikely to change, and because this makes the interface much simpler, it's a reasonable bending of the rules. Signed-off-by: Jeff King Signed-off-by: Junio C Hamano --- revision.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'revision.c') diff --git a/revision.c b/revision.c index be151ef462..1cc91e5911 100644 --- a/revision.c +++ b/revision.c @@ -2844,7 +2844,7 @@ static int commit_match(struct commit *commit, struct rev_info *opt) retval = grep_buffer(&opt->grep_filter, (char *)message, strlen(message)); strbuf_release(&buf); - logmsg_free(message, commit); + unuse_commit_buffer(commit, message); return retval; } -- cgit v1.2.3