Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 5424bc6

Browse files
janvorlijkotas
authored andcommitted
Fix one missing check for NULL after malloc (#15877)
When making the last change to the numa.cpp, I have made a mistake and forgotten to check return value of one of the mallocs. This change fixes that and also changes the code pattern to use goto for the cleanup purposes instead of the nested ifs.
1 parent 4c8a732 commit 5424bc6

File tree

1 file changed

+27
-16
lines changed

1 file changed

+27
-16
lines changed

src/pal/src/numa/numa.cpp

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -117,26 +117,37 @@ BOOL
117117
AllocateLookupArrays()
118118
{
119119
g_groupAndIndexToCpu = (short*)malloc(g_groupCount * MaxCpusPerGroup * sizeof(short));
120-
if (g_groupAndIndexToCpu != NULL)
120+
if (g_groupAndIndexToCpu == NULL)
121121
{
122-
g_cpuToAffinity = (CpuAffinity*)malloc(g_possibleCpuCount * sizeof(CpuAffinity));
123-
if (g_cpuToAffinity != NULL)
124-
{
125-
g_groupToCpuMask = (KAFFINITY*)malloc(g_groupCount * sizeof(KAFFINITY));
126-
if (g_groupToCpuMask != NULL)
127-
{
128-
g_groupToCpuCount = (BYTE*)malloc(g_groupCount * sizeof(BYTE));
129-
memset(g_groupAndIndexToCpu, 0xff, g_groupCount * MaxCpusPerGroup * sizeof(short));
130-
memset(g_cpuToAffinity, 0xff, g_possibleCpuCount * sizeof(CpuAffinity));
131-
memset(g_groupToCpuMask, 0, g_groupCount * sizeof(KAFFINITY));
132-
memset(g_groupToCpuCount, 0, g_groupCount * sizeof(BYTE));
122+
goto FAILED;
123+
}
133124

134-
return TRUE;
135-
}
136-
}
125+
g_cpuToAffinity = (CpuAffinity*)malloc(g_possibleCpuCount * sizeof(CpuAffinity));
126+
if (g_cpuToAffinity == NULL)
127+
{
128+
goto FAILED;
137129
}
138130

139-
// One of the allocations have failed
131+
g_groupToCpuMask = (KAFFINITY*)malloc(g_groupCount * sizeof(KAFFINITY));
132+
if (g_groupToCpuMask == NULL)
133+
{
134+
goto FAILED;
135+
}
136+
137+
g_groupToCpuCount = (BYTE*)malloc(g_groupCount * sizeof(BYTE));
138+
if (g_groupToCpuCount == NULL)
139+
{
140+
goto FAILED;
141+
}
142+
143+
memset(g_groupAndIndexToCpu, 0xff, g_groupCount * MaxCpusPerGroup * sizeof(short));
144+
memset(g_cpuToAffinity, 0xff, g_possibleCpuCount * sizeof(CpuAffinity));
145+
memset(g_groupToCpuMask, 0, g_groupCount * sizeof(KAFFINITY));
146+
memset(g_groupToCpuCount, 0, g_groupCount * sizeof(BYTE));
147+
148+
return TRUE;
149+
150+
FAILED:
140151
FreeLookupArrays();
141152

142153
return FALSE;

0 commit comments

Comments
 (0)