Skip to content

max_cliques() on a directed graph segfaults (C stack overflow) if any_multiple() was called first (igraph 2.3.3) #2748

Open

Description

@jhollway

What happens, and what did you expect instead?

Calling max_cliques() on a directed graph crashes R with Error: segfault from C stack overflow — but only if any_multiple() has been called on the graph beforehand. Either call alone is fine.

Additional observations:

  • max_cliques(g) alone works (returns 12 cliques, with the expected "Edge directions are ignored" warning). any_multiple(g) alone works.
  • Wrapping the call in suppressWarnings() does not prevent the crash.
  • The crash is specific to any_multiple() as the priming call: which_multiple(g), any_loop(g), or is_connected(g) before max_cliques(g) do not crash.
  • Undirected graphs are unaffected: max_cliques(as_undirected(g, mode = "collapse")) after any_multiple() is fine, suggesting the trigger is the directed-graph warning path in max_cliques().
  • In a sequence that doesn't crash, the "Edge directions are ignored for maximal clique calculations" warning is mis-attributed to has_multiple_impl (In has_multiple_impl(graph = graph) : Edge directions are ignored..., source cliques/maximal_cliques_template.h:221), which may point to stale condition-handler state left by any_multiple().

Found while testing the manynet package, but reproduces with igraph alone in a fresh session.

To reproduce

library(igraph) # 2.3.3
set.seed(1)
m <- matrix(rbinom(64, 1, .5), 8, 8); diag(m) <- 0
g <- graph_from_adjacency_matrix(m) # directed
invisible(any_multiple(g))
max_cliques(g)
#> Error: segfault from C stack overflow

System information

No response

