Skip to content

const-correctness in grep.c

Jeff King git 1 version In review

Get series with b4
b4 am [email protected]

Versions newest first

  1. v1 Latest5 patches 26 repliesIn review

Patches in v1 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/5grep: stop modifying buffer in strip_timestampgrep.c+3 -1010 replies
2/5grep: stop modifying buffer in show_line()grep.c+0 -33 replies
3/5grep: stop modifying buffer in grep_source_1()grep.c+1 -40 replies
4/5grep: mark "haystack" buffers as constgrep.c+35 -262 replies
5/5grep: store grep_source buffer as constgrep.c grep.h+8 -50 replies

Reviews trailers per patch

None of the 5 patches has a Reviewed-by or Acked-by of their own; the series has one for all of them.

PatchTaylor Blau
SeriesR-b
1/5
2/5
3/5
4/5
5/5

Trailers given in replies are filled; those carried in the patches from earlier versions are outlined with dashes.

Read the v1 thread

Cover letter [PATCH 0/5] const-correctness in grep.c

While discussing [1], I noticed that the grep code mostly takes non-const buffers, even though it is conceptually a read-only operation to search in them. The culprit is a handful of spots that temporarily tie off NUL-terminated strings by overwriting a byte of the buffer and then restoring it. But I think we no longer need to do so these days, now that we have a regexec_buf() that can take a ptr/size pair.

The first three patches are a bit repetitive, but I broke them up individually because they're the high-risk part. I.e., if my assumptions about needing the NUL are wrong, it could introduce a bug. But based on my reading of the code, plus running the test suite with ASan/UBSan, I feel reasonably confident.

The last two are the bigger cleanups, but should obviously avoid any behavior changes.

[1/5]: grep: stop modifying buffer in strip_timestamp [2/5]: grep: stop modifying buffer in show_line() [3/5]: grep: stop modifying buffer in grep_source_1() [4/5]: grep: mark "haystack" buffers as const [5/5]: grep: store grep_source buffer as const

 grep.c | 87 +++++++++++++++++++++++++++++-----------------------------
 grep.h |  4 +--
 2 files changed, 45 insertions(+), 46 deletions(-)

-Peff

[1] https://lore.kernel.org/git/[email protected]/