From: Elia Pinto Date: Sun, 01 Nov 2026 08:44:56 GMT Subject: [PATCH 1/2] grep: use size_t for the size of buffers to search Message-ID: In-Reply-To: struct grep_source tracks the size of the search buffer in an "unsigned long", while grep_source_load_file() reads worktree files into memory using size_t. On platforms where "long" is 32 bits but size_t is 64 bits (such as 64-bit Windows), processing a file of 4GiB or more truncates the size, causing "git grep" to search only the first "size mod 2^32" bytes and silently miss matches beyond that threshold. With gs->size truncated to 16 bits, "git grep" and "git grep --no-index" find nothing in a 100KiB file whose last line matches, while grepping the same file as a blob finds it. Use size_t for struct grep_source.size and across the internal matching engine functions in grep.c and grep.h. Additionally, remove the cast_size_t_to_ulong() call in grep_source_load_oid(), allowing grepping blobs of 4GiB or more to succeed instead of aborting. Triggering this requires processing 4GiB or more of input on a platform where "long" is 32 bits but size_t is 64 bits, so no test is added. Signed-off-by: Elia Pinto --- grep.c | 16 ++++++++-------- grep.h | 4 ++-- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/grep.c b/grep.c index 1d75d31421..a8c01b4737 100644 --- a/grep.c +++ b/grep.c @@ -864,9 +864,9 @@ void free_grep_patterns(struct grep_opt *opt) free_pattern_expr(opt->pattern_expression); } -static const char *end_of_line(const char *cp, unsigned long *left) +static const char *end_of_line(const char *cp, size_t *left) { - unsigned long l = *left; + size_t l = *left; while (l && *cp != '\n') { l--; cp++; @@ -1454,7 +1454,7 @@ static int should_lookahead(struct grep_opt *opt) } static int look_ahead(struct grep_opt *opt, - unsigned long *left_p, + size_t *left_p, unsigned *lno_p, const char **bol_p) { @@ -1567,7 +1567,7 @@ static int grep_source_1(struct grep_opt *opt, struct grep_source *gs, int colle { const char *bol; const char *peek_bol = NULL; - unsigned long left; + size_t left; unsigned lno = 1; unsigned last_hit = 0; int binary_match_only = 0; @@ -1735,7 +1735,7 @@ static int grep_source_1(struct grep_opt *opt, struct grep_source *gs, int colle goto next_line; } if (show_function && (!peek_bol || peek_bol < bol)) { - unsigned long peek_left = left; + size_t peek_left = left; const char *peek_eol = eol; /* @@ -1854,7 +1854,7 @@ int grep_source(struct grep_opt *opt, struct grep_source *gs) static void grep_source_init_buf(struct grep_source *gs, const char *buf, - unsigned long size) + size_t size) { gs->type = GREP_SOURCE_BUF; gs->name = NULL; @@ -1865,7 +1865,7 @@ static void grep_source_init_buf(struct grep_source *gs, gs->identifier = NULL; } -int grep_buffer(struct grep_opt *opt, const char *buf, unsigned long size) +int grep_buffer(struct grep_opt *opt, const char *buf, size_t size) { struct grep_source gs; int r; @@ -1935,7 +1935,7 @@ static int grep_source_load_oid(struct grep_source *gs) gs->buf = odb_read_object(gs->repo->objects, gs->identifier, &type, &size_st); - gs->size = cast_size_t_to_ulong(size_st); + gs->size = size_st; if (!gs->buf) return error(_("'%s': unable to read %s"), gs->name, diff --git a/grep.h b/grep.h index 13e26a9318..0bd705dfc0 100644 --- a/grep.h +++ b/grep.h @@ -212,7 +212,7 @@ void append_grep_pattern(struct grep_opt *opt, const char *pat, const char *orig void append_header_grep_pattern(struct grep_opt *, enum grep_header_field, const char *); void compile_grep_patterns(struct grep_opt *opt); void free_grep_patterns(struct grep_opt *opt); -int grep_buffer(struct grep_opt *opt, const char *buf, unsigned long size); +int grep_buffer(struct grep_opt *opt, const char *buf, size_t size); /* The field parameter is only used to filter header patterns * (where appropriate). If filtering isn't desirable @@ -235,7 +235,7 @@ struct grep_source { struct repository *repo; /* if GREP_SOURCE_OID */ const char *buf; - unsigned long size; + size_t size; char *path; /* for attribute lookups */ struct userdiff_driver *driver; -- 2.53.0