Skip to content

Commit 3d0580a

Browse files
committed
Fix memory leak on non-VLA-supporting systems when n<=0.
1 parent 2ff4c52 commit 3d0580a

1 file changed

Lines changed: 48 additions & 17 deletions

File tree

src/util/integerfactoring.c

Lines changed: 48 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,19 @@ static void gaIFLScheduleOpt(const int n,
174174
const uint64_t maxTot,
175175
const uint64_t* maxInd);
176176

177+
/**
178+
* @brief Schedule block/grid/chunk size, integer version, n checked >= 0.
179+
*/
180+
181+
static void gaIScheduleChecked(const int n,
182+
const uint64_t maxBtot,
183+
const uint64_t* maxBind,
184+
const uint64_t maxGtot,
185+
const uint64_t* maxGind,
186+
uint64_t* bs,
187+
uint64_t* gs,
188+
uint64_t* cs);
189+
177190

178191

179192
/**
@@ -1309,37 +1322,35 @@ void gaIFLappend(strb *sb, const ga_factor_list* fl){
13091322
}
13101323
}
13111324

1312-
void gaISchedule(const int n,
1313-
const uint64_t maxBtot,
1314-
const uint64_t* maxBind,
1315-
const uint64_t maxGtot,
1316-
const uint64_t* maxGind,
1317-
uint64_t* bs,
1318-
uint64_t* gs,
1319-
uint64_t* cs){
1325+
static void gaIScheduleChecked(const int n,
1326+
const uint64_t maxBtot,
1327+
const uint64_t* maxBind,
1328+
const uint64_t maxGtot,
1329+
const uint64_t* maxGind,
1330+
uint64_t* bs,
1331+
uint64_t* gs,
1332+
uint64_t* cs){
13201333
int i;
13211334
uint64_t kBS, kGS, k;
13221335

13231336
/**
13241337
* Allocate a VLA or similar.
1325-
*
1326-
* C89 neither allows VLAs nor a check beforehand that n>0 to avoid UB
1327-
* (but malloc of 0 bytes is well-defined), so force VLA size to be >= 1
1328-
* with a !n + n trick.
1338+
*
1339+
* C89 neither allows VLAs nor a check beforehand that n>0 to avoid UB. The
1340+
* check for n>0 was thus done in our caller.
13291341
*/
1330-
1342+
13311343
#if GA_USING_MALLOC_FOR_VLA
13321344
ga_factor_list* factBS = malloc(n * sizeof(*factBS));
13331345
ga_factor_list* factGS = malloc(n * sizeof(*factGS));
13341346
ga_factor_list* factCS = malloc(n * sizeof(*factCS));
13351347
#else
1336-
ga_factor_list factBS[!n + n];
1337-
ga_factor_list factGS[!n + n];
1338-
ga_factor_list factCS[!n + n];
1348+
ga_factor_list factBS[n];
1349+
ga_factor_list factGS[n];
1350+
ga_factor_list factCS[n];
13391351
#endif
13401352

13411353

1342-
if(n<=0){return;}
13431354

13441355

13451356
/**
@@ -1394,6 +1405,26 @@ void gaISchedule(const int n,
13941405
#endif
13951406
}
13961407

1408+
void gaISchedule(const int n,
1409+
const uint64_t maxBtot,
1410+
const uint64_t* maxBind,
1411+
const uint64_t maxGtot,
1412+
const uint64_t* maxGind,
1413+
uint64_t* bs,
1414+
uint64_t* gs,
1415+
uint64_t* cs){
1416+
if(n<=0){return;}
1417+
1418+
gaIScheduleChecked(n,
1419+
maxBtot,
1420+
maxBind,
1421+
maxGtot,
1422+
maxGind,
1423+
bs,
1424+
gs,
1425+
cs);
1426+
}
1427+
13971428
void gaIFLSchedule(const int n,
13981429
const uint64_t maxBtot,
13991430
const uint64_t* maxBind,

0 commit comments

Comments
 (0)