Skip to content

Commit cbee348

Browse files
committed
Use std::make_unique() for variable-length arrays instead of array[N] when N is
not constant. VLAs are non-standard, and allocate on the stack instead of the heap, which increases the chance of stack overflows.
1 parent 291f8cc commit cbee348

File tree

4 files changed

+19
-12
lines changed

4 files changed

+19
-12
lines changed

src/sst/core/bootsst.cc

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
#include "sst/core/configShared.h"
1616

1717
#include <cstdlib>
18-
#include <string.h>
18+
#include <cstring>
19+
#include <memory>
1920
#include <unistd.h> // for opterr
2021

2122
int
@@ -31,14 +32,14 @@ main(int argc, char* argv[])
3132
SST::ConfigShared cfg(true, true, true, true);
3233

3334
// Make a copy of the argv array (shallow)
34-
char* argv_copy[argc + 1];
35+
auto argv_copy = std::make_unique<char*[]>(argc + 1);
3536
for ( int i = 0; i < argc; ++i ) {
3637
argv_copy[i] = argv[i];
3738
}
3839
argv[argc] = nullptr;
3940

4041
// All ranks parse the command line
41-
cfg.parseCmdLine(argc, argv_copy);
42+
cfg.parseCmdLine(argc, argv_copy.get());
4243

4344
if ( cfg.no_env_config() ) config_env = 0;
4445
if ( cfg.verbose() ) verbose = 1;

src/sst/core/bootsstinfo.cc

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
#include "sst/core/bootshared.h"
1515
#include "sst/core/configShared.h"
1616

17+
#include <cstdio>
18+
#include <memory>
19+
1720
int
1821
main(int argc, char* argv[])
1922
{
@@ -26,13 +29,13 @@ main(int argc, char* argv[])
2629
SST::ConfigShared cfg(true, true, true, true);
2730

2831
// Make a copy of the argv array (shallow)
29-
char* argv_copy[argc + 1];
32+
auto argv_copy = std::make_unique<char*[]>(argc + 1);
3033
for ( int i = 0; i < argc; ++i ) {
3134
argv_copy[i] = argv[i];
3235
}
3336
argv[argc] = nullptr;
3437

35-
cfg.parseCmdLine(argc, argv_copy, true);
38+
cfg.parseCmdLine(argc, argv_copy.get(), true);
3639

3740
if ( cfg.no_env_config() ) config_env = false;
3841

src/sst/core/configBase.cc

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <cstdlib>
2121
#include <getopt.h>
2222
#include <iostream>
23+
#include <memory>
2324
#include <string>
2425
#include <sys/ioctl.h>
2526
#include <sys/stat.h>
@@ -302,13 +303,13 @@ ConfigBase::parseCmdLine(int argc, char* argv[], bool ignore_unknown)
302303
// Turn off printing of errors in getopt_long
303304
opterr = 0;
304305
}
305-
struct option sst_long_options[num_options + 2];
306+
auto sst_long_options = std::make_unique<option[]>(num_options + 2);
306307

307308
// Because a zero index can mean two different things, we put in a
308309
// dummy so we don't ever get a zero index
309310
sst_long_options[0] = { "*DUMMY_ARGUMENT*", no_argument, nullptr, 0 };
310-
int option_map[num_options + 1];
311-
option_map[0] = 0;
311+
auto option_map = std::make_unique<int[]>(num_options + 1);
312+
option_map[0] = 0;
312313
{
313314
int count = 1;
314315

@@ -344,7 +345,8 @@ ConfigBase::parseCmdLine(int argc, char* argv[], bool ignore_unknown)
344345
int status = 0;
345346
while ( 0 == status ) {
346347
int option_index = 0;
347-
const int intC = getopt_long(my_argc, argv, short_options_string.c_str(), sst_long_options, &option_index);
348+
const int intC =
349+
getopt_long(my_argc, argv, short_options_string.c_str(), sst_long_options.get(), &option_index);
348350

349351
if ( intC == -1 ) /* We're done */
350352
break;

src/sst/core/impl/partitioners/simplepart.cc

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "sst/core/warnmacros.h"
1818

1919
#include <map>
20+
#include <memory>
2021
#include <stdlib.h>
2122
#include <vector>
2223

@@ -192,8 +193,8 @@ SimplePartitioner::performPartition(PartitionGraph* graph)
192193
graph->getNumComponents() % 2 == 1 ? (graph->getNumComponents() / 2) + 1 : (graph->getNumComponents() / 2);
193194
const int B_size = graph->getNumComponents() / 2;
194195

195-
ComponentId_t setA[A_size];
196-
ComponentId_t setB[B_size];
196+
auto setA = std::make_unique<ComponentId_t[]>(A_size);
197+
auto setB = std::make_unique<ComponentId_t[]>(B_size);
197198

198199
int indexA = 0;
199200
int indexB = 0;
@@ -229,7 +230,7 @@ SimplePartitioner::performPartition(PartitionGraph* graph)
229230
}
230231
}
231232

232-
simple_partition_step(component_map, setA, A_size, 0, setB, B_size, 1, timeTable, 1);
233+
simple_partition_step(component_map, setA.get(), A_size, 0, setB.get(), B_size, 1, timeTable, 1);
233234
}
234235
}
235236

0 commit comments

Comments
 (0)