Skip to content

Commit 28ecef4

Browse files
committed
Merge branch 'jk/grep-haystack-is-read-only' into hm/paint-hits-in-log-grep
* jk/grep-haystack-is-read-only: grep: store grep_source buffer as const grep: mark "haystack" buffers as const grep: stop modifying buffer in grep_source_1() grep: stop modifying buffer in show_line() grep: stop modifying buffer in strip_timestamp
2 parents 99c99ed + 1e66871 commit 28ecef4

File tree

2 files changed

+45
-46
lines changed

2 files changed

+45
-46
lines changed

grep.c

Lines changed: 43 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -867,7 +867,7 @@ void free_grep_patterns(struct grep_opt *opt)
867867
free_pattern_expr(opt->pattern_expression);
868868
}
869869

870-
static char *end_of_line(char *cp, unsigned long *left)
870+
static const char *end_of_line(const char *cp, unsigned long *left)
871871
{
872872
unsigned long l = *left;
873873
while (l && *cp != '\n') {
@@ -908,7 +908,8 @@ static void show_name(struct grep_opt *opt, const char *name)
908908
opt->output(opt, opt->null_following_name ? "\0" : "\n", 1);
909909
}
910910

911-
static int patmatch(struct grep_pat *p, char *line, char *eol,
911+
static int patmatch(struct grep_pat *p,
912+
const char *line, const char *eol,
912913
regmatch_t *match, int eflags)
913914
{
914915
int hit;
@@ -922,20 +923,16 @@ static int patmatch(struct grep_pat *p, char *line, char *eol,
922923
return hit;
923924
}
924925

925-
static int strip_timestamp(char *bol, char **eol_p)
926+
static void strip_timestamp(const char *bol, const char **eol_p)
926927
{
927-
char *eol = *eol_p;
928-
int ch;
928+
const char *eol = *eol_p;
929929

930930
while (bol < --eol) {
931931
if (*eol != '>')
932932
continue;
933933
*eol_p = ++eol;
934-
ch = *eol;
935-
*eol = '\0';
936-
return ch;
934+
break;
937935
}
938-
return 0;
939936
}
940937

941938
static struct {
@@ -947,12 +944,12 @@ static struct {
947944
{ "reflog ", 7 },
948945
};
949946

950-
static int match_one_pattern(struct grep_pat *p, char *bol, char *eol,
947+
static int match_one_pattern(struct grep_pat *p,
948+
const char *bol, const char *eol,
951949
enum grep_context ctx,
952950
regmatch_t *pmatch, int eflags)
953951
{
954952
int hit = 0;
955-
int saved_ch = 0;
956953
const char *start = bol;
957954

958955
if ((p->token != GREP_PATTERN) &&
@@ -971,7 +968,7 @@ static int match_one_pattern(struct grep_pat *p, char *bol, char *eol,
971968
switch (p->field) {
972969
case GREP_HEADER_AUTHOR:
973970
case GREP_HEADER_COMMITTER:
974-
saved_ch = strip_timestamp(bol, &eol);
971+
strip_timestamp(bol, &eol);
975972
break;
976973
default:
977974
break;
@@ -1021,17 +1018,16 @@ static int match_one_pattern(struct grep_pat *p, char *bol, char *eol,
10211018
goto again;
10221019
}
10231020
}
1024-
if (p->token == GREP_PATTERN_HEAD && saved_ch)
1025-
*eol = saved_ch;
10261021
if (hit) {
10271022
pmatch[0].rm_so += bol - start;
10281023
pmatch[0].rm_eo += bol - start;
10291024
}
10301025
return hit;
10311026
}
10321027

1033-
static int match_expr_eval(struct grep_opt *opt, struct grep_expr *x, char *bol,
1034-
char *eol, enum grep_context ctx, ssize_t *col,
1028+
static int match_expr_eval(struct grep_opt *opt, struct grep_expr *x,
1029+
const char *bol, const char *eol,
1030+
enum grep_context ctx, ssize_t *col,
10351031
ssize_t *icol, int collect_hits)
10361032
{
10371033
int h = 0;
@@ -1098,15 +1094,17 @@ static int match_expr_eval(struct grep_opt *opt, struct grep_expr *x, char *bol,
10981094
return h;
10991095
}
11001096

1101-
static int match_expr(struct grep_opt *opt, char *bol, char *eol,
1097+
static int match_expr(struct grep_opt *opt,
1098+
const char *bol, const char *eol,
11021099
enum grep_context ctx, ssize_t *col,
11031100
ssize_t *icol, int collect_hits)
11041101
{
11051102
struct grep_expr *x = opt->pattern_expression;
11061103
return match_expr_eval(opt, x, bol, eol, ctx, col, icol, collect_hits);
11071104
}
11081105

1109-
static int match_line(struct grep_opt *opt, char *bol, char *eol,
1106+
static int match_line(struct grep_opt *opt,
1107+
const char *bol, const char *eol,
11101108
ssize_t *col, ssize_t *icol,
11111109
enum grep_context ctx, int collect_hits)
11121110
{
@@ -1138,7 +1136,8 @@ static int match_line(struct grep_opt *opt, char *bol, char *eol,
11381136
return hit;
11391137
}
11401138

1141-
static int match_next_pattern(struct grep_pat *p, char *bol, char *eol,
1139+
static int match_next_pattern(struct grep_pat *p,
1140+
const char *bol, const char *eol,
11421141
enum grep_context ctx,
11431142
regmatch_t *pmatch, int eflags)
11441143
{
@@ -1159,7 +1158,8 @@ static int match_next_pattern(struct grep_pat *p, char *bol, char *eol,
11591158
return 1;
11601159
}
11611160

1162-
static int next_match(struct grep_opt *opt, char *bol, char *eol,
1161+
static int next_match(struct grep_opt *opt,
1162+
const char *bol, const char *eol,
11631163
enum grep_context ctx, regmatch_t *pmatch, int eflags)
11641164
{
11651165
struct grep_pat *p;
@@ -1215,7 +1215,8 @@ static void show_line_header(struct grep_opt *opt, const char *name,
12151215
}
12161216
}
12171217

1218-
static void show_line(struct grep_opt *opt, char *bol, char *eol,
1218+
static void show_line(struct grep_opt *opt,
1219+
const char *bol, const char *eol,
12191220
const char *name, unsigned lno, ssize_t cno, char sign)
12201221
{
12211222
int rest = eol - bol;
@@ -1246,7 +1247,6 @@ static void show_line(struct grep_opt *opt, char *bol, char *eol,
12461247
if (opt->color || opt->only_matching) {
12471248
regmatch_t match;
12481249
enum grep_context ctx = GREP_CONTEXT_BODY;
1249-
int ch = *eol;
12501250
int eflags = 0;
12511251

12521252
if (opt->color) {
@@ -1261,7 +1261,6 @@ static void show_line(struct grep_opt *opt, char *bol, char *eol,
12611261
else if (sign == '=')
12621262
line_color = opt->colors[GREP_COLOR_FUNCTION];
12631263
}
1264-
*eol = '\0';
12651264
while (next_match(opt, bol, eol, ctx, &match, eflags)) {
12661265
if (match.rm_so == match.rm_eo)
12671266
break;
@@ -1279,7 +1278,6 @@ static void show_line(struct grep_opt *opt, char *bol, char *eol,
12791278
rest -= match.rm_eo;
12801279
eflags = REG_NOTBOL;
12811280
}
1282-
*eol = ch;
12831281
}
12841282
if (!opt->only_matching) {
12851283
output_color(opt, bol, rest, line_color);
@@ -1307,7 +1305,8 @@ static inline void grep_attr_unlock(void)
13071305
pthread_mutex_unlock(&grep_attr_mutex);
13081306
}
13091307

1310-
static int match_funcname(struct grep_opt *opt, struct grep_source *gs, char *bol, char *eol)
1308+
static int match_funcname(struct grep_opt *opt, struct grep_source *gs,
1309+
const char *bol, const char *eol)
13111310
{
13121311
xdemitconf_t *xecfg = opt->priv;
13131312
if (xecfg && !xecfg->find_func) {
@@ -1334,10 +1333,10 @@ static int match_funcname(struct grep_opt *opt, struct grep_source *gs, char *bo
13341333
}
13351334

13361335
static void show_funcname_line(struct grep_opt *opt, struct grep_source *gs,
1337-
char *bol, unsigned lno)
1336+
const char *bol, unsigned lno)
13381337
{
13391338
while (bol > gs->buf) {
1340-
char *eol = --bol;
1339+
const char *eol = --bol;
13411340

13421341
while (bol > gs->buf && bol[-1] != '\n')
13431342
bol--;
@@ -1356,7 +1355,7 @@ static void show_funcname_line(struct grep_opt *opt, struct grep_source *gs,
13561355
static int is_empty_line(const char *bol, const char *eol);
13571356

13581357
static void show_pre_context(struct grep_opt *opt, struct grep_source *gs,
1359-
char *bol, char *end, unsigned lno)
1358+
const char *bol, const char *end, unsigned lno)
13601359
{
13611360
unsigned cur = lno, from = 1, funcname_lno = 0, orig_from;
13621361
int funcname_needed = !!opt->funcname, comment_needed = 0;
@@ -1376,8 +1375,8 @@ static void show_pre_context(struct grep_opt *opt, struct grep_source *gs,
13761375

13771376
/* Rewind. */
13781377
while (bol > gs->buf && cur > from) {
1379-
char *next_bol = bol;
1380-
char *eol = --bol;
1378+
const char *next_bol = bol;
1379+
const char *eol = --bol;
13811380

13821381
while (bol > gs->buf && bol[-1] != '\n')
13831382
bol--;
@@ -1408,7 +1407,7 @@ static void show_pre_context(struct grep_opt *opt, struct grep_source *gs,
14081407

14091408
/* Back forward. */
14101409
while (cur < lno) {
1411-
char *eol = bol, sign = (cur == funcname_lno) ? '=' : '-';
1410+
const char *eol = bol, sign = (cur == funcname_lno) ? '=' : '-';
14121411

14131412
while (*eol != '\n')
14141413
eol++;
@@ -1436,12 +1435,12 @@ static int should_lookahead(struct grep_opt *opt)
14361435
static int look_ahead(struct grep_opt *opt,
14371436
unsigned long *left_p,
14381437
unsigned *lno_p,
1439-
char **bol_p)
1438+
const char **bol_p)
14401439
{
14411440
unsigned lno = *lno_p;
1442-
char *bol = *bol_p;
1441+
const char *bol = *bol_p;
14431442
struct grep_pat *p;
1444-
char *sp, *last_bol;
1443+
const char *sp, *last_bol;
14451444
regoff_t earliest = -1;
14461445

14471446
for (p = opt->pattern_list; p; p = p->next) {
@@ -1543,8 +1542,8 @@ static int is_empty_line(const char *bol, const char *eol)
15431542

15441543
static int grep_source_1(struct grep_opt *opt, struct grep_source *gs, int collect_hits)
15451544
{
1546-
char *bol;
1547-
char *peek_bol = NULL;
1545+
const char *bol;
1546+
const char *peek_bol = NULL;
15481547
unsigned long left;
15491548
unsigned lno = 1;
15501549
unsigned last_hit = 0;
@@ -1626,7 +1625,7 @@ static int grep_source_1(struct grep_opt *opt, struct grep_source *gs, int colle
16261625
bol = gs->buf;
16271626
left = gs->size;
16281627
while (left) {
1629-
char *eol, ch;
1628+
const char *eol;
16301629
int hit;
16311630
ssize_t cno;
16321631
ssize_t col = -1, icol = -1;
@@ -1647,14 +1646,11 @@ static int grep_source_1(struct grep_opt *opt, struct grep_source *gs, int colle
16471646
&& look_ahead(opt, &left, &lno, &bol))
16481647
break;
16491648
eol = end_of_line(bol, &left);
1650-
ch = *eol;
1651-
*eol = 0;
16521649

16531650
if ((ctx == GREP_CONTEXT_HEAD) && (eol == bol))
16541651
ctx = GREP_CONTEXT_BODY;
16551652

16561653
hit = match_line(opt, bol, eol, &col, &icol, ctx, collect_hits);
1657-
*eol = ch;
16581654

16591655
if (collect_hits)
16601656
goto next_line;
@@ -1713,7 +1709,7 @@ static int grep_source_1(struct grep_opt *opt, struct grep_source *gs, int colle
17131709
}
17141710
if (show_function && (!peek_bol || peek_bol < bol)) {
17151711
unsigned long peek_left = left;
1716-
char *peek_eol = eol;
1712+
const char *peek_eol = eol;
17171713

17181714
/*
17191715
* Trailing empty lines are not interesting.
@@ -1825,7 +1821,8 @@ int grep_source(struct grep_opt *opt, struct grep_source *gs)
18251821
return grep_source_1(opt, gs, 0);
18261822
}
18271823

1828-
static void grep_source_init_buf(struct grep_source *gs, char *buf,
1824+
static void grep_source_init_buf(struct grep_source *gs,
1825+
const char *buf,
18291826
unsigned long size)
18301827
{
18311828
gs->type = GREP_SOURCE_BUF;
@@ -1837,7 +1834,7 @@ static void grep_source_init_buf(struct grep_source *gs, char *buf,
18371834
gs->identifier = NULL;
18381835
}
18391836

1840-
int grep_buffer(struct grep_opt *opt, char *buf, unsigned long size)
1837+
int grep_buffer(struct grep_opt *opt, const char *buf, unsigned long size)
18411838
{
18421839
struct grep_source gs;
18431840
int r;
@@ -1889,7 +1886,9 @@ void grep_source_clear_data(struct grep_source *gs)
18891886
switch (gs->type) {
18901887
case GREP_SOURCE_FILE:
18911888
case GREP_SOURCE_OID:
1892-
FREE_AND_NULL(gs->buf);
1889+
/* these types own the buffer */
1890+
free((char *)gs->buf);
1891+
gs->buf = NULL;
18931892
gs->size = 0;
18941893
break;
18951894
case GREP_SOURCE_BUF:

grep.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ void append_grep_pattern(struct grep_opt *opt, const char *pat, const char *orig
189189
void append_header_grep_pattern(struct grep_opt *, enum grep_header_field, const char *);
190190
void compile_grep_patterns(struct grep_opt *opt);
191191
void free_grep_patterns(struct grep_opt *opt);
192-
int grep_buffer(struct grep_opt *opt, char *buf, unsigned long size);
192+
int grep_buffer(struct grep_opt *opt, const char *buf, unsigned long size);
193193

194194
struct grep_source {
195195
char *name;
@@ -202,7 +202,7 @@ struct grep_source {
202202
void *identifier;
203203
struct repository *repo; /* if GREP_SOURCE_OID */
204204

205-
char *buf;
205+
const char *buf;
206206
unsigned long size;
207207

208208
char *path; /* for attribute lookups */

0 commit comments

Comments
 (0)