@@ -467,7 +467,6 @@ open_cache_file(const char * path, struct bs_cache_key * key, const char ** errn
467
467
static int
468
468
fetch_cached_data (int fd , ssize_t data_size , VALUE handler , VALUE args , VALUE * output_data , int * exception_tag , const char * * errno_provenance )
469
469
{
470
- char * data = NULL ;
471
470
ssize_t nread ;
472
471
int ret ;
473
472
@@ -479,8 +478,8 @@ fetch_cached_data(int fd, ssize_t data_size, VALUE handler, VALUE args, VALUE *
479
478
ret = ERROR_WITH_ERRNO ;
480
479
goto done ;
481
480
}
482
- data = ALLOC_N ( char , data_size );
483
- nread = read (fd , data , data_size );
481
+ storage_data = rb_str_buf_new ( data_size );
482
+ nread = read (fd , RSTRING_PTR ( storage_data ) , data_size );
484
483
if (nread < 0 ) {
485
484
* errno_provenance = "bs_fetch:fetch_cached_data:read" ;
486
485
ret = ERROR_WITH_ERRNO ;
@@ -491,7 +490,7 @@ fetch_cached_data(int fd, ssize_t data_size, VALUE handler, VALUE args, VALUE *
491
490
goto done ;
492
491
}
493
492
494
- storage_data = rb_str_new ( data , data_size );
493
+ rb_str_set_len ( storage_data , nread );
495
494
496
495
* exception_tag = bs_storage_to_output (handler , args , storage_data , output_data );
497
496
if (* output_data == rb_cBootsnap_CompileCache_UNCOMPILABLE ) {
@@ -500,7 +499,6 @@ fetch_cached_data(int fd, ssize_t data_size, VALUE handler, VALUE args, VALUE *
500
499
}
501
500
ret = 0 ;
502
501
done :
503
- if (data != NULL ) xfree (data );
504
502
return ret ;
505
503
}
506
504
@@ -607,17 +605,22 @@ atomic_write_cache_file(char * path, struct bs_cache_key * key, VALUE data, cons
607
605
608
606
609
607
/* Read contents from an fd, whose contents are asserted to be +size+ bytes
610
- * long, into a buffer */
611
- static ssize_t
612
- bs_read_contents (int fd , size_t size , char * * contents , const char * * errno_provenance )
608
+ * long, returning a Ruby string on success and Qfalse on failure */
609
+ static VALUE
610
+ bs_read_contents (int fd , size_t size , const char * * errno_provenance )
613
611
{
612
+ VALUE contents ;
614
613
ssize_t nread ;
615
- * contents = ALLOC_N (char , size );
616
- nread = read (fd , * contents , size );
614
+ contents = rb_str_buf_new (size );
615
+ nread = read (fd , RSTRING_PTR (contents ), size );
616
+
617
617
if (nread < 0 ) {
618
618
* errno_provenance = "bs_fetch:bs_read_contents:read" ;
619
+ return Qfalse ;
620
+ } else {
621
+ rb_str_set_len (contents , nread );
622
+ return contents ;
619
623
}
620
- return nread ;
621
624
}
622
625
623
626
/*
@@ -668,7 +671,6 @@ static VALUE
668
671
bs_fetch (char * path , VALUE path_v , char * cache_path , VALUE handler , VALUE args )
669
672
{
670
673
struct bs_cache_key cached_key , current_key ;
671
- char * contents = NULL ;
672
674
int cache_fd = -1 , current_fd = -1 ;
673
675
int res , valid_cache = 0 , exception_tag = 0 ;
674
676
const char * errno_provenance = NULL ;
@@ -713,8 +715,7 @@ bs_fetch(char * path, VALUE path_v, char * cache_path, VALUE handler, VALUE args
713
715
else if (res == CACHE_UNCOMPILABLE ) {
714
716
/* If fetch_cached_data returned `Uncompilable` we fallback to `input_to_output`
715
717
This happens if we have say, an unsafe YAML cache, but try to load it in safe mode */
716
- if (bs_read_contents (current_fd , current_key .size , & contents , & errno_provenance ) < 0 ) goto fail_errno ;
717
- input_data = rb_str_new (contents , current_key .size );
718
+ if ((input_data = bs_read_contents (current_fd , current_key .size , & errno_provenance )) == Qfalse ) goto fail_errno ;
718
719
bs_input_to_output (handler , args , input_data , & output_data , & exception_tag );
719
720
if (exception_tag != 0 ) goto raise ;
720
721
goto succeed ;
@@ -727,8 +728,7 @@ bs_fetch(char * path, VALUE path_v, char * cache_path, VALUE handler, VALUE args
727
728
/* Cache is stale, invalid, or missing. Regenerate and write it out. */
728
729
729
730
/* Read the contents of the source file into a buffer */
730
- if (bs_read_contents (current_fd , current_key .size , & contents , & errno_provenance ) < 0 ) goto fail_errno ;
731
- input_data = rb_str_new (contents , current_key .size );
731
+ if ((input_data = bs_read_contents (current_fd , current_key .size , & errno_provenance )) == Qfalse ) goto fail_errno ;
732
732
733
733
/* Try to compile the input_data using input_to_storage(input_data) */
734
734
exception_tag = bs_input_to_storage (handler , args , input_data , path_v , & storage_data );
@@ -775,7 +775,6 @@ bs_fetch(char * path, VALUE path_v, char * cache_path, VALUE handler, VALUE args
775
775
goto succeed ; /* output_data is now the correct return. */
776
776
777
777
#define CLEANUP \
778
- if (contents != NULL) xfree(contents); \
779
778
if (current_fd >= 0) close(current_fd); \
780
779
if (cache_fd >= 0) close(cache_fd);
781
780
@@ -836,8 +835,7 @@ bs_precompile(char * path, VALUE path_v, char * cache_path, VALUE handler)
836
835
/* Cache is stale, invalid, or missing. Regenerate and write it out. */
837
836
838
837
/* Read the contents of the source file into a buffer */
839
- if (bs_read_contents (current_fd , current_key .size , & contents , & errno_provenance ) < 0 ) goto fail ;
840
- input_data = rb_str_new (contents , current_key .size );
838
+ if ((input_data = bs_read_contents (current_fd , current_key .size , & errno_provenance )) == Qfalse ) goto fail ;
841
839
842
840
/* Try to compile the input_data using input_to_storage(input_data) */
843
841
exception_tag = bs_input_to_storage (handler , Qnil , input_data , path_v , & storage_data );
@@ -858,7 +856,6 @@ bs_precompile(char * path, VALUE path_v, char * cache_path, VALUE handler)
858
856
goto succeed ;
859
857
860
858
#define CLEANUP \
861
- if (contents != NULL) xfree(contents); \
862
859
if (current_fd >= 0) close(current_fd); \
863
860
if (cache_fd >= 0) close(cache_fd);
864
861
0 commit comments