Index: generic/regc_nfa.c
===================================================================
RCS file: /cvsroot/tcl/tcl/generic/regc_nfa.c,v
retrieving revision 1.9
diff -u -r1.9 regc_nfa.c
--- generic/regc_nfa.c	15 Nov 2007 17:07:11 -0000	1.9
+++ generic/regc_nfa.c	15 Nov 2007 21:31:14 -0000
@@ -859,6 +859,25 @@
     }
 
     /*
+     * DGP 2007-11-15: Cloning a state with a circular constraint on its list
+     * of outs can lead to trouble [Bug 1810038], so get rid of them first.
+     */
+
+    for (a = from->outs; a != NULL; a = nexta) {
+	nexta = a->outchain;
+	switch (a->type) {
+	case '^':
+	case '$':
+	case BEHIND:
+	case AHEAD:
+	    if (from == a->to) {
+		freearc(nfa, a);
+	    }
+	    break;
+	}
+    }
+
+    /*
      * First, clone from state if necessary to avoid other outarcs.
      */
 
@@ -997,6 +1016,28 @@
     }
 
     /*
+     * DGP 2007-11-15: Here we duplicate the same protections as appear
+     * in pull() above to avoid troubles with cloning a state with a
+     * circular constraint on its list of ins.  It is not clear whether
+     * this is necessary, or is protecting against a "can't happen".
+     * Any test case that actually leads to a freearc() call here would
+     * be a welcome addition to the test suite.
+     */
+
+    for (a = to->ins; a != NULL; a = nexta) {
+	nexta = a->inchain;
+	switch (a->type) {
+	case '^':
+	case '$':
+	case BEHIND:
+	case AHEAD:
+	    if (a->from == to) {
+		freearc(nfa, a);
+	    }
+	    break;
+	}
+    }
+    /*
      * First, clone to state if necessary to avoid other inarcs.
      */
 
Index: tests/regexp.test
===================================================================
RCS file: /cvsroot/tcl/tcl/tests/regexp.test,v
retrieving revision 1.27
diff -u -r1.27 regexp.test
--- tests/regexp.test	10 May 2005 18:35:23 -0000	1.27
+++ tests/regexp.test	15 Nov 2007 21:31:15 -0000
@@ -655,6 +655,11 @@
 } {{0 -1} {2 1} {4 3}}
 
 
+test regexp-22.1 {Bug 1810038} {
+    regexp ($|^X)* {}
+} 1
+
+
 # cleanup
 ::tcltest::cleanupTests
 return
