Skip to content
This repository was archived by the owner on Nov 9, 2017. It is now read-only.

Commit b000c59

Browse files
peffgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 10322a0 commit b000c59

File tree

5 files changed

+23
-16
lines changed

5 files changed

+23
-16
lines changed

builtin/blame.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1405,7 +1405,7 @@ static void get_commit_info(struct commit *commit,
14051405
{
14061406
int len;
14071407
const char *subject, *encoding;
1408-
char *message;
1408+
const char *message;
14091409

14101410
commit_info_init(ret);
14111411

builtin/reset.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ static int reset_index(const unsigned char *sha1, int reset_type, int quiet)
9393
static void print_new_head_line(struct commit *commit)
9494
{
9595
const char *hex, *body;
96-
char *msg;
96+
const char *msg;
9797

9898
hex = find_unique_abbrev(commit->object.sha1, DEFAULT_ABBREV);
9999
printf(_("HEAD is now at %s"), hex);

commit.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,10 @@ struct userformat_want {
115115

116116
extern int has_non_ascii(const char *text);
117117
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);
122122
extern void get_commit_format(const char *arg, struct rev_info *);
123123
extern const char *format_subject(struct strbuf *sb, const char *msg,
124124
const char *line_separator);

pretty.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -606,9 +606,9 @@ static char *replace_encoding_header(char *buf, const char *encoding)
606606
return strbuf_detach(&tmp, NULL);
607607
}
608608

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)
612612
{
613613
static const char *utf8 = "UTF-8";
614614
const char *use_encoding;
@@ -687,10 +687,10 @@ char *logmsg_reencode(const struct commit *commit,
687687
return out ? out : msg;
688688
}
689689

690-
void logmsg_free(char *msg, const struct commit *commit)
690+
void logmsg_free(const char *msg, const struct commit *commit)
691691
{
692692
if (msg != commit->buffer)
693-
free(msg);
693+
free((void *)msg);
694694
}
695695

696696
static int mailmap_name(const char **email, size_t *email_len,
@@ -796,7 +796,7 @@ struct format_commit_context {
796796
struct signature_check signature_check;
797797
enum flush_type flush_type;
798798
enum trunc_type truncate;
799-
char *message;
799+
const char *message;
800800
char *commit_encoding;
801801
size_t width, indent1, indent2;
802802
int auto_color;
@@ -1700,7 +1700,7 @@ void pretty_print_commit(struct pretty_print_context *pp,
17001700
unsigned long beginning_of_body;
17011701
int indent = 4;
17021702
const char *msg;
1703-
char *reencoded;
1703+
const char *reencoded;
17041704
const char *encoding;
17051705
int need_8bit_cte = pp->need_8bit_cte;
17061706

revision.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2788,7 +2788,7 @@ static int commit_match(struct commit *commit, struct rev_info *opt)
27882788
{
27892789
int retval;
27902790
const char *encoding;
2791-
char *message;
2791+
const char *message;
27922792
struct strbuf buf = STRBUF_INIT;
27932793

27942794
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)
28302830
format_display_notes(commit->object.sha1, &buf, encoding, 1);
28312831
}
28322832

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+
*/
28342841
if (buf.len)
28352842
retval = grep_buffer(&opt->grep_filter, buf.buf, buf.len);
28362843
else
28372844
retval = grep_buffer(&opt->grep_filter,
2838-
message, strlen(message));
2845+
(char *)message, strlen(message));
28392846
strbuf_release(&buf);
28402847
logmsg_free(message, commit);
28412848
return retval;

0 commit comments

Comments
 (0)