grep: mark "haystack" buffers as const

When we're grepping in a buffer, we don't need to modify it. So we can take "const char *" buffers, rather than "char *". This can avoid some awkward casts in our callers, and make our expectations more clear (we will not take ownership of the memory, nor will we ever write to it). These spots don't all necessarily have to be converted at the same time, but some of them are dependent on others, because we pass pointers-to-pointers in a few cases. And none of this should change any behavior, since we're just adding "const" qualifiers (and likewise, the compiler will let us know if we missed any spots). So it's relatively low-risk to just do this all at once. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Sep 20, 2021 at 23:49 UTC 1a845fbc48f2613c0daab717ee934e066e65723d
1 file changed +35 -26
grep.c
+35 -26
@@ -867,7 +867,7 @@ void free_grep_patterns(struct grep_opt *opt)
867 free_pattern_expr(opt->pattern_expression);
868 }
869
870 -static char *end_of_line(char *cp, unsigned long *left)
870 +static const char *end_of_line(const char *cp, unsigned long *left)
871 {
872 unsigned long l = *left;
873 while (l && *cp != '\n') {
@@ -908,7 +908,8 @@ static void show_name(struct grep_opt *opt, const char *name)
908 opt->output(opt, opt->null_following_name ? "\0" : "\n", 1);
909 }
910
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,
913 regmatch_t *match, int eflags)
914 {
915 int hit;
@@ -922,9 +923,9 @@ static int patmatch(struct grep_pat *p, char *line, char *eol,
923 return hit;
924 }
925
925 -static void strip_timestamp(char *bol, char **eol_p)
926 +static void strip_timestamp(const char *bol, const char **eol_p)
927 {
927 - char *eol = *eol_p;
928 + const char *eol = *eol_p;
929
930 while (bol < --eol) {
931 if (*eol != '>')
@@ -943,7 +944,8 @@ static struct {
944 { "reflog ", 7 },
945 };
946
946 -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,
949 enum grep_context ctx,
950 regmatch_t *pmatch, int eflags)
951 {
@@ -1023,8 +1025,9 @@ static int match_one_pattern(struct grep_pat *p, char *bol, char *eol,
1025 return hit;
1026 }
1027
1026 -static int match_expr_eval(struct grep_opt *opt, struct grep_expr *x, char *bol,
1027 - 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,
1031 ssize_t *icol, int collect_hits)
1032 {
1033 int h = 0;
@@ -1091,7 +1094,8 @@ static int match_expr_eval(struct grep_opt *opt, struct grep_expr *x, char *bol,
1094 return h;
1095 }
1096
1094 -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,
1099 enum grep_context ctx, ssize_t *col,
1100 ssize_t *icol, int collect_hits)
1101 {
@@ -1099,7 +1103,8 @@ static int match_expr(struct grep_opt *opt, char *bol, char *eol,
1103 return match_expr_eval(opt, x, bol, eol, ctx, col, icol, collect_hits);
1104 }
1105
1102 -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,
1108 ssize_t *col, ssize_t *icol,
1109 enum grep_context ctx, int collect_hits)
1110 {
@@ -1131,7 +1136,8 @@ static int match_line(struct grep_opt *opt, char *bol, char *eol,
1136 return hit;
1137 }
1138
1134 -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,
1141 enum grep_context ctx,
1142 regmatch_t *pmatch, int eflags)
1143 {
@@ -1152,7 +1158,8 @@ static int match_next_pattern(struct grep_pat *p, char *bol, char *eol,
1158 return 1;
1159 }
1160
1155 -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,
1163 enum grep_context ctx, regmatch_t *pmatch, int eflags)
1164 {
1165 struct grep_pat *p;
@@ -1208,7 +1215,8 @@ static void show_line_header(struct grep_opt *opt, const char *name,
1215 }
1216 }
1217
1211 -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,
1220 const char *name, unsigned lno, ssize_t cno, char sign)
1221 {
1222 int rest = eol - bol;
@@ -1297,7 +1305,8 @@ static inline void grep_attr_unlock(void)
1305 pthread_mutex_unlock(&grep_attr_mutex);
1306 }
1307
1300 -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)
1310 {
1311 xdemitconf_t *xecfg = opt->priv;
1312 if (xecfg && !xecfg->find_func) {
@@ -1324,10 +1333,10 @@ static int match_funcname(struct grep_opt *opt, struct grep_source *gs, char *bo
1333 }
1334
1335 static void show_funcname_line(struct grep_opt *opt, struct grep_source *gs,
1327 - char *bol, unsigned lno)
1336 + const char *bol, unsigned lno)
1337 {
1338 while (bol > gs->buf) {
1330 - char *eol = --bol;
1339 + const char *eol = --bol;
1340
1341 while (bol > gs->buf && bol[-1] != '\n')
1342 bol--;
@@ -1346,7 +1355,7 @@ static void show_funcname_line(struct grep_opt *opt, struct grep_source *gs,
1355 static int is_empty_line(const char *bol, const char *eol);
1356
1357 static void show_pre_context(struct grep_opt *opt, struct grep_source *gs,
1349 - char *bol, char *end, unsigned lno)
1358 + const char *bol, const char *end, unsigned lno)
1359 {
1360 unsigned cur = lno, from = 1, funcname_lno = 0, orig_from;
1361 int funcname_needed = !!opt->funcname, comment_needed = 0;
@@ -1366,8 +1375,8 @@ static void show_pre_context(struct grep_opt *opt, struct grep_source *gs,
1375
1376 /* Rewind. */
1377 while (bol > gs->buf && cur > from) {
1369 - char *next_bol = bol;
1370 - char *eol = --bol;
1378 + const char *next_bol = bol;
1379 + const char *eol = --bol;
1380
1381 while (bol > gs->buf && bol[-1] != '\n')
1382 bol--;
@@ -1398,7 +1407,7 @@ static void show_pre_context(struct grep_opt *opt, struct grep_source *gs,
1407
1408 /* Back forward. */
1409 while (cur < lno) {
1401 - char *eol = bol, sign = (cur == funcname_lno) ? '=' : '-';
1410 + const char *eol = bol, sign = (cur == funcname_lno) ? '=' : '-';
1411
1412 while (*eol != '\n')
1413 eol++;
@@ -1426,12 +1435,12 @@ static int should_lookahead(struct grep_opt *opt)
1435 static int look_ahead(struct grep_opt *opt,
1436 unsigned long *left_p,
1437 unsigned *lno_p,
1429 - char **bol_p)
1438 + const char **bol_p)
1439 {
1440 unsigned lno = *lno_p;
1432 - char *bol = *bol_p;
1441 + const char *bol = *bol_p;
1442 struct grep_pat *p;
1434 - char *sp, *last_bol;
1443 + const char *sp, *last_bol;
1444 regoff_t earliest = -1;
1445
1446 for (p = opt->pattern_list; p; p = p->next) {
@@ -1533,8 +1542,8 @@ static int is_empty_line(const char *bol, const char *eol)
1542
1543 static int grep_source_1(struct grep_opt *opt, struct grep_source *gs, int collect_hits)
1544 {
1536 - char *bol;
1537 - char *peek_bol = NULL;
1545 + const char *bol;
1546 + const char *peek_bol = NULL;
1547 unsigned long left;
1548 unsigned lno = 1;
1549 unsigned last_hit = 0;
@@ -1616,7 +1625,7 @@ static int grep_source_1(struct grep_opt *opt, struct grep_source *gs, int colle
1625 bol = gs->buf;
1626 left = gs->size;
1627 while (left) {
1619 - char *eol;
1628 + const char *eol;
1629 int hit;
1630 ssize_t cno;
1631 ssize_t col = -1, icol = -1;
@@ -1700,7 +1709,7 @@ static int grep_source_1(struct grep_opt *opt, struct grep_source *gs, int colle
1709 }
1710 if (show_function && (!peek_bol || peek_bol < bol)) {
1711 unsigned long peek_left = left;
1703 - char *peek_eol = eol;
1712 + const char *peek_eol = eol;
1713
1714 /*
1715 * Trailing empty lines are not interesting.