Change pcre2grep line number and count variables to unsigned long int.

This commit is contained in:
Philip.Hazel 2017-12-08 10:25:49 +00:00
parent 77513bfc04
commit 1a81b738fe
2 changed files with 22 additions and 16 deletions

View File

@ -60,6 +60,11 @@ in a given mode, it was also expected that at least one mode is available.
This is fixed and pcre2_jit_match returns with PCRE2_ERROR_JIT_BADOPTION This is fixed and pcre2_jit_match returns with PCRE2_ERROR_JIT_BADOPTION
when the pattern is not optimized by JIT at all. when the pattern is not optimized by JIT at all.
16. The line number and related variables such as match counts in pcre2grep
were all int variables, causing overflow when files with more than 2147483647
lines were processed (assuming 32-bit ints). They have all been changed to
unsigned long ints.
Version 10.30 14-August-2017 Version 10.30 14-August-2017
---------------------------- ----------------------------

View File

@ -196,8 +196,9 @@ static int bufthird = PCRE2GREP_BUFSIZE;
static int max_bufthird = PCRE2GREP_MAX_BUFSIZE; static int max_bufthird = PCRE2GREP_MAX_BUFSIZE;
static int bufsize = 3*PCRE2GREP_BUFSIZE; static int bufsize = 3*PCRE2GREP_BUFSIZE;
static int endlinetype; static int endlinetype;
static int total_count = 0;
static int counts_printed = 0; static unsigned long int total_count = 0;
static unsigned long int counts_printed = 0;
#ifdef WIN32 #ifdef WIN32
static int dee_action = dee_SKIP; static int dee_action = dee_SKIP;
@ -1606,8 +1607,8 @@ Returns: nothing
*/ */
static void static void
do_after_lines(int lastmatchnumber, char *lastmatchrestart, char *endptr, do_after_lines(unsigned long int lastmatchnumber, char *lastmatchrestart,
const char *printname) char *endptr, const char *printname)
{ {
if (after_context > 0 && lastmatchnumber > 0) if (after_context > 0 && lastmatchnumber > 0)
{ {
@ -1618,7 +1619,7 @@ if (after_context > 0 && lastmatchnumber > 0)
char *pp = end_of_line(lastmatchrestart, endptr, &ellength); char *pp = end_of_line(lastmatchrestart, endptr, &ellength);
if (ellength == 0 && pp == main_buffer + bufsize) break; if (ellength == 0 && pp == main_buffer + bufsize) break;
if (printname != NULL) fprintf(stdout, "%s-", printname); if (printname != NULL) fprintf(stdout, "%s-", printname);
if (number) fprintf(stdout, "%d-", lastmatchnumber++); if (number) fprintf(stdout, "%lu-", lastmatchnumber++);
FWRITE_IGNORE(lastmatchrestart, 1, pp - lastmatchrestart, stdout); FWRITE_IGNORE(lastmatchrestart, 1, pp - lastmatchrestart, stdout);
lastmatchrestart = pp; lastmatchrestart = pp;
count++; count++;
@ -2309,10 +2310,10 @@ static int
pcre2grep(void *handle, int frtype, const char *filename, const char *printname) pcre2grep(void *handle, int frtype, const char *filename, const char *printname)
{ {
int rc = 1; int rc = 1;
int linenumber = 1;
int lastmatchnumber = 0;
int count = 0;
int filepos = 0; int filepos = 0;
unsigned long int linenumber = 1;
unsigned long int lastmatchnumber = 0;
unsigned long int count = 0;
char *lastmatchrestart = NULL; char *lastmatchrestart = NULL;
char *ptr = main_buffer; char *ptr = main_buffer;
char *endptr; char *endptr;
@ -2400,7 +2401,7 @@ while (ptr < endptr)
if (new_buffer == NULL) if (new_buffer == NULL)
{ {
fprintf(stderr, fprintf(stderr,
"pcre2grep: line %d%s%s is too long for the internal buffer\n" "pcre2grep: line %lu%s%s is too long for the internal buffer\n"
"pcre2grep: not enough memory to increase the buffer size to %d\n", "pcre2grep: not enough memory to increase the buffer size to %d\n",
linenumber, linenumber,
(filename == NULL)? "" : " of file ", (filename == NULL)? "" : " of file ",
@ -2430,7 +2431,7 @@ while (ptr < endptr)
else else
{ {
fprintf(stderr, fprintf(stderr,
"pcre2grep: line %d%s%s is too long for the internal buffer\n" "pcre2grep: line %lu%s%s is too long for the internal buffer\n"
"pcre2grep: the maximum buffer size is %d\n" "pcre2grep: the maximum buffer size is %d\n"
"pcre2grep: use the --max-buffer-size option to change it\n", "pcre2grep: use the --max-buffer-size option to change it\n",
linenumber, linenumber,
@ -2562,7 +2563,7 @@ while (ptr < endptr)
size_t oldstartoffset; size_t oldstartoffset;
if (printname != NULL) fprintf(stdout, "%s:", printname); if (printname != NULL) fprintf(stdout, "%s:", printname);
if (number) fprintf(stdout, "%d:", linenumber); if (number) fprintf(stdout, "%lu:", linenumber);
/* Handle --line-offsets */ /* Handle --line-offsets */
@ -2682,7 +2683,7 @@ while (ptr < endptr)
{ {
char *pp = lastmatchrestart; char *pp = lastmatchrestart;
if (printname != NULL) fprintf(stdout, "%s-", printname); if (printname != NULL) fprintf(stdout, "%s-", printname);
if (number) fprintf(stdout, "%d-", lastmatchnumber++); if (number) fprintf(stdout, "%lu-", lastmatchnumber++);
pp = end_of_line(pp, endptr, &ellength); pp = end_of_line(pp, endptr, &ellength);
FWRITE_IGNORE(lastmatchrestart, 1, pp - lastmatchrestart, stdout); FWRITE_IGNORE(lastmatchrestart, 1, pp - lastmatchrestart, stdout);
lastmatchrestart = pp; lastmatchrestart = pp;
@ -2722,7 +2723,7 @@ while (ptr < endptr)
int ellength; int ellength;
char *pp = p; char *pp = p;
if (printname != NULL) fprintf(stdout, "%s-", printname); if (printname != NULL) fprintf(stdout, "%s-", printname);
if (number) fprintf(stdout, "%d-", linenumber - linecount--); if (number) fprintf(stdout, "%lu-", linenumber - linecount--);
pp = end_of_line(pp, endptr, &ellength); pp = end_of_line(pp, endptr, &ellength);
FWRITE_IGNORE(p, 1, pp - p, stdout); FWRITE_IGNORE(p, 1, pp - p, stdout);
p = pp; p = pp;
@ -2736,7 +2737,7 @@ while (ptr < endptr)
endhyphenpending = TRUE; endhyphenpending = TRUE;
if (printname != NULL) fprintf(stdout, "%s:", printname); if (printname != NULL) fprintf(stdout, "%s:", printname);
if (number) fprintf(stdout, "%d:", linenumber); if (number) fprintf(stdout, "%lu:", linenumber);
/* This extra option, for Jeffrey Friedl's debugging requirements, /* This extra option, for Jeffrey Friedl's debugging requirements,
replaces the matched string, or a specific captured string if it exists, replaces the matched string, or a specific captured string if it exists,
@ -2918,7 +2919,7 @@ if (count_only && !quiet)
{ {
if (printname != NULL && filenames != FN_NONE) if (printname != NULL && filenames != FN_NONE)
fprintf(stdout, "%s:", printname); fprintf(stdout, "%s:", printname);
fprintf(stdout, "%d" STDOUT_NL, count); fprintf(stdout, "%lu" STDOUT_NL, count);
counts_printed++; counts_printed++;
} }
} }
@ -4196,7 +4197,7 @@ if (show_total_count && counts_printed != 1 && filenames != FN_NOMATCH_ONLY)
{ {
if (counts_printed != 0 && filenames >= FN_DEFAULT) if (counts_printed != 0 && filenames >= FN_DEFAULT)
fprintf(stdout, "TOTAL:"); fprintf(stdout, "TOTAL:");
fprintf(stdout, "%d" STDOUT_NL, total_count); fprintf(stdout, "%lu" STDOUT_NL, total_count);
} }
EXIT: EXIT: