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 <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Jeff King committed
Jun 10, 2014 at 17:39 UTC
b000c59b0c80fc187e5e0e48dc9396cd60576c4e
5 files changed
+23
-16
builtin/blame.c
+1
-1
@@ -1405,7 +1405,7 @@ static void get_commit_info(struct commit *commit,
1405
{
1406
int len;
1407
const char *subject, *encoding;
1408
- char *message;
1408
+ const char *message;
1409
1410
commit_info_init(ret);
1411
builtin/reset.c
+1
-1
@@ -93,7 +93,7 @@ static int reset_index(const unsigned char *sha1, int reset_type, int quiet)
93
static void print_new_head_line(struct commit *commit)
94
{
95
const char *hex, *body;
96
- char *msg;
96
+ const char *msg;
97
98
hex = find_unique_abbrev(commit->object.sha1, DEFAULT_ABBREV);
99
printf(_("HEAD is now at %s"), hex);
commit.h
+4
-4
@@ -115,10 +115,10 @@ struct userformat_want {
115
116
extern int has_non_ascii(const char *text);
117
struct rev_info; /* in revision.h, it circularly uses enum cmit_fmt */
118
-extern char *logmsg_reencode(const struct commit *commit,
119
- char **commit_encoding,
120
- const char *output_encoding);
121
-extern void logmsg_free(char *msg, const struct commit *commit);
118
+extern const char *logmsg_reencode(const struct commit *commit,
119
+ char **commit_encoding,
120
+ const char *output_encoding);
121
+extern void logmsg_free(const char *msg, const struct commit *commit);
122
extern void get_commit_format(const char *arg, struct rev_info *);
123
extern const char *format_subject(struct strbuf *sb, const char *msg,
124
const char *line_separator);
pretty.c
+7
-7
@@ -606,9 +606,9 @@ static char *replace_encoding_header(char *buf, const char *encoding)
606
return strbuf_detach(&tmp, NULL);
607
}
608
609
-char *logmsg_reencode(const struct commit *commit,
610
- char **commit_encoding,
611
- const char *output_encoding)
609
+const char *logmsg_reencode(const struct commit *commit,
610
+ char **commit_encoding,
611
+ const char *output_encoding)
612
{
613
static const char *utf8 = "UTF-8";
614
const char *use_encoding;
@@ -687,10 +687,10 @@ char *logmsg_reencode(const struct commit *commit,
687
return out ? out : msg;
688
}
689
690
-void logmsg_free(char *msg, const struct commit *commit)
690
+void logmsg_free(const char *msg, const struct commit *commit)
691
{
692
if (msg != commit->buffer)
693
- free(msg);
693
+ free((void *)msg);
694
}
695
696
static int mailmap_name(const char **email, size_t *email_len,
@@ -796,7 +796,7 @@ struct format_commit_context {
796
struct signature_check signature_check;
797
enum flush_type flush_type;
798
enum trunc_type truncate;
799
- char *message;
799
+ const char *message;
800
char *commit_encoding;
801
size_t width, indent1, indent2;
802
int auto_color;
@@ -1700,7 +1700,7 @@ void pretty_print_commit(struct pretty_print_context *pp,
1700
unsigned long beginning_of_body;
1701
int indent = 4;
1702
const char *msg;
1703
- char *reencoded;
1703
+ const char *reencoded;
1704
const char *encoding;
1705
int need_8bit_cte = pp->need_8bit_cte;
1706
revision.c
+10
-3
@@ -2788,7 +2788,7 @@ static int commit_match(struct commit *commit, struct rev_info *opt)
2788
{
2789
int retval;
2790
const char *encoding;
2791
- char *message;
2791
+ const char *message;
2792
struct strbuf buf = STRBUF_INIT;
2793
2794
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)
2830
format_display_notes(commit->object.sha1, &buf, encoding, 1);
2831
}
2832
2833
- /* Find either in the original commit message, or in the temporary */
2833
+ /*
2834
+ * Find either in the original commit message, or in the temporary.
2835
+ * Note that we cast away the constness of "message" here. It is
2836
+ * const because it may come from the cached commit buffer. That's OK,
2837
+ * because we know that it is modifiable heap memory, and that while
2838
+ * grep_buffer may modify it for speed, it will restore any
2839
+ * changes before returning.
2840
+ */
2841
if (buf.len)
2842
retval = grep_buffer(&opt->grep_filter, buf.buf, buf.len);
2843
else
2844
retval = grep_buffer(&opt->grep_filter,
2838
- message, strlen(message));
2845
+ (char *)message, strlen(message));
2846
strbuf_release(&buf);
2847
logmsg_free(message, commit);
2848
return retval;