@@ -519,7 +519,7 @@ PHP_FUNCTION(file_get_contents)
519
519
char * contents ;
520
520
zend_bool use_include_path = 0 ;
521
521
php_stream * stream ;
522
- int len ;
522
+ long len ;
523
523
long offset = -1 ;
524
524
long maxlen = PHP_STREAM_COPY_ALL ;
525
525
zval * zcontext = NULL ;
@@ -551,6 +551,10 @@ PHP_FUNCTION(file_get_contents)
551
551
}
552
552
553
553
if ((len = php_stream_copy_to_mem (stream , & contents , maxlen , 0 )) > 0 ) {
554
+ if (len > INT_MAX ) {
555
+ php_error_docref (NULL TSRMLS_CC , E_WARNING , "content truncated from %ld to %d bytes" , len , INT_MAX );
556
+ len = INT_MAX ;
557
+ }
554
558
RETVAL_STRINGL (contents , len , 0 );
555
559
} else if (len == 0 ) {
556
560
RETVAL_EMPTY_STRING ();
@@ -570,7 +574,7 @@ PHP_FUNCTION(file_put_contents)
570
574
char * filename ;
571
575
int filename_len ;
572
576
zval * data ;
573
- int numbytes = 0 ;
577
+ long numbytes = 0 ;
574
578
long flags = 0 ;
575
579
zval * zcontext = NULL ;
576
580
php_stream_context * context = NULL ;
@@ -622,6 +626,10 @@ PHP_FUNCTION(file_put_contents)
622
626
if (php_stream_copy_to_stream_ex (srcstream , stream , PHP_STREAM_COPY_ALL , & len ) != SUCCESS ) {
623
627
numbytes = -1 ;
624
628
} else {
629
+ if (len > LONG_MAX ) {
630
+ php_error_docref (NULL TSRMLS_CC , E_WARNING , "content truncated from %lu to %ld bytes" , (unsigned long ) len , LONG_MAX );
631
+ len = LONG_MAX ;
632
+ }
625
633
numbytes = len ;
626
634
}
627
635
break ;
@@ -637,7 +645,7 @@ PHP_FUNCTION(file_put_contents)
637
645
if (Z_STRLEN_P (data )) {
638
646
numbytes = php_stream_write (stream , Z_STRVAL_P (data ), Z_STRLEN_P (data ));
639
647
if (numbytes != Z_STRLEN_P (data )) {
640
- php_error_docref (NULL TSRMLS_CC , E_WARNING , "Only %d of %d bytes written, possibly out of free disk space" , numbytes , Z_STRLEN_P (data ));
648
+ php_error_docref (NULL TSRMLS_CC , E_WARNING , "Only %ld of %d bytes written, possibly out of free disk space" , numbytes , Z_STRLEN_P (data ));
641
649
numbytes = -1 ;
642
650
}
643
651
}
@@ -680,7 +688,7 @@ PHP_FUNCTION(file_put_contents)
680
688
if (zend_std_cast_object_tostring (data , & out , IS_STRING TSRMLS_CC ) == SUCCESS ) {
681
689
numbytes = php_stream_write (stream , Z_STRVAL (out ), Z_STRLEN (out ));
682
690
if (numbytes != Z_STRLEN (out )) {
683
- php_error_docref (NULL TSRMLS_CC , E_WARNING , "Only %d of %d bytes written, possibly out of free disk space" , numbytes , Z_STRLEN (out ));
691
+ php_error_docref (NULL TSRMLS_CC , E_WARNING , "Only %ld of %d bytes written, possibly out of free disk space" , numbytes , Z_STRLEN (out ));
684
692
numbytes = -1 ;
685
693
}
686
694
zval_dtor (& out );
0 commit comments