Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 42c5544

Browse filesBrowse files
Asaf-Federmanaduh95
authored andcommitted
src: assert memory calc for max-old-space-size-percentage
Add validation to ensure that --max-old-space-size-percentage cannot be used when available memory cannot be calculated, preventing undefined behavior when memory detection fails. Also enhance test-process-constrained-memory.js to support testing in constrained environments where memory calculation may fail. PR-URL: #59460 Backport-PR-URL: #59631 Reviewed-By: theanarkh <theratliter@gmail.com> Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
1 parent 686ac49 commit 42c5544
Copy full SHA for 42c5544

File tree

Expand file treeCollapse file tree

2 files changed

+13
-3
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

2 files changed

+13
-3
lines changed
Open diff view settings
Collapse file

‎src/node_options.cc‎

Copy file name to clipboardExpand all lines: src/node_options.cc
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,11 @@ void PerIsolateOptions::HandleMaxOldSpaceSizePercentage(
134134
? constrained_memory
135135
: total_memory;
136136

137+
if (available_memory == 0) {
138+
errors->push_back("the available memory can not be calculated");
139+
return;
140+
}
141+
137142
// Convert to MB and calculate the percentage
138143
uint64_t memory_mb = available_memory / (1024 * 1024);
139144
uint64_t calculated_mb = static_cast<size_t>(memory_mb * percentage / 100.0);
Collapse file

‎test/parallel/test-max-old-space-size-percentage.js‎

Copy file name to clipboardExpand all lines: test/parallel/test-max-old-space-size-percentage.js
+8-3Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,14 +119,19 @@ assert(
119119

120120
// Validate heap sizes against system memory
121121
const totalMemoryMB = Math.floor(os.totalmem() / 1024 / 1024);
122-
const margin = 10; // 5% margin
122+
const uint64Max = 2 ** 64 - 1;
123+
const constrainedMemory = process.constrainedMemory();
124+
const constrainedMemoryMB = Math.floor(constrainedMemory / 1024 / 1024);
125+
const effectiveMemoryMB =
126+
constrainedMemory > 0 && constrainedMemory !== uint64Max ? constrainedMemoryMB : totalMemoryMB;
127+
const margin = 10; // 10% margin
123128
testPercentages.forEach((percentage) => {
124-
const upperLimit = totalMemoryMB * ((percentage + margin) / 100);
129+
const upperLimit = effectiveMemoryMB * ((percentage + margin) / 100);
125130
assert(
126131
heapSizes[percentage] <= upperLimit,
127132
`Heap size for ${percentage}% (${heapSizes[percentage]} MB) should not exceed upper limit (${upperLimit} MB)`
128133
);
129-
const lowerLimit = totalMemoryMB * ((percentage - margin) / 100);
134+
const lowerLimit = effectiveMemoryMB * ((percentage - margin) / 100);
130135
assert(
131136
heapSizes[percentage] >= lowerLimit,
132137
`Heap size for ${percentage}% (${heapSizes[percentage]} MB) should not be less than lower limit (${lowerLimit} MB)`

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.