Tk Source Code

View Ticket
Login
Ticket UUID: ad458a3a72112080ed073c3aa0cc0e1704eea1d3
Title: Sections consistency assertion fails when switching -elide in the revised text widget
Type: Bug Version: revised_text
Submitter: nalingupta Created on: 2025-02-01 20:39:16
Subsystem: 18. [text] Assigned To: fvogel
Priority: 5 Medium Severity: Severe
Status: Closed Last Modified: 2026-10-13 19:54:57
Resolution: Fixed Closed By: nobody
    Closed on:
Description:
TIP 466: Revised Implementation of the Text Widget

Sections consistency assertion fails "CheckSections: too few segments in section".

Steps to reproduce bug:
 (1) Include fix from ticket bdd1f64523 over code from https://github.com/tcltk/tk/tree/revised_text/generic
 (2) Using Debug-Build, run following code
      pack [text .t]
      .t tag configure tag_ellipse  -elide 0
      .t tag configure tag_hidden   -elide 1
      .t insert end "text_ellipse" tag_ellipse
      .t insert end "text_hidden"  tag_hidden
      .t insert end "text_ellipse" tag_ellipse
      .t insert end "text_hidden"  tag_hidden
      .t tag configure tag_ellipse  -elide 1
      .t tag configure tag_hidden   -elide 0
      --> assert failure in tkTextBTree.c CheckSections()
          Tcl_Panic("CheckSections: too few segments in section");

Analysis (code analysis also below)
--------
                                         Issue: Missing Branch
                                                    |
          ___________            ___________       \ /__
         |           |          |           |       '   |
 seg1--seg2--seg3--seg4--seg5--seg6--seg7--seg8--seg9--seg10--seg11--seg12--seg13
 mark  brch  char  link  char  brch  char  link  char  link    mark   mark  char
  |     |     |     |     |     |     |      |     |      |     |     |     |
   \   /      |      \    |    /      |       \   /        \     \   /     /
    \ /       |       \   |   /       |        \ /           \    \ /    /
  section1 section2   section3     section4  section5          section6
  (len=2)  (len=1)    (len=3)      (len=1)    (len=2)           (len=4)


- rootPtr 0x000001567deb80d0 Node *
 ...
 - linePtr 0x000001567debfff0 TkTextLine *
  ...
  +  nextPtr 0x000001567dea6700 TkTextLine *
  +  prevPtr 0x0000000000000000 TkTextLine *
  -  segPtr 0x000001567ded37c0 TkTextSegment *
   +  typePtr 0x00007ff923401c30 tkTextLeftMarkType "mark" Tk_SegType *
   -  nextPtr 0x000001567dec5a80 TkTextSegment *
    +  typePtr 0x00007ff9233f9e78 tkTextBranchType "branch" Tk_SegType *
    -  nextPtr 0x000001567deb58c0 TkTextSegment *
     +  typePtr 0x00007ff9233f9e40 tkTextCharType "character" Tk_SegType *
     -  nextPtr 0x000001567dec6f80 TkTextSegment *
      +  typePtr 0x00007ff9233f9eb0 tkTextLinkType "connection" Tk_SegType *
      -  nextPtr 0x000001567deb1bb0 TkTextSegment *
       +  typePtr 0x00007ff9233f9e40 tkTextCharType "character" Tk_SegType *
       -  nextPtr 0x000001567dee0470 TkTextSegment *
        +  typePtr 0x00007ff9233f9e78 tkTextBranchType "branch" Tk_SegType *
        -  nextPtr 0x000001567deb8250 TkTextSegment *
         +  typePtr 0x00007ff9233f9e40 tkTextCharType "character" Tk_SegType *
         -  nextPtr 0x000001567dea3d00 TkTextSegment *
          +  typePtr 0x00007ff9233f9eb0 tkTextLinkType "connection"Tk_SegType *
          -  nextPtr 0x000001567dec5e80 TkTextSegment *
           +  typePtr 0x00007ff9233f9e40 tkTextCharType "character" Tk_SegType *
           -  nextPtr 0x000001567ded3dc0 TkTextSegment *
            +  typePtr 0x00007ff9233f9eb0 tkTextLinkType "connection" Tk_SegType *
            -  nextPtr 0x000001567dea8510 TkTextSegment *
             +  typePtr 0x00007ff923401c68 tkTextRightMarkType "mark" Tk_SegType *
             -  nextPtr 0x000001567dec1cf0 TkTextSegment *
              +  typePtr 0x00007ff923401c68 tkTextRightMarkType "mark" Tk_SegType *
              -  nextPtr 0x000001567debff70 TkTextSegment *
               +  typePtr 0x00007ff9233f9e40 tkTextCharType "character" Tk_SegType *
               +  nextPtr 0x0000000000000000 TkTextSegment *


Code analysis
-------------
TkBTreeUpdateElideInfo()
{
  ...
  while () {
    ...
    UpdateElideInfo( firstSegPtr=first-instance-of-"hidden_text", search.segPtr=second-instance-of-"ellipse_text"


UpdateElideInfo()
{
  ...
  if (!(endSegPtr = endSegPtr->nextPtr)) {  -->moves endSegPtr to second-instance-of-"hidden_text"  line 8673


Root-cause
----------
Moving endSegPtr to second-instance-of-"hidden_text" causes code following this statement to generate inconsistent segment-chain which has a "branch" missing.
User Comments: nab added on 2026-10-13 19:54:57:

Merged into revised_text with [07716463c0|07716463c0], as you asked. Thank you.


serhiy.storchaka added on 2026-10-10 11:36:33:

The scripts from this ticket pass on revised_text since [f80a5b2df7], but removing a tag from the end of a range elided together with an adjacent range of another tag still left lines elided without switches:

pack [text .t]; .t debug 1
.t tag configure A -elide 1; .t tag configure B -elide 1
for {set i 1} {$i <= 9} {incr i} {.t insert end "row $i\n"}
.t tag add B 2.0 4.0; .t tag add A 4.0 8.0
.t tag remove B 3.0 6.0
UpdateElideInfo() skipped lines 4 and 5 because they do not carry B, although they are elided by A. Proposed fix in branch elide-walk-skipped-lines, [8d0c690381], test btree-14.120.


fvogel added on 2025-02-03 22:59:14:
Code analysis
-------------

When processing the last statement, i.e. '.t tag configure tag_ellipse -elide 1', at the call to UpdateElideInfo():

1. UpdateElideInfo() is first called with  tagPtr{name=tag_ellipse, elide=1)  firstSegPtr=seg2 ("text_1")  lastSegPtr=seg4 ("text_2") reason=ELISION_HAS_BEEN_CHANGED

2. UpdateElideInfo() is then called with  tagPtr{name=tag_ellipse, elide=1)  firstSegPtr=seg6 ("text_3")  lastSegPtr=seg8 ("text_4") reason=ELISION_HAS_BEEN_CHANGED

After execution of the first call to UpdateElideInfo(), the line architecture is correct (there is a newly inserted branch segment: seg13):
         __________________             ___________ 
        |                  |           |           |
 seg1--seg13--seg2--seg4--seg5--seg6--seg7--seg8--seg9--seg10--seg11--seg12
 mark  brch   char  char  link  char  brch  char  link  mark   mark   char
  |     |      |     |     |     |      |    |      |    |       |     |
   \   /        \   /       \    \     /     |       \    \     /     /
    \ /          \ /         \    |   /      |        \    \   /     /
  section1     section2       section3    section4       section5
  (len=2)      (len=2)        (len=3)     (len=1)        (len=4)
             (text_1text2)    (text_3)    (text_4)       (\n)
               (elided)                   (elided)


After execution of the second call to UpdateElideInfo(), we get:

         _____________<???>____________ 
        |                              |
 seg1--seg13--seg2--seg4--seg6--seg8--seg9--seg10--seg11--seg12
 mark  brch   char  char  char  char  link  mark   mark   char
  |     |      |     |     |     |      |    |      |      |
   \   /        \    |     |    /        \    \    /      /
    \ /          \   |     |   /          \    |  |      /
  section1           section2                  section3
  (len=2)            (len=4)                   (len=4)
               (text_1text2text_3text_4)       (\n)
                     (elided)

The connection between the branch and the link is missing!
The seg13 branch points to something that is not a link (typePtr is NULL)
The link points to something that is not a branch (typePtr is NULL)

--> KABOOM!

We're narrowing down. Stay tuned to our next issue!

fvogel added on 2025-02-02 21:19:55:
An analysis start (adopting the formalism you have used):

I'm using a slightly modified script (this eases debugging):

package require Tk
pack [text .t]
.t tag configure tag_hidden -elide 1
.t insert end "text_1" tag_ellipse
.t insert end "text_2" tag_hidden
.t insert end "text_3" tag_ellipse
.t insert end "text_4" tag_hidden
.t tag configure tag_ellipse -elide 1  ; # assertion fails "BranchCheckProc: connection is not linked" WHEN tkBTreeDebug = 1 ONLY


Snapshot before last statement
------------------------------

linePtr = 0x00000209288e0550
linePtr->numBranches = 2
linePtr->numLinks = 2
linePtr->segPtr = seg1Ptr = 0x00000209288c1560
linePtr->segPtr->sectionPtr 0x0000020928885570
linePtr->lastPtr = 0x00000209288f24a0

	addr			type			value
seg1Ptr->prevPtr=NULL
seg1:	0x00000209288c1560	tkTextLeftMarkType
seg2:	0x00000209289a7be0	tkTextCharType		"text_1"
seg3:	0x00000209289a7b60	tkTextBranchType
seg4:	0x00000209289a7ce0	tkTextCharType		"text_2"	"tag_hidden"->elide=1
seg5:	0x00000209289a7960	tkTextLinkType
seg6:	0x00000209289982a0	tkTextCharType		"text_3"
seg7:	0x0000020928958bd0	tkTextBranchType
seg8:	0x00000209288cca00	tkTextCharType		"text_4"	"tag_hidden"->elide=1
seg9:	0x000002092aa29040	tkTextLinkType
seg10:	0x000002092831c020	tkTextRightMarkType
seg11:	0x000002092831b0a0	tkTextRightMarkType
seg12:	0x00000209288f24a0	tkTextCharType		"\n"
seg12->nextPtr=NULL

sec1Ptr->prevPtr=NULL
sec1:	0x0000020928885570	size=6	len=3	linePtr=0x00000209288e0550	seg1+seg2+seg3
sec2:	0x00000209288294c0	size=6	len=1	linePtr=0x00000209288e0550	seg4
sec3:	0x00000209288bbbc0	size=6	len=3	linePtr=0x00000209288e0550	seg5+seg6+seg7
sec4:	0x000002092889fb10	size=6	len=1	linePtr=0x00000209288e0550	seg8
sec5:	0x000002092888dc90	size=1	len=4	linePtr=0x00000209288e0550	seg9+seg10+seg11+seg12
sec5->nextPtr=NULL

Each branch points to its corresponding link, and each link points to its corresponding branch.

Representing the situation graphically, it is easy to see that everything is correct at this point:
               ___________             ___________
              |           |           |           |
 seg1--seg2--seg3--seg4--seg5--seg6--seg7--seg8--seg9--seg10--seg11--seg12
 mark  char  brch  char  link  char  brch  char  link  mark   mark   char
  |     |     |     |     |     |     |     |      |    |       |     |
   \    |    /      |      \    |    /      |       \    \     /     /
    \   |   /       |       \   |   /       |        \    \   /     /
     section1    section2    section3    section4        section5
     (len=3)     (len=1)     (len=3)     (len=1)         (len=4)
                 (elided)                (elided)


Next step is code analysis when processing the last statement, i.e. '.t tag configure tag_ellipse -elide 1', at the call to UpdateElideInfo(). Stay tuned.

fvogel added on 2025-02-02 08:29:56:

Thank you for the short script, this is so useful! It makes a world of difference when trying to solve the problem.

One useful trick: When you build the revised text widget with

int tkBTreeDebug = 1;
in tkTextBtree.c:135, then quite some more consistency checks are exercised at runtime.

In the present case, when running your provided script one gets a panic in BranchCheckProc(): "BranchCheckProc: connection is not linked".

This happens before the reported CheckSections panic triggered by the second tag configure command from your script. It happens when running the first tag configure command, so it is very interesting in narrowing down the issue.

And this time it looks very much like [73d8d94916] (same panic) for which we didn't yet have a short reproducible script. Great!