1515#include < algorithm>
1616#include < array>
1717#include < charconv>
18+ #include < cstdint>
1819#include < limits>
1920#include < sstream>
2021#include < string_view>
@@ -112,12 +113,6 @@ void PerIsolateOptions::HandleMaxOldSpaceSizePercentage(
112113 std::vector<std::string>* errors,
113114 std::string* max_old_space_size_percentage) {
114115 std::string original_input_for_error = *max_old_space_size_percentage;
115- // Check if the percentage value is empty
116- if (max_old_space_size_percentage->empty ()) {
117- errors->push_back (" --max-old-space-size-percentage must not be empty" );
118- return ;
119- }
120-
121116 // Parse the percentage value
122117 char * end_ptr;
123118 double percentage =
@@ -131,17 +126,21 @@ void PerIsolateOptions::HandleMaxOldSpaceSizePercentage(
131126 return ;
132127 }
133128
134- // Get available memory in MB
135- size_t total_memory = uv_get_total_memory ();
136- size_t constrained_memory = uv_get_constrained_memory ();
129+ // Get available memory in bytes
130+ uint64_t total_memory = uv_get_total_memory ();
131+ uint64_t constrained_memory = uv_get_constrained_memory ();
137132
138133 // Use constrained memory if available, otherwise use total memory
139- size_t available_memory =
140- (constrained_memory > 0 ) ? constrained_memory : total_memory;
134+ // This logic correctly handles the documented guarantees.
135+ // Use uint64_t for the result to prevent data loss on 32-bit systems.
136+ uint64_t available_memory =
137+ (constrained_memory > 0 && constrained_memory != UINT64_MAX)
138+ ? constrained_memory
139+ : total_memory;
141140
142141 // Convert to MB and calculate the percentage
143- size_t memory_mb = available_memory / (1024 * 1024 );
144- size_t calculated_mb = static_cast <size_t >(memory_mb * percentage / 100.0 );
142+ uint64_t memory_mb = available_memory / (1024 * 1024 );
143+ uint64_t calculated_mb = static_cast <size_t >(memory_mb * percentage / 100.0 );
145144
146145 // Convert back to string
147146 max_old_space_size = std::to_string (calculated_mb);
0 commit comments