Skip to content

Commit bc921a8

Browse files
authored
Merge pull request #536 from beached/v2
Handle large file sizes in read_file
2 parents 4778a5b + a19cefe commit bc921a8

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

include/daw/daw_read_file.h

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "daw_attributes.h"
1313
#include "daw_traits.h"
1414

15+
#include <cstddef>
1516
#include <cstdio>
1617
#include <exception>
1718
#include <filesystem>
@@ -28,7 +29,13 @@ namespace daw {
2829
if( fsize == 0 ) {
2930
return std::basic_string<CharT>{ };
3031
}
31-
auto result = std::basic_string<CharT>( fsize, CharT{ } );
32+
if( static_cast<std::uintmax_t>( std::basic_string<CharT>{ }.max_size( ) ) <
33+
fsize ) {
34+
// File is too big to fit into string(WIN32)
35+
return std::nullopt;
36+
}
37+
auto result =
38+
std::basic_string<CharT>( static_cast<std::size_t>( fsize ), CharT{ } );
3239
auto *f = fopen( path.c_str( ), "rb" );
3340
if( not f ) {
3441
return std::nullopt;
@@ -62,7 +69,13 @@ namespace daw {
6269
if( fsize == 0 ) {
6370
return std::basic_string<CharT>{ };
6471
}
65-
auto result = std::basic_string<CharT>( fsize, CharT{ } );
72+
if( static_cast<std::uintmax_t>( std::basic_string<CharT>{ }.max_size( ) ) <
73+
fsize ) {
74+
// File is too big to fit into string(WIN32)
75+
return std::nullopt;
76+
}
77+
auto result =
78+
std::basic_string<CharT>( static_cast<std::size_t>( fsize ), CharT{ } );
6679
auto *f = _wfopen( path.c_str( ), L"rb" );
6780
if( not f ) {
6881
return std::nullopt;

0 commit comments

Comments
 (0)