Activity

  1. added
    bug
    on Aug 28, 2026
  2. maelle commented on Aug 28, 2026

    @maelle
    Contributor

    Posting some results from Claude

    Additional reprexes for #2748

    All run just now on this branch (igraph 2.3.3.9033, R 4.5.2) via reprex::reprex().

    1. Original reprex, confirmed as-is

    library(igraph) # 2.3.3
    set.seed(1)
    m <- matrix(rbinom(64, 1, .5), 8, 8); diag(m) <- 0
    g <- graph_from_adjacency_matrix(m) # directed
    invisible(any_multiple(g))
    max_cliques(g)

    This reprex appears to crash R.
    See standard output and standard error for more details.

    Standard output and error

     *** caught segfault ***
    address 0x111000006, cause 'memory not mapped'
    
    Traceback:
     1: max_cliques(g)
       [remaining frames are reprex/rmarkdown/callr internals — omitted]

    2. Smaller/faster deterministic case (n = 5 instead of 8)

    Caught as an R-level condition (Error: segfault from C stack overflow) rather than a hard SIGSEGV, but same underlying cause — and it's exactly the error text from the issue title.

    library(igraph)
    set.seed(1)
    m <- matrix(rbinom(25, 1, .5), 5, 5); diag(m) <- 0
    g <- graph_from_adjacency_matrix(m)
    invisible(any_multiple(g))
    max_cliques(g)
    Error: segfault from C stack overflow
    
    Quitting from r2_small_reprex.spin.Rmd:20-27 [unnamed-chunk-2]

    3. Not max_cliques()-specific — the whole clique family is affected

    library(igraph)
    set.seed(1)
    m <- matrix(rbinom(64, 1, .5), 8, 8); diag(m) <- 0
    
    g1 <- graph_from_adjacency_matrix(m)
    invisible(any_multiple(g1))
    count_max_cliques(g1)

    This reprex appears to crash R.
    See standard output and standard error for more details.

    Standard output and error

    Error: segfault from C stack overflow

    Also confirmed the same way for largest_cliques(g) and clique_num(g) in place of count_max_cliques(g):

    library(igraph)
    set.seed(1)
    m <- matrix(rbinom(64, 1, .5), 8, 8); diag(m) <- 0
    g <- graph_from_adjacency_matrix(m)
    invisible(any_multiple(g))
    largest_cliques(g)

    This reprex appears to crash R.
    See standard output and standard error for more details.

    Error: segfault from C stack overflow
    free(): invalid pointer
    library(igraph)
    set.seed(1)
    m <- matrix(rbinom(64, 1, .5), 8, 8); diag(m) <- 0
    g <- graph_from_adjacency_matrix(m)
    invisible(any_multiple(g))
    clique_num(g)

    This reprex appears to crash R.
    See standard output and standard error for more details.

    Error: segfault from C stack overflow
    double free or corruption (out)

    Note the extra free(): invalid pointer / double free or corruption (out) glibc messages on these two — this isn't pure stack exhaustion, there's real heap corruption involved.

    4. Not any_multiple()-specific either — is_simple() triggers it too

    library(igraph)
    set.seed(1)
    m <- matrix(rbinom(64, 1, .5), 8, 8); diag(m) <- 0
    g <- graph_from_adjacency_matrix(m)
    
    invisible(is_simple(g))
    max_cliques(g)

    This reprex appears to crash R.
    See standard output and standard error for more details.

    Standard output and error

     *** caught segfault ***
    address (nil), cause 'unknown'
    
    Traceback:
     1: max_cliques(g)
       [remaining frames are reprex/rmarkdown/callr internals — omitted]

    has.multiple(g) reproduces it the same way; is.simple(g) is just the dotted alias of is_simple().

    5. Controls that do not trigger it

    library(igraph)
    #> 
    #> Attaching package: 'igraph'
    #> The following objects are masked from 'package:stats':
    #> 
    #>     decompose, spectrum
    #> The following object is masked from 'package:base':
    #> 
    #>     union
    set.seed(1)
    m <- matrix(rbinom(64, 1, .5), 8, 8); diag(m) <- 0
    g <- graph_from_adjacency_matrix(m)
    
    invisible(which_multiple(g))
    max_cliques(g)
    #> [[1]]
    #> ── <vertex sequence> 4/8 · from a7db2fe ────────────────────────────────────────
    #> [1] 8 3 1 2
    #> 
    #> [[2]]
    #> ── <vertex sequence> 4/8 · from a7db2fe ────────────────────────────────────────
    #> [1] 8 3 1 4
    #> 
    #> [[3]]
    #> ── <vertex sequence> 4/8 · from a7db2fe ────────────────────────────────────────
    #> [1] 8 3 5 2
    #> 
    #> [[4]]
    #> ── <vertex sequence> 4/8 · from a7db2fe ────────────────────────────────────────
    #> [1] 8 3 5 4
    #> 
    #> [[5]]
    #> ── <vertex sequence> 4/8 · from a7db2fe ────────────────────────────────────────
    #> [1] 2 3 6 1
    #> 
    #> [[6]]
    #> ── <vertex sequence> 4/8 · from a7db2fe ────────────────────────────────────────
    #> [1] 2 3 6 5
    #> 
    #> [[7]]
    #> ── <vertex sequence> 4/8 · from a7db2fe ────────────────────────────────────────
    #> [1] 2 3 7 1
    #> 
    #> [[8]]
    #> ── <vertex sequence> 4/8 · from a7db2fe ────────────────────────────────────────
    #> [1] 2 3 7 5
    #> 
    #> [[9]]
    #> ── <vertex sequence> 4/8 · from a7db2fe ────────────────────────────────────────
    #> [1] 3 4 6 1
    #> 
    #> [[10]]
    #> ── <vertex sequence> 4/8 · from a7db2fe ────────────────────────────────────────
    #> [1] 3 4 6 5
    #> 
    #> [[11]]
    #> ── <vertex sequence> 4/8 · from a7db2fe ────────────────────────────────────────
    #> [1] 3 4 7 1
    #> 
    #> [[12]]
    #> ── <vertex sequence> 4/8 · from a7db2fe ────────────────────────────────────────
    #> [1] 3 4 7 5

    Standard output and standard error

    -- nothing to show --

    Same result (no crash) for count_multiple(g), any_loop(g), which_loop(g), count_loops(g), and is_connected(g) in place of which_multiple(g).

    This narrows the trigger to functions that return a single "is this graph simple / does it have multi-edges" scalar (any_multiple, is_simple, has.multiple) rather than the per-edge variants (which_multiple, count_multiple) — those likely share one internal C code path, and the clique family's directed-graph handling is what stumbles into whatever state it leaves behind.

    6. Not seed-specific — random directed graphs trigger it generally

    library(igraph)
    set.seed(42)
    g <- sample_gnp(8, 0.5, directed = TRUE)
    invisible(any_multiple(g))
    max_cliques(g)
    Error: segfault from C stack overflow
    
    Quitting from r6_gnp_reprex.spin.Rmd:20-26 [unnamed-chunk-2]

    7. Control: undirected graphs never crash

    library(igraph)
    #> 
    #> Attaching package: 'igraph'
    #> The following objects are masked from 'package:stats':
    #> 
    #>     decompose, spectrum
    #> The following object is masked from 'package:base':
    #> 
    #>     union
    set.seed(1)
    m <- matrix(rbinom(64, 1, .5), 8, 8); diag(m) <- 0
    g <- graph_from_adjacency_matrix(m, mode = "max")
    invisible(any_multiple(g))
    max_cliques(g)
    #> [[1]]
    #> ── <vertex sequence> 4/8 · from 19c86b6 ────────────────────────────────────────
    #> [1] 8 3 1 2
    #> 
    #> [[2]]
    #> ── <vertex sequence> 4/8 · from 19c86b6 ────────────────────────────────────────
    #> [1] 8 3 1 4
    #> 
    #> [[3]]
    #> ── <vertex sequence> 4/8 · from 19c86b6 ────────────────────────────────────────
    #> [1] 8 3 5 2
    #> 
    #> [[4]]
    #> ── <vertex sequence> 4/8 · from 19c86b6 ────────────────────────────────────────
    #> [1] 8 3 5 4
    #> 
    #> [[5]]
    #> ── <vertex sequence> 4/8 · from 19c86b6 ────────────────────────────────────────
    #> [1] 2 3 6 1
    #> 
    #> [[6]]
    #> ── <vertex sequence> 4/8 · from 19c86b6 ────────────────────────────────────────
    #> [1] 2 3 6 5
    #> 
    #> [[7]]
    #> ── <vertex sequence> 4/8 · from 19c86b6 ────────────────────────────────────────
    #> [1] 2 3 7 1
    #> 
    #> [[8]]
    #> ── <vertex sequence> 4/8 · from 19c86b6 ────────────────────────────────────────
    #> [1] 2 3 7 5
    #> 
    #> [[9]]
    #> ── <vertex sequence> 4/8 · from 19c86b6 ────────────────────────────────────────
    #> [1] 3 4 6 1
    #> 
    #> [[10]]
    #> ── <vertex sequence> 4/8 · from 19c86b6 ────────────────────────────────────────
    #> [1] 3 4 6 5
    #> 
    #> [[11]]
    #> ── <vertex sequence> 4/8 · from 19c86b6 ────────────────────────────────────────
    #> [1] 3 4 7 1
    #> 
    #> [[12]]
    #> ── <vertex sequence> 4/8 · from 19c86b6 ────────────────────────────────────────
    #> [1] 3 4 7 5

    Standard output and standard error

    -- nothing to show --

    Summary: the bug isn't limited to any_multiple() + max_cliques() specifically. It's any of {any_multiple, is_simple, has.multiple} (scalar "is-simple" checks) followed by any of {max_cliques, count_max_cliques, largest_cliques, clique_num} (the maximal-cliques family), and only on directed graphs. Per-edge variants (which_multiple, count_multiple) and loop-only checks (any_loop, which_loop, count_loops) don't trigger it, and undirected graphs are unaffected.

  3. maelle commented on Aug 28, 2026

    @maelle
    Contributor

    After more Claude use:

    Root cause found: igraph_has_multiple()/is_simple() cache "no multi-edges" using out-edges only, which is true even when a reciprocal pair (a,b)/(b,a) exists; max_cliques() builds an undirected (IGRAPH_ALL-mode) adjacency list and trusted that cache to skip deduplication, so reciprocal pairs survived as duplicate entries and broke the clique search's recursion. Already fixed upstream in igraph/igraph e547b8007, not yet in a tagged release. Fix for rigraph: re-vendor src/vendor/cigraph from a commit at or after that fix.

  4. krlmlr commented on Sep 3, 2026

    @krlmlr
    Contributor

    Let's hold off patching this, we'll have v1.0.x vendored soon.

  5. szhorvat commented on Oct 1, 2026

    @szhorvat
    Member

    If this repro's with C core 1.x, can you please get a C reproducer and open an issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions