Skip to content

Commit

Permalink
fix memory leak in vector allocation
Browse files Browse the repository at this point in the history
Address Sanitizer detected a few instances of the following:

  Direct leak of 168 byte(s) in 7 object(s) allocated from:
    #0 0x7f4bc2724330 in __interceptor_malloc (/lib/x86_64-linux-gnu/libasan.so.5+0xe9330)
    ellson#1 0x7f4bc24b1ea3 in vec_new /tmp/tmp.m68Z0dFAYs/graphviz/lib/common/routespl.c:918
    ellson#2 0x7f4bc24b291a in find_all_cycles /tmp/tmp.m68Z0dFAYs/graphviz/lib/common/routespl.c:1073
    ellson#3 0x7f4bc24b2b64 in get_cycle_centroid /tmp/tmp.m68Z0dFAYs/graphviz/lib/common/routespl.c:1121
    ellson#4 0x7f4bc24b3c5c in makeStraightEdges /tmp/tmp.m68Z0dFAYs/graphviz/lib/common/routespl.c:1233
    ellson#5 0x7f4bc24b35f8 in makeStraightEdge /tmp/tmp.m68Z0dFAYs/graphviz/lib/common/routespl.c:1203
    ellson#6 0x7f4bbdf28b52 in _spline_edges /tmp/tmp.m68Z0dFAYs/graphviz/lib/neatogen/neatosplines.c:659
    ellson#7 0x7f4bbdf29240 in splineEdges /tmp/tmp.m68Z0dFAYs/graphviz/lib/neatogen/neatosplines.c:736
    ellson#8 0x7f4bbdf29310 in spline_edges1 /tmp/tmp.m68Z0dFAYs/graphviz/lib/neatogen/neatosplines.c:749
    ellson#9 0x7f4bbdf293be in spline_edges0 /tmp/tmp.m68Z0dFAYs/graphviz/lib/neatogen/neatosplines.c:779
    ellson#10 0x7f4bbdf29bcb in spline_edges /tmp/tmp.m68Z0dFAYs/graphviz/lib/neatogen/neatosplines.c:817
    ellson#11 0x7f4bbdf14ea6 in neato_layout /tmp/tmp.m68Z0dFAYs/graphviz/lib/neatogen/neatoinit.c:1489
    ellson#12 0x7f4bc242b6fc in gvLayoutJobs /tmp/tmp.m68Z0dFAYs/graphviz/lib/gvc/gvlayout.c:85
    ellson#13 0x559fcd474960 in main /tmp/tmp.m68Z0dFAYs/graphviz/cmd/dot/dot.c:132
    ellson#14 0x7f4bc21d709a in __libc_start_main ../csu/libc-start.c:308

find_all_cycles() was calling vec_delete() on the temporary vector it allocated,
but it did not take into account that the nested vectors were not freed by
vec_delete(). This change moves the open coded clean up logic from
get_cycle_centroid() into vec_delete() to fix this and make it less likely that
this is missed in future.

This was initially encountered while investigating #1554.
  • Loading branch information
Smattr committed Jun 25, 2020
1 parent 5ec0aa2 commit c4ef19f
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions lib/common/routespl.c
Original file line number Diff line number Diff line change
Expand Up @@ -935,6 +935,10 @@ static void* vec_get(vec* pvec, size_t index)

static void vec_delete(vec* pvec)
{
size_t i;
for (i = 0; i < vec_length(pvec); ++i) {
vec_delete(vec_get(pvec, i));
}
free(pvec->_mem);
free(pvec);
}
Expand Down Expand Up @@ -1112,10 +1116,6 @@ static pointf get_cycle_centroid(graph_t *g, edge_t* edge)
if (cycles == 0 || ref_g != g) {
//free the memory we're using to hold the previous cycles
if (cycles != 0) {
size_t i;
for (i=0; i < vec_length(cycles); ++i) {
vec_delete(vec_get(cycles, i));
}
vec_delete(cycles);
}
cycles = find_all_cycles(g);
Expand Down

0 comments on commit c4ef19f

Please sign in to comment.