Skip to content

Commit bc224d1

Browse files
Abort if malloc fails (#627)
1 parent 30ac2d7 commit bc224d1

14 files changed

+129
-68
lines changed

Makefile.in

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ KEXT_SOURCES += src/homos-graphs.c
2525
KEXT_SOURCES += src/perms.c
2626
KEXT_SOURCES += src/planar.c
2727
KEXT_SOURCES += src/schreier-sims.c
28+
KEXT_SOURCES += src/safemalloc.c
2829

2930
ifdef WITH_INCLUDED_BLISS
3031
KEXT_SOURCES += extern/bliss-0.73/defs.cc

src/bitarray.c

+3-5
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,22 @@
1010

1111
#include "bitarray.h"
1212

13-
// C headers
14-
#include <stdlib.h> // for free, calloc, malloc
15-
1613
// GAP headers
1714
#include "gap-includes.h" // for Obj, ELM_LIST, ISB_LIST, Fail
1815

1916
// Digraphs headers
2017
#include "digraphs-debug.h" // for DIGRAPHS_ASSERT
18+
#include "safemalloc.h"
2119

2220
BitArray* new_bit_array(uint16_t const nr_bits) {
23-
BitArray* bit_array = malloc(sizeof(BitArray));
21+
BitArray* bit_array = safe_malloc(sizeof(BitArray));
2422
bit_array->nr_bits = nr_bits;
2523
bit_array->nr_blocks = ((nr_bits % NUMBER_BITS_PER_BLOCK) == 0
2624
? nr_bits / NUMBER_BITS_PER_BLOCK
2725
: nr_bits / NUMBER_BITS_PER_BLOCK + 1);
2826
// The previous line is not tested since all the bit arrays we use are
2927
// currently of length MAXVERTS = 512.
30-
bit_array->blocks = calloc(bit_array->nr_blocks, NUMBER_BITS_PER_BLOCK);
28+
bit_array->blocks = safe_calloc(bit_array->nr_blocks, NUMBER_BITS_PER_BLOCK);
3129
return bit_array;
3230
}
3331

src/bitarray.h

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <stdbool.h> // for bool
1717
#include <stddef.h> // for size_t
1818
#include <stdint.h> // for uint16_t
19+
#include <string.h> // for memset
1920

2021
// GAP headers
2122
#include "gap-includes.h" // for COUNT_TRUES_BLOCKS, Obj, . . .

src/cliques.c

-2
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@
1010

1111
// C headers
1212
#include <stdbool.h> // for true, false, bool
13-
#include <stddef.h> // for NULL
1413
#include <stdint.h> // for uint16_t, uint64_t
15-
#include <stdlib.h> // for malloc, NULL
1614

1715
// GAP headers
1816
#include "gap-includes.h"

src/conditions.c

+6-5
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,17 @@
1717

1818
// Digraphs headers
1919
#include "digraphs-debug.h" // for DIGRAPHS_ASSERT
20+
#include "safemalloc.h"
2021

2122
Conditions* new_conditions(uint16_t const nr1, uint16_t const nr2) {
2223
DIGRAPHS_ASSERT(nr1 != 0);
2324
DIGRAPHS_ASSERT(nr2 != 0);
24-
Conditions* conditions = malloc(sizeof(Conditions));
25+
Conditions* conditions = safe_malloc(sizeof(Conditions));
2526

26-
conditions->bit_array = malloc(sizeof(BitArray*) * nr1 * nr1);
27-
conditions->changed = malloc(nr1 * (nr1 + 1) * sizeof(uint16_t));
28-
conditions->height = malloc(nr1 * sizeof(uint16_t));
29-
conditions->sizes = malloc(nr1 * nr1 * sizeof(uint16_t));
27+
conditions->bit_array = safe_malloc(sizeof(BitArray*) * nr1 * nr1);
28+
conditions->changed = safe_malloc(nr1 * (nr1 + 1) * sizeof(uint16_t));
29+
conditions->height = safe_malloc(nr1 * sizeof(uint16_t));
30+
conditions->sizes = safe_malloc(nr1 * nr1 * sizeof(uint16_t));
3031
conditions->nr1 = nr1;
3132
conditions->nr2 = nr2;
3233

src/digraphs.c

+34-32
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,19 @@
1313
*******************************************************************************/
1414

1515
#include "digraphs.h"
16-
#include "digraphs-config.h"
1716

1817
#include <stdbool.h> // for false, true, bool
1918
#include <stdint.h> // for uint64_t
2019
#include <stdlib.h> // for NULL, free
20+
#include <string.h> // for memcpy
2121

22-
#include "bliss-includes.h" // for bliss stuff
23-
#include "cliques.h"
24-
#include "digraphs-debug.h" // for DIGRAPHS_ASSERT
25-
#include "homos.h" // for FuncHomomorphismDigraphsFinder
26-
#include "planar.h" // for FUNC_IS_PLANAR, . . .
22+
#include "bliss-includes.h" // for bliss stuff
23+
#include "cliques.h" // for FuncDigraphsCliquesFinder
24+
#include "digraphs-config.h" // for DIGRAPHS_WITH_INCLUDED_BLISS
25+
#include "digraphs-debug.h" // for DIGRAPHS_ASSERT
26+
#include "homos.h" // for FuncHomomorphismDigraphsFinder
27+
#include "planar.h" // for FUNC_IS_PLANAR, . . .
28+
#include "safemalloc.h" // for safe_malloc
2729

2830
#undef PACKAGE
2931
#undef PACKAGE_BUGREPORT
@@ -184,7 +186,7 @@ static Obj FuncGABOW_SCC(Obj self, Obj digraph) {
184186

185187
comps = NEW_PLIST_IMM(T_PLIST_TAB, n);
186188

187-
stack2 = malloc((4 * n + 2) * sizeof(UInt));
189+
stack2 = safe_malloc((4 * n + 2) * sizeof(UInt));
188190
frames = stack2 + n + 1;
189191
end2 = 0;
190192

@@ -292,7 +294,7 @@ static Obj FuncDIGRAPH_CONNECTED_COMPONENTS(Obj self, Obj digraph) {
292294
gid = NEW_PLIST_IMM(T_PLIST_EMPTY, 0);
293295
gcomps = NEW_PLIST_IMM(T_PLIST_EMPTY, 0);
294296
} else {
295-
id = malloc(n * sizeof(UInt));
297+
id = safe_malloc(n * sizeof(UInt));
296298
for (i = 0; i < n; i++) {
297299
id[i] = i;
298300
}
@@ -308,7 +310,7 @@ static Obj FuncDIGRAPH_CONNECTED_COMPONENTS(Obj self, Obj digraph) {
308310
}
309311

310312
// "Normalise" id, giving it sensible labels
311-
nid = malloc(n * sizeof(UInt));
313+
nid = safe_malloc(n * sizeof(UInt));
312314
nrcomps = 0;
313315
for (i = 0; i < n; i++) {
314316
f = UF_FIND(id, i);
@@ -346,8 +348,8 @@ static Obj FuncIS_ACYCLIC_DIGRAPH(Obj self, Obj adj) {
346348
nr = LEN_PLIST(adj);
347349

348350
// init the buf
349-
ptr = calloc(nr + 1, sizeof(UInt));
350-
stack = malloc((2 * nr + 2) * sizeof(UInt));
351+
ptr = safe_calloc(nr + 1, sizeof(UInt));
352+
stack = safe_malloc((2 * nr + 2) * sizeof(UInt));
351353

352354
for (i = 1; i <= nr; i++) {
353355
nbs = ELM_PLIST(adj, i);
@@ -416,9 +418,9 @@ static Obj FuncDIGRAPH_LONGEST_DIST_VERTEX(Obj self, Obj adj, Obj start) {
416418
return INTOBJ_INT(0);
417419
}
418420

419-
ptr = calloc(nr + 1, sizeof(UInt));
420-
depth = calloc(nr + 1, sizeof(UInt));
421-
stack = malloc((2 * nr + 2) * sizeof(UInt));
421+
ptr = safe_calloc(nr + 1, sizeof(UInt));
422+
depth = safe_calloc(nr + 1, sizeof(UInt));
423+
stack = safe_malloc((2 * nr + 2) * sizeof(UInt));
422424

423425
level = 1;
424426
stack[0] = i;
@@ -503,9 +505,9 @@ static Obj FuncDIGRAPH_TRANS_REDUCTION(Obj self, Obj D) {
503505
Obj const in_list = FuncDIGRAPH_IN_OUT_NBS(self, FuncOutNeighbours(self, D));
504506

505507
// Create data structures needed for computation
506-
UInt* ptr = calloc(n + 1, sizeof(UInt));
507-
bool* mat = calloc(n * n, sizeof(bool));
508-
UInt* stack = malloc((2 * n + 2) * sizeof(UInt));
508+
UInt* ptr = safe_calloc(n + 1, sizeof(UInt));
509+
bool* mat = safe_calloc(n * n, sizeof(bool));
510+
UInt* stack = safe_malloc((2 * n + 2) * sizeof(UInt));
509511

510512
// Start a depth-first search from each source of the digraph
511513
for (UInt i = 1; i <= n; i++) {
@@ -604,8 +606,8 @@ static Obj FuncDIGRAPH_PATH(Obj self, Obj adj, Obj u, Obj v) {
604606
nr = LEN_PLIST(adj);
605607

606608
// init the buf
607-
ptr = calloc(nr + 1, sizeof(UInt));
608-
stack = malloc((2 * nr + 2) * sizeof(UInt));
609+
ptr = safe_calloc(nr + 1, sizeof(UInt));
610+
stack = safe_malloc((2 * nr + 2) * sizeof(UInt));
609611

610612
level = 1;
611613
stack[0] = i;
@@ -673,8 +675,8 @@ Obj FuncIS_ANTISYMMETRIC_DIGRAPH(Obj self, Obj adj) {
673675
}
674676

675677
// init the buf (is this correct length?)
676-
ptr = calloc(nr + 1, sizeof(UInt));
677-
stack = malloc((4 * nr + 4) * sizeof(UInt));
678+
ptr = safe_calloc(nr + 1, sizeof(UInt));
679+
stack = safe_malloc((4 * nr + 4) * sizeof(UInt));
678680

679681
for (i = 1; i <= nr; i++) {
680682
nbs = ELM_PLIST(adj, i);
@@ -751,11 +753,11 @@ static Obj FuncIS_STRONGLY_CONNECTED_DIGRAPH(Obj self, Obj digraph) {
751753
}
752754

753755
nextid = 1;
754-
bag = malloc(n * 4 * sizeof(UInt));
756+
bag = safe_malloc(n * 4 * sizeof(UInt));
755757
ptr1 = bag;
756758
ptr2 = bag + n;
757759
fptr = bag + n * 2;
758-
id = calloc(n + 1, sizeof(UInt));
760+
id = safe_calloc(n + 1, sizeof(UInt));
759761

760762
// first vertex v=1
761763
PLAIN_LIST(ELM_PLIST(digraph, 1));
@@ -817,8 +819,8 @@ static Obj FuncDIGRAPH_TOPO_SORT(Obj self, Obj adj) {
817819
}
818820

819821
// init the buf
820-
ptr = calloc(nr + 1, sizeof(UInt));
821-
stack = malloc((2 * nr + 2) * sizeof(UInt));
822+
ptr = safe_calloc(nr + 1, sizeof(UInt));
823+
stack = safe_malloc((2 * nr + 2) * sizeof(UInt));
822824
count = 0;
823825

824826
for (i = 1; i <= nr; i++) {
@@ -912,8 +914,8 @@ static Obj FuncDIGRAPH_SYMMETRIC_SPANNING_FOREST(Obj self, Obj adj) {
912914
}
913915

914916
// init the buffer
915-
ptr = calloc(nr + 1, sizeof(UInt));
916-
stack = malloc((2 * nr + 2) * sizeof(UInt));
917+
ptr = safe_calloc(nr + 1, sizeof(UInt));
918+
stack = safe_malloc((2 * nr + 2) * sizeof(UInt));
917919

918920
for (i = 1; i <= nr; i++) {
919921
// perform DFS only on still-undiscovered non-trivial connected components
@@ -1082,7 +1084,7 @@ static Obj FuncIS_MULTI_DIGRAPH(Obj self, Obj digraph) {
10821084

10831085
adj = FuncOutNeighbours(self, digraph);
10841086
n = DigraphNrVertices(digraph);
1085-
seen = calloc(n + 1, sizeof(UInt));
1087+
seen = safe_calloc(n + 1, sizeof(UInt));
10861088

10871089
for (i = 1; i <= n; i++) {
10881090
adji = ELM_PLIST(adj, i);
@@ -1155,7 +1157,7 @@ static Obj FLOYD_WARSHALL(Obj digraph,
11551157
}
11561158

11571159
// Initialise the n x n matrix with val1 and val2
1158-
dist = malloc(n * n * sizeof(Int));
1160+
dist = safe_malloc(n * n * sizeof(Int));
11591161
for (i = 0; i < n * n; i++) {
11601162
dist[i] = val1;
11611163
}
@@ -1178,7 +1180,7 @@ static Obj FLOYD_WARSHALL(Obj digraph,
11781180

11791181
if (copy) {
11801182
// This is the special case for IS_TRANSITIVE_DIGRAPH
1181-
adj = malloc(n * n * sizeof(Int));
1183+
adj = safe_malloc(n * n * sizeof(Int));
11821184
for (i = 0; i < n * n; i++) {
11831185
adj[i] = dist[i];
11841186
}
@@ -1406,7 +1408,7 @@ static Obj FuncDIGRAPH_EQUALS(Obj self, Obj digraph1, Obj digraph2) {
14061408
out1 = FuncOutNeighbours(self, digraph1);
14071409
out2 = FuncOutNeighbours(self, digraph2);
14081410

1409-
buf = calloc(n1, sizeof(Int));
1411+
buf = safe_calloc(n1, sizeof(Int));
14101412

14111413
// Compare OutNeighbours of each vertex in turn
14121414
for (i = 1; i <= n1; i++) {
@@ -1513,7 +1515,7 @@ static Obj FuncDIGRAPH_LT(Obj self, Obj digraph1, Obj digraph2) {
15131515
out1 = FuncOutNeighbours(self, digraph1);
15141516
out2 = FuncOutNeighbours(self, digraph2);
15151517

1516-
buf = calloc(n1, sizeof(Int));
1518+
buf = safe_calloc(n1, sizeof(Int));
15171519

15181520
// Compare Sorted(out1[i]) and Sorted(out2[i]) for each vertex i
15191521
for (i = 1; i <= n1; i++) {

src/homos-graphs.c

+6-6
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,18 @@
1717
#include "gap-includes.h" // for Obj, Int
1818

1919
// Digraphs headers
20-
#include "bliss-includes.h" // for bliss stuff
2120
#include "digraphs-config.h" // for DIGRAPHS_WITH_INCLUDED_BLISS
2221
#include "digraphs-debug.h" // for DIGRAPHS_ASSERT
22+
#include "safemalloc.h" // for safe_malloc
2323
#include "schreier-sims.h" // for PERM_DEGREE
2424

2525
extern Obj GeneratorsOfGroup;
2626

2727
Digraph* new_digraph(uint16_t const nr_verts) {
2828
DIGRAPHS_ASSERT(nr_verts <= MAXVERTS);
29-
Digraph* digraph = malloc(sizeof(Digraph));
30-
digraph->in_neighbours = malloc(nr_verts * sizeof(BitArray));
31-
digraph->out_neighbours = malloc(nr_verts * sizeof(BitArray));
29+
Digraph* digraph = safe_malloc(sizeof(Digraph));
30+
digraph->in_neighbours = safe_malloc(nr_verts * sizeof(BitArray));
31+
digraph->out_neighbours = safe_malloc(nr_verts * sizeof(BitArray));
3232
for (uint16_t i = 0; i < nr_verts; i++) {
3333
digraph->in_neighbours[i] = new_bit_array(nr_verts);
3434
digraph->out_neighbours[i] = new_bit_array(nr_verts);
@@ -39,8 +39,8 @@ Digraph* new_digraph(uint16_t const nr_verts) {
3939

4040
Graph* new_graph(uint16_t const nr_verts) {
4141
DIGRAPHS_ASSERT(nr_verts <= MAXVERTS);
42-
Graph* graph = malloc(sizeof(Graph));
43-
graph->neighbours = malloc(nr_verts * sizeof(BitArray));
42+
Graph* graph = safe_malloc(sizeof(Graph));
43+
graph->neighbours = safe_malloc(nr_verts * sizeof(BitArray));
4444
for (uint16_t i = 0; i < nr_verts; i++) {
4545
graph->neighbours[i] = new_bit_array(nr_verts);
4646
}

src/homos-graphs.h

+5-4
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,11 @@
1616
#include <stdint.h> // for uint16_t
1717

1818
// Digraphs headers
19-
#include "bitarray.h" // for BitArray
20-
#include "bliss-includes.h" // for bliss stuff
21-
#include "digraphs-debug.h" // for DIGRAPHS_ASSERT
22-
#include "perms.h" // for PermColl
19+
#include "bitarray.h" // for BitArray
20+
#include "bliss-includes.h" // for bliss stuff
21+
#include "digraphs-config.h" // for DIGRAPHS_WITH_INCLUDED_BLISS
22+
#include "digraphs-debug.h" // for DIGRAPHS_ASSERT
23+
#include "perms.h" // for PermColl
2324

2425
////////////////////////////////////////////////////////////////////////
2526
// Directed graphs (digraphs)

src/homos.c

+7-4
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@
3333
#include <stddef.h> // for NULL
3434
#include <stdint.h> // for uint16_t, uint64_t
3535
#include <stdlib.h> // for malloc, NULL
36-
#include <time.h> // for time
36+
#ifdef DIGRAPHS_ENABLE_STATS
37+
#include <time.h> // for time
38+
#endif
3739

3840
// GAP headers
3941
#include "gap-includes.h"
@@ -46,7 +48,8 @@
4648
#include "digraphs-debug.h" // for DIGRAPHS_ASSERT
4749
#include "homos-graphs.h" // for Digraph, Graph, . . .
4850
#include "perms.h" // for MAXVERTS, UNDEFINED, PermColl, Perm
49-
#include "schreier-sims.h" // for PermColl, . . .
51+
#include "safemalloc.h"
52+
#include "schreier-sims.h" // for PermColl, . . .
5053

5154
////////////////////////////////////////////////////////////////////////////////
5255
// 1. Macros
@@ -1596,7 +1599,7 @@ static bool init_data_from_args(Obj digraph1_obj,
15961599
// srand(time(0));
15971600
is_initialized = true;
15981601
#ifdef DIGRAPHS_ENABLE_STATS
1599-
STATS = malloc(sizeof(HomoStats));
1602+
STATS = safe_malloc(sizeof(HomoStats));
16001603
#endif
16011604

16021605
DIGRAPH1 = new_digraph(MAXVERTS);
@@ -1607,7 +1610,7 @@ static bool init_data_from_args(Obj digraph1_obj,
16071610

16081611
IMAGE_RESTRICT = new_bit_array(MAXVERTS);
16091612
ORB_LOOKUP = new_bit_array(MAXVERTS);
1610-
REPS = malloc(MAXVERTS * sizeof(BitArray*));
1613+
REPS = safe_malloc(MAXVERTS * sizeof(BitArray*));
16111614
for (uint16_t i = 0; i < MAXVERTS; i++) {
16121615
BLISS_GRAPH[i] = bliss_digraphs_new(i);
16131616
REPS[i] = new_bit_array(MAXVERTS);

src/perms.c

+5-6
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,14 @@
1414

1515
#include "perms.h"
1616

17-
// C headers
18-
#include <stdlib.h> // for malloc, . . .
19-
2017
// Digraphs package headers
2118
#include "digraphs-debug.h" // for DIGRAPHS_ASSERT
19+
#include "gap-includes.h" // for ErrorQuit, ADDR_PERM2, ..
20+
#include "safemalloc.h" // for safe_malloc
2221

2322
Perm new_perm(uint16_t const degree) {
2423
DIGRAPHS_ASSERT(degree <= MAXVERTS);
25-
return malloc(degree * sizeof(uint16_t));
24+
return safe_malloc(degree * sizeof(uint16_t));
2625
}
2726

2827
Perm new_perm_from_gap(Obj gap_perm_obj, uint16_t const degree) {
@@ -61,8 +60,8 @@ Perm new_perm_from_gap(Obj gap_perm_obj, uint16_t const degree) {
6160
}
6261

6362
PermColl* new_perm_coll(uint16_t const capacity, uint16_t const degree) {
64-
PermColl* coll = malloc(sizeof(PermColl));
65-
coll->perms = malloc(capacity * sizeof(Perm));
63+
PermColl* coll = safe_malloc(sizeof(PermColl));
64+
coll->perms = safe_malloc(capacity * sizeof(Perm));
6665
for (uint16_t i = 0; i < capacity; ++i) {
6766
coll->perms[i] = new_perm(degree);
6867
}

0 commit comments

Comments
 (0)