pcre2grep: track the file position in PCRE2_SIZE, not int by ManoharPaturi · Pull Request #1020 · PCRE2Project/pcre2 · GitHub
Skip to content

pcre2grep: track the file position in PCRE2_SIZE, not int - #1020

Merged
NWilson merged 3 commits into
PCRE2Project:mainfrom
ManoharPaturi:fix-grep-filepos-width
Sep 30, 2026
Merged

NWilson merged 3 commits into
PCRE2Project:mainfrom
ManoharPaturi:fix-grep-filepos-width

Conversation

@ManoharPaturi

Copy link
Copy Markdown
Contributor

Problem

pcre2grep tracks the input file position in an int (src/pcre2grep.c: int filepos = 0;) and accumulates the per-line advance into it with narrowing casts:

filepos += (int)lineadvance;
filepos += (int)(linelength + endlinelength);

On files larger than 2GiB the counter overflows: the -o byte-offset output prints wrapped (negative) offsets via %d, and the restart path computes fseek(handle, stream_start + (long int)filepos, SEEK_SET) from the wrapped value. This follows the same multiline-advance code that #956 recently touched.

Fix

Track filepos as PCRE2_SIZE, drop the narrowing casts, and print the offsets as unsigned long long. No behavioral change below 2GiB.

Verification

Full ctest suite passes; the arithmetic change is mechanical (no test fixture can practically carry a >2GiB file).

Copilot AI balanced review requested due to automatic review settings September 29, 2026 15:59

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Comment thread src/pcre2grep.c Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can't be right. You're still casting to int: (int)start but now printing as llu.

The correct way to print a size_t is using %zu - we have the macro SIZ_FORM for that.

Comment thread src/pcre2grep.c
@NWilson

NWilson commented Sep 30, 2026

Copy link
Copy Markdown
Member

Now you've expanded filepos to size_t, it makes it more obvious that the cast fseek(..., (long)filepos, ...) is rather unfortunate as well.

I think you're right that the correct unit is size_t. The fact that fseek/ftell work in longs shouldn't drive anything.

I also dislike the fact the linelength is unsigned and endlinelength is signed. That's a quite unfortunate implicit conversion for linelength + endlinelength on 32-bit systems. You don't need to fix that though, unless you want to!

@NWilson

NWilson commented Sep 30, 2026 •

Copy link
Copy Markdown
Member

I've revised my opinion!

Clearly filepos can't be correct as PCRE2_SIZE (=size_t). On a 32-bit system, you can certainly get files larger than 4GiB. It should be off_t, and there is a mess of platform macros to ensure that off_t becomes 64-bit.

If you don't feel brave enough to tackle the off_t mess in Autoconf & CMake, don't worry about it. We can merge this with a simple int64_t filepos.

@ManoharPaturi

Copy link
Copy Markdown
Contributor Author

Thanks for the review. Switched to int64_t and adjusted the printf format, pushed. The off_t migration sounds like its own project, happy to leave that for later.

@NWilson

NWilson commented Sep 30, 2026

Copy link
Copy Markdown
Member

Thanks! The first if (line_offsets) fprintf still looks like it's using mismatched types though in the format string.

@NWilson

NWilson commented Sep 30, 2026

Copy link
Copy Markdown
Member

See also #1024 for the future work.

@ManoharPaturi

Copy link
Copy Markdown
Contributor Author

Good catch, fixed that one too and pushed. Both output paths now use long long consistently.

@NWilson NWilson left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great! Running CI

@ManoharPaturi

Copy link
Copy Markdown
Contributor Author

@NWilson
NWilson merged commit f4d766d into PCRE2Project:main Sep 30, 2026
40 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants