It is possible to construct (invalid) input to Graphviz that leads to two copies
of the same node being encountered during UF_union(). Previously this would
cause an integer overflow. This was detectable with Undefined Behavior Sanitizer
using the following input:
digraph G { {rank=same a b n A;C;E;G;I;K;M;O;Q;S;U;W;Y;
B;D;F;H;J;L;N;P;R;T;V;X;Z; }
a{rank=same a b A;C;E;G;I;K;M;O;Q;S;U;W;Y B;D;F;H;J;L;N;P;R;T;V;X;Z;
}Courier6;
} ?
We now anticipate this scenario and handle it gracefully, copying the pattern
from UF_union() in lib/spine/union_find.c. Fixes #1682. This issue was
originally found by the Google Autofuzz project.
ND_UF_size(v) = 1;
} else
v = UF_find(v);
+ /* if we have two copies of the same node, their union is just that node */
+ if (u == v)
+ return u;
if (ND_id(u) > ND_id(v)) {
ND_UF_parent(u) = v;
ND_UF_size(v) += ND_UF_size(u);