# Segmentation fault when using "layerselect"

**URL:** <https://forum.graphviz.org/t/segmentation-fault-when-using-layerselect/3077>\
**Category:** Help\
**Created:** [July 5, 2025, 8:13pm UTC](https://forum.graphviz.org/t/segmentation-fault-when-using-layerselect/3077 "2025-07-05T20:13:48Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![decision-making-mike](https://avatars.discourse-cdn.com/v4/letter/d/9dc877/32.png) [@decision-making-mike](https://forum.graphviz.org/u/decision-making-mike)\
**Post date:** [July 5, 2025, 8:13pm UTC](https://forum.graphviz.org/t/segmentation-fault-when-using-layerselect/3077/1 "2025-07-05T20:13:48Z")

</div>

I’ve recently started to experiment with layers in Graphviz. I’ve however noticed that “dot” produces segmentation fault when I want to use any other layer in the `layerselect` attribute than the first one listed in the `layers` attribute. I’ve worked out that the behavior can be reproduced with the following minimal Bash code,

```bash
echo 'graph{}' | dot -Tjpg -Glayers='a b' -Glayerselect='b'

```

The result is the text “Segmentation fault (core dumped)”. The exit status is “139”. The expected behavior is that an “empty” diagram be produced. Note that I use the second layer in `layerselect`.

On the other hand, when I run nearly identical code,

```bash
echo 'graph{}' | dot -Tjpg -Glayers='a b' -Glayerselect='a'

```

then there is correctly produced a JPG file with an “empty” diagram (I can display it with `feh`), no errors. The exit status is “0”. Note that now I use the first layer in `layerselect`.

**Is this an expected behavior or a bug? If this is an expected behavior, what am I doing wrong? Is there a workaround so that I be able to use any other layer in `layerselect` than the first one listed in `layers`?**

`dot -V` gives `dot - graphviz version 13.1.0 (20250701.0955)`.

`uname -srm` gives `Linux 5.4.0-216-generic x86_64`.

If you need more details, please tell which ones and I’ll post them.

---

<div class="post-metadata">

**Author:** ![steveroush](https://avatars.discourse-cdn.com/v4/letter/s/a9adbd/32.png) [@steveroush](https://forum.graphviz.org/u/steveroush)\
**Post date:** [July 6, 2025, 12:28am UTC](https://forum.graphviz.org/t/segmentation-fault-when-using-layerselect/3077/2 "2025-07-06T00:28:23Z")

</div>

A known bug ([Using layerselect to produce a single layer fails for many output formats (#1648) · Issues · graphviz / graphviz · GitLab](https://gitlab.com/graphviz/graphviz/-/issues/1648)).

However, it will work as desired it you set the output format to svg, ps (Postscript), or pdf.

---

<div class="post-metadata">

**Author:** ![decision-making-mike](https://avatars.discourse-cdn.com/v4/letter/d/9dc877/32.png) [@decision-making-mike](https://forum.graphviz.org/u/decision-making-mike)\
**Post date:** [July 6, 2025, 2:07pm UTC](https://forum.graphviz.org/t/segmentation-fault-when-using-layerselect/3077/3 "2025-07-06T14:07:47Z")

</div>

Steve, thank you for that. It looks the bug you’ve reported is related to what I’ve encountered. If not the same, but to be sure I would need to see the `layer.gv` file, which you’re using in the code there.

I’m not able to accurately analyze the C patch you’ve proposed, but anyway, thank you for that! If it works for you, it’s one step closer to having it resolved in production. Can you say why the bug hasn’t been fixed so far?

I can confirm that segmentation fault in my case happens only for some file formats. Just to be sure and comprehensive, I’ve checked the formats that `dot` returns as a hint for an unrecognized format. (By the way, it’s much more than there’s listed in the “OUTPUT FORMATS” section in `man dot`. Shouldn’t it be the same list?) So, for the Bash code

```bash
{
    for x in 'bmp' 'canon' 'cmap' 'cmapx' 'cmapx_np' 'dot' 'dot_json' 'eps' 'fig' 'gv' 'ico' 'imap' 'imap_np' 'ismap' 'jpe' 'jpeg' 'jpg' 'json' 'json0' 'kitty' 'kittyz' 'pdf' 'pic' 'plain' 'plain-ext' 'png' 'pov' 'ps' 'ps2' 'svg' 'svg_inline' 'svgz' 'tif' 'tiff' 'tk' 'vt' 'vt-24bit' 'vt-4up' 'vt-6up' 'vt-8up' 'x11' 'xdot' 'xdot1.2' 'xdot1.4' 'xdot_json' 'xlib'
    do
        echo "$x"
        echo 'graph{}' \
            | >/dev/null dot -T"$x" -Glayers='a b' -Glayerselect='b' \
                && echo
    done
} &>/tmp/dot.log

# Here I am just formatting the output nicely
</tmp/dot.log paste -d ' ' - - \
    | gawk -F' ' '{
        printf ( "%s" , $1 )
        for ( x = 0 ; x < 16 - length ( $1 ) ; ++x ) {
            printf ( " " )
        }
        $1 = ""
        print $0
    }' \
    | sort -k2

```

I get

```auto
svg_inline      
xdot_json       
cmapx_np        
dot_json        
imap_np         
xdot1.2         
xdot1.4         
cmapx           
ismap           
json0           
cmap            
imap            
json            
svgz            
xdot            
eps             
pdf             
ps2             
svg             
ps              
gv Segmentation fault (core dumped)
tk Segmentation fault (core dumped)
vt Segmentation fault (core dumped)
bmp Segmentation fault (core dumped)
dot Segmentation fault (core dumped)
fig Segmentation fault (core dumped)
ico Segmentation fault (core dumped)
jpe Segmentation fault (core dumped)
jpg Segmentation fault (core dumped)
pic Segmentation fault (core dumped)
png Segmentation fault (core dumped)
pov Segmentation fault (core dumped)
tif Segmentation fault (core dumped)
x11 Segmentation fault (core dumped)
jpeg Segmentation fault (core dumped)
tiff Segmentation fault (core dumped)
xlib Segmentation fault (core dumped)
canon Segmentation fault (core dumped)
kitty Segmentation fault (core dumped)
plain Segmentation fault (core dumped)
kittyz Segmentation fault (core dumped)
vt-4up Segmentation fault (core dumped)
vt-6up Segmentation fault (core dumped)
vt-8up Segmentation fault (core dumped)
vt-24bit Segmentation fault (core dumped)
plain-ext Segmentation fault (core dumped)

```

* * *

**Update.** I’ve noticed that running the following code I don’t have segmentation fault, although I would expect it.

```bash
echo 'graph{}' | dot -Tjpg -Glayers='2 1' -Glayerselect='1'

```

* * *

**Update.** As for my question about a workaround, for now I’ve resolved to set the output format to `pdf`. Then I can convert the file to a JPG or PNG (e.g. using the `convert` tool).

---

<div class="post-metadata">

**Author:** ![steveroush](https://avatars.discourse-cdn.com/v4/letter/s/a9adbd/32.png) [@steveroush](https://forum.graphviz.org/u/steveroush)\
**Post date:** [July 6, 2025, 7:20pm UTC](https://forum.graphviz.org/t/segmentation-fault-when-using-layerselect/3077/4 "2025-07-06T19:20:26Z")

</div>

Nope, I can’t address why this bug has not been fixed. I am not a compentent C coder, more of a free-range assistant.  
While I think **layers** are cool, you and I are in the minority.

Here is layer.gv (part of the source):

```auto
// SOURCE: Graphviz Source ./tests/graphs/layer.gv
digraph G {
	layers="local:pvt:test:new:ofc";

	node1 [layer="pvt"];
	node2 [layer="all"];
	node3 [layer="pvt:ofc"]; /* pvt, test, new, and ofc */
	node2 -> node3 [layer="pvt:all"];	/* same as pvt:ofc */
	node2 -> node4 [layer=3]; /* same as test */
}

```

Here is a different workaround. It uses **gvpr** to rearrange the **layers** value to put the desired layer in the 1st position. The gvpr code is a little fiddly, and assumes the standard values for **layersep** ([layersep | Graphviz](https://www.graphviz.org/docs/attrs/layersep/)), but it seems to work.

```auto
L= ## <<assign the desired layer here
f= ## << assign your file name here
T=png; ## or any other desired format
F=${f%.*};
gvpr -a"$L" -c '
BEG_G{
int n,i;string tok[int];
n=tokens(layers,tok," :\t");
layers=ARGV[0];
for (tok[i])if (tok[i]!=ARGV[0])
  layers=layers+" "+tok[i];
}' $f |
dot -Glayerselect="$L" -T$T -o$F.$T;

```

Whoops, the _man_ page is out-of-date.  
Here is a better reference: [Output Formats | Graphviz](https://www.graphviz.org/docs/outputs/)

---

<div class="post-metadata">

**Author:** ![decision-making-mike](https://avatars.discourse-cdn.com/v4/letter/d/9dc877/32.png) [@decision-making-mike](https://forum.graphviz.org/u/decision-making-mike)\
**Post date:** [July 8, 2025, 2:07am UTC](https://forum.graphviz.org/t/segmentation-fault-when-using-layerselect/3077/5 "2025-07-08T02:07:51Z")

</div>

Thanks for `layer.gv`. I guess then that our problems are the same. Irrespectively of whether a layer exists or not, if it’s not the first listed, Graphviz produces a segmentation fault.

Also, thanks for the workaround in gvpr! I don’t know that language, but your code looks quite simple. But I don’t know if it’s going to be of any help to me. I have yet to see how I’m going to use layers.

Also, thanks for the reference. Well, I now have read the man page more carefully, and it says that

> to see what output formats your installation of dot supports[,] you can use ``dot -T:‘’ and check the warning message.

So you can say that this hint, in a way, makes the man page always up to date.

---

<div class="post-metadata">

**Author:** ![smattr](https://sea2.discourse-cdn.com/graphviz/user_avatar/forum.graphviz.org/smattr/32/85_2.png) [@smattr](https://forum.graphviz.org/u/smattr)\
**Post date:** [July 8, 2025, 10:37am UTC](https://forum.graphviz.org/t/segmentation-fault-when-using-layerselect/3077/6 "2025-07-08T10:37:29Z")

</div>

If all goes smoothly, this will be fixed in the next Graphviz release: [fix: anticipate layers with null identifiers (!4436) · Merge requests · graphviz / graphviz · GitLab](https://gitlab.com/graphviz/graphviz/-/merge_requests/4436)

---

<div class="post-metadata">

**Author:** ![decision-making-mike](https://avatars.discourse-cdn.com/v4/letter/d/9dc877/32.png) [@decision-making-mike](https://forum.graphviz.org/u/decision-making-mike)\
**Post date:** [July 8, 2025, 3:47pm UTC](https://forum.graphviz.org/t/segmentation-fault-when-using-layerselect/3077/7 "2025-07-08T15:47:22Z")

</div>

Thanks, Matthew! I did see yesterday your activity in the GitLab issue, but generally I didn’t expect anything to be done so quick! (Maybe this thread has influenced it, maybe not, I don’t know, but am glad either way.)

Steve, will you mind I un-mark your post in this thread as a “solution”, and mark Matthew’s instead?

---

<div class="post-metadata">

**Author:** ![steveroush](https://avatars.discourse-cdn.com/v4/letter/s/a9adbd/32.png) [@steveroush](https://forum.graphviz.org/u/steveroush)\
**Post date:** [July 8, 2025, 5:52pm UTC](https://forum.graphviz.org/t/segmentation-fault-when-using-layerselect/3077/8 "2025-07-08T17:52:39Z")

</div>

I do not mind at all. @smattr is doing the heavy lifting

---

<div class="post-metadata">

**Author:** ![smattr](https://sea2.discourse-cdn.com/graphviz/user_avatar/forum.graphviz.org/smattr/32/85_2.png) [@smattr](https://forum.graphviz.org/u/smattr)\
**Post date:** [July 9, 2025, 7:06am UTC](https://forum.graphviz.org/t/segmentation-fault-when-using-layerselect/3077/9 "2025-07-09T07:06:53Z")

</div>

We have too few hands and too many bugs. So it’s mostly a case of the squeaky wheel getting the grease. This thread prompted me to take a look at that issue.

---

<div class="post-metadata">

**Author:** ![decision-making-mike](https://avatars.discourse-cdn.com/v4/letter/d/9dc877/32.png) [@decision-making-mike](https://forum.graphviz.org/u/decision-making-mike)\
**Post date:** [July 9, 2025, 9:20pm UTC](https://forum.graphviz.org/t/segmentation-fault-when-using-layerselect/3077/10 "2025-07-09T21:20:48Z")

</div>

Well, GitLab says Graphviz is mostly C, so if I knew C better, I could offer some help. But now I can only say that if I find another bug, I’ll do my best in helping to fix it, even if it means just reminding about it.
