@@ -49,7 +49,9 @@ POSSIBILITY OF SUCH DAMAGE.
49
49
#endif
50
50
51
51
#include <ctype.h>
52
+ #include <limits.h>
52
53
#include <locale.h>
54
+ #include <stddef.h>
53
55
#include <stdio.h>
54
56
#include <string.h>
55
57
#include <stdlib.h>
@@ -2586,7 +2588,7 @@ return result != 0;
2586
2588
* Read a portion of the file into buffer *
2587
2589
*************************************************/
2588
2590
2589
- static PCRE2_SIZE
2591
+ static ptrdiff_t
2590
2592
fill_buffer (void * handle , int frtype , char * buffer , PCRE2_SIZE length ,
2591
2593
BOOL input_line_buffered )
2592
2594
{
@@ -2595,13 +2597,15 @@ PCRE2_SIZE nread;
2595
2597
2596
2598
#ifdef SUPPORT_LIBZ
2597
2599
if (frtype == FR_LIBZ )
2598
- return gzread ((gzFile )handle , buffer , length );
2600
+ return gzread ((gzFile )handle , buffer ,
2601
+ (length > UINT_MAX )? UINT_MAX : (unsigned )length );
2599
2602
else
2600
2603
#endif
2601
2604
2602
2605
#ifdef SUPPORT_LIBBZ2
2603
2606
if (frtype == FR_LIBBZ2 )
2604
- return (PCRE2_SIZE )BZ2_bzread ((BZFILE * )handle , buffer , length );
2607
+ return BZ2_bzread ((BZFILE * )handle , buffer ,
2608
+ (length > UINT_MAX )? UINT_MAX : (unsigned )length );
2605
2609
else
2606
2610
#endif
2607
2611
@@ -2614,7 +2618,7 @@ if (nread > 0) VALGRIND_MAKE_MEM_DEFINED_IF_ADDRESSABLE(buffer, nread);
2614
2618
if (nread < length ) VALGRIND_MAKE_MEM_UNDEFINED (buffer + nread , length - nread );
2615
2619
#endif
2616
2620
2617
- return nread ;
2621
+ return ( ptrdiff_t ) nread ;
2618
2622
}
2619
2623
2620
2624
@@ -2659,6 +2663,7 @@ char *lastmatchrestart = main_buffer;
2659
2663
char * ptr = main_buffer ;
2660
2664
char * endptr ;
2661
2665
PCRE2_SIZE bufflength ;
2666
+ ptrdiff_t buffrc ;
2662
2667
BOOL binary = FALSE;
2663
2668
BOOL endhyphenpending = FALSE;
2664
2669
BOOL lines_printed = FALSE;
@@ -2684,13 +2689,17 @@ if (frtype != FR_LIBZ && frtype != FR_LIBBZ2)
2684
2689
}
2685
2690
else input_line_buffered = FALSE;
2686
2691
2687
- bufflength = fill_buffer (handle , frtype , main_buffer , bufsize ,
2692
+ buffrc = fill_buffer (handle , frtype , main_buffer , bufsize ,
2688
2693
input_line_buffered );
2689
2694
2695
+ #if defined SUPPORT_LIBZ
2696
+ if (frtype == FR_LIBZ && buffrc < 0 ) return 3 ;
2697
+ #endif
2690
2698
#ifdef SUPPORT_LIBBZ2
2691
- if (frtype == FR_LIBBZ2 && ( int ) bufflength < 0 ) return 3 ; /* Gotcha: bufflength is PCRE2_SIZE */
2699
+ if (frtype == FR_LIBBZ2 && buffrc < 0 ) return 3 ;
2692
2700
#endif
2693
2701
2702
+ bufflength = (PCRE2_SIZE )buffrc ;
2694
2703
endptr = main_buffer + bufflength ;
2695
2704
2696
2705
/* Unless binary-files=text, see if we have a binary file. This uses the same
@@ -2788,8 +2797,17 @@ while (ptr < endptr)
2788
2797
/* Read more data into the buffer and then try to find the line ending
2789
2798
again. */
2790
2799
2791
- bufflength + = fill_buffer (handle , frtype , main_buffer + bufflength ,
2800
+ buffrc = fill_buffer (handle , frtype , main_buffer + bufflength ,
2792
2801
bufsize - bufflength , input_line_buffered );
2802
+
2803
+ #if defined SUPPORT_LIBZ
2804
+ if (frtype == FR_LIBZ && buffrc < 0 ) return 3 ;
2805
+ #endif
2806
+ #ifdef SUPPORT_LIBBZ2
2807
+ if (frtype == FR_LIBBZ2 && buffrc < 0 ) return 3 ;
2808
+ #endif
2809
+
2810
+ bufflength += (PCRE2_SIZE )buffrc ;
2793
2811
endptr = main_buffer + bufflength ;
2794
2812
continue ;
2795
2813
}
@@ -3260,8 +3278,17 @@ while (ptr < endptr)
3260
3278
(void )memmove (main_buffer , main_buffer + bufthird , 2 * bufthird );
3261
3279
ptr -= bufthird ;
3262
3280
3263
- bufflength = 2 * bufthird + fill_buffer (handle , frtype ,
3264
- main_buffer + 2 * bufthird , bufthird , input_line_buffered );
3281
+ buffrc = fill_buffer (handle , frtype , main_buffer + 2 * bufthird , bufthird ,
3282
+ input_line_buffered );
3283
+
3284
+ #if defined SUPPORT_LIBZ
3285
+ if (frtype == FR_LIBZ && buffrc < 0 ) return 3 ;
3286
+ #endif
3287
+ #ifdef SUPPORT_LIBBZ2
3288
+ if (frtype == FR_LIBBZ2 && buffrc < 0 ) return 3 ;
3289
+ #endif
3290
+
3291
+ bufflength = 2 * bufthird + (PCRE2_SIZE )buffrc ;
3265
3292
endptr = main_buffer + bufflength ;
3266
3293
3267
3294
/* Adjust any last match point */
@@ -3625,7 +3652,18 @@ rc = pcre2grep(handle, frtype, pathname, (filenames > FN_DEFAULT ||
3625
3652
3626
3653
#ifdef SUPPORT_LIBZ
3627
3654
if (frtype == FR_LIBZ )
3655
+ {
3656
+ if (rc == 3 )
3657
+ {
3658
+ int errnum ;
3659
+ const char * err = gzerror (ingz , & errnum );
3660
+ if (!silent )
3661
+ fprintf (stderr , "pcre2grep: Failed to read %s using zlib: %s\n" ,
3662
+ pathname , err );
3663
+ rc = 2 ; /* The normal "something went wrong" code */
3664
+ }
3628
3665
gzclose (ingz );
3666
+ }
3629
3667
else
3630
3668
#endif
3631
3669
@@ -4275,9 +4313,9 @@ for (i = 1; i < argc; i++)
4275
4313
else
4276
4314
{
4277
4315
unsigned long int n = decode_number (option_data , op , longop );
4278
- if (op -> type == OP_U32NUMBER ) * ((uint32_t * )op -> dataptr ) = n ;
4316
+ if (op -> type == OP_U32NUMBER ) * ((uint32_t * )op -> dataptr ) = ( int ) n ;
4279
4317
else if (op -> type == OP_SIZE ) * ((PCRE2_SIZE * )op -> dataptr ) = n ;
4280
- else * ((int * )op -> dataptr ) = n ;
4318
+ else * ((int * )op -> dataptr ) = ( int ) n ;
4281
4319
}
4282
4320
}
4283
4321
0 commit comments