Bug 290515

Summary: Kate crash on lookAhead highlighting
Product: [Applications] kate Reporter: Matt Lewis <matthewwalkerlewis>
Component: generalAssignee: KWrite Developers <kwrite-bugs-null>
Status: RESOLVED NOT A BUG    
Severity: crash    
Priority: NOR    
Version First Reported In: unspecified   
Target Milestone: ---   
Platform: Ubuntu   
OS: Linux   
Latest Commit: Version Fixed/Implemented In:
Sentry Crash Report:

Description Matt Lewis 2012-01-03 17:37:59 UTC
Application: kate (3.7.3)
KDE Platform Version: 4.7.3 (4.7.3)
Qt Version: 4.7.4
Operating System: Linux 3.0.0-14-generic x86_64
Distribution: Ubuntu 11.10

-- Information about the crash:
- What I was doing when the application crashed:
Kate crashes when viewing a file using a syntax highlighting rule with both lookAhead="true" and endRegion="regionName".

- Custom settings of the application:
The highlighting file is meant to fold code based on switch and case regions.  Case regions are contained within a switch.  The "end switch" should end both the switch region and the open case region.  The crash only happens with the lookAhead attribute present.

Syntax highlighting file (a minimal version to cause the crash):

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE language SYSTEM "language.dtd">
<language name="EndRegionError" version="1.0" kateversion="3.7.3" section="Other" extensions="*.*">
<highlighting>
	<list name="keywords">
		<item> case </item>
		<item> switch </item>
	</list>

	<contexts>
		<context attribute="Normal Text" lineEndContext="#stay" name="Normal">
			<RegExpr attribute="Keyword" context="#stay" String="\bcase\s+else\b"  endRegion="regCase" beginRegion="regCase"/>
			<RegExpr attribute="Keyword" context="#stay" String="\bcase\b" endRegion="regCase" beginRegion="regCase"/>
			
			<RegExpr attribute="Keyword" context="#stay" String="\bend\s+switch\b" lookAhead="true" endRegion="regCase"/>
			<RegExpr attribute="Keyword" context="#stay" String="\bend\s+switch\b" endRegion="regSwitch" />
			<RegExpr attribute="Keyword" context="#stay" String="\bswitch\b" beginRegion="regSwitch"/>
		</context>
	</contexts>
	
	<itemDatas>
		<itemData name="Normal Text"     defStyleNum="dsNormal" />
		<itemData name="Keyword"         defStyleNum="dsKeyword" />
	</itemDatas>

</highlighting>
<general>
	<keywords casesensitive="1" />
</general>
</language>


Example text:

switch x do
        case foo then
        
        case bar then
        
        case else
        
end switch

switch y do
        
end switch

The crash can be reproduced every time.

-- Backtrace:
Application: Kate (kate), signal: Segmentation fault
[Current thread is 1 (Thread 0x7fbe357f1780 (LWP 12048))]

Thread 3 (Thread 0x7fbe22505700 (LWP 12055)):
#0  0x00007fbe32771034 in __pthread_mutex_lock (mutex=0x298e7f8) at pthread_mutex_lock.c:61
#1  0x00007fbe3229b649 in g_main_context_acquire () from /lib/x86_64-linux-gnu/libglib-2.0.so.0
#2  0x00007fbe3229cd81 in ?? () from /lib/x86_64-linux-gnu/libglib-2.0.so.0
#3  0x00007fbe3229d429 in g_main_context_iteration () from /lib/x86_64-linux-gnu/libglib-2.0.so.0
#4  0x00007fbe33058f3e in QEventDispatcherGlib::processEvents (this=0x29451b0, flags=<optimized out>) at kernel/qeventdispatcher_glib.cpp:424
#5  0x00007fbe3302ccf2 in QEventLoop::processEvents (this=<optimized out>, flags=...) at kernel/qeventloop.cpp:149
#6  0x00007fbe3302cef7 in QEventLoop::exec (this=0x7fbe22504dd0, flags=...) at kernel/qeventloop.cpp:201
#7  0x00007fbe32f4427f in QThread::exec (this=<optimized out>) at thread/qthread.cpp:498
#8  0x00007fbe3300fcbf in QInotifyFileSystemWatcherEngine::run (this=0x298fa40) at io/qfilesystemwatcher_inotify.cpp:248
#9  0x00007fbe32f46d05 in QThreadPrivate::start (arg=0x298fa40) at thread/qthread_unix.cpp:331
#10 0x00007fbe3276eefc in start_thread (arg=0x7fbe22505700) at pthread_create.c:304
#11 0x00007fbe3514c89d in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:112
#12 0x0000000000000000 in ?? ()

Thread 2 (Thread 0x7fbe1f78c700 (LWP 12062)):
#0  pthread_cond_wait@@GLIBC_2.3.2 () at ../nptl/sysdeps/unix/sysv/linux/x86_64/pthread_cond_wait.S:162
#1  0x00007fbe24d5afe2 in ?? () from /usr/lib/x86_64-linux-gnu/libQtScript.so.4
#2  0x00007fbe24d5b019 in ?? () from /usr/lib/x86_64-linux-gnu/libQtScript.so.4
#3  0x00007fbe3276eefc in start_thread (arg=0x7fbe1f78c700) at pthread_create.c:304
#4  0x00007fbe3514c89d in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:112
#5  0x0000000000000000 in ?? ()

Thread 1 (Thread 0x7fbe357f1780 (LWP 12048)):
[KCrash Handler]
#6  QVector<int>::realloc (this=0x7fff7caca520, asize=268435454, aalloc=536870911) at /usr/include/qt4/QtCore/qvector.h:492
#7  0x00007fbe253eb261 in KateHighlighting::doHighlight (this=0x2819b40, prevLine=<optimized out>, textLine=0x2d7a9d0, foldingList=..., ctxChanged=@0x7fff7caca57d) at ../../part/syntax/katehighlight.cpp:394
#8  0x00007fbe253a743c in KateBuffer::doHighlight (this=<optimized out>, startLine=0, endLine=12, invalidate=true) at ../../part/document/katebuffer.cpp:472
#9  0x00007fbe253a8a54 in KateBuffer::ensureHighlighted (this=0x2860570, line=<optimized out>) at ../../part/document/katebuffer.cpp:245
#10 0x00007fbe253884ea in KateDocument::kateTextLine (this=0x285fb20, i=0) at ../../part/document/katedocument.cpp:4747
#11 0x00007fbe2540602c in KateView::setCursorPositionInternal (this=0x2ec87b0, position=..., tabwidth=1, calledExternally=false) at ../../part/view/kateview.cpp:1108
#12 0x00007fbe254064cb in KateView::readSessionConfig (this=0x2ec87b0, config=...) at ../../part/view/kateview.cpp:1172
#13 0x00007fbe33c87394 in KateViewSpace::restoreConfig (this=0x2d3ddb0, viewMan=0x2d2a0e0, config=0x24bc320, groupname=...) at ../../../kate/app/kateviewspace.cpp:240
#14 0x00007fbe33c84f94 in KateViewManager::restoreSplitter (this=0x2d2a0e0, configBase=0x24bc320, group=<optimized out>, parent=0x2d2a0e0, viewConfGrp=...) at ../../../kate/app/kateviewmanager.cpp:850
#15 0x00007fbe33c858ff in KateViewManager::restoreViewConfiguration (this=0x2d2a0e0, config=...) at ../../../kate/app/kateviewmanager.cpp:756
#16 0x00007fbe33c7a9b0 in KateMainWindow::KateMainWindow (this=0x2d22520, sconfig=0x24bc320, sgroup=..., __in_chrg=<optimized out>, __vtt_parm=<optimized out>) at ../../../kate/app/katemainwindow.cpp:204
#17 0x00007fbe33c69414 in KateApp::newMainWindow (this=<optimized out>, sconfig_=<optimized out>, sgroup_=<optimized out>) at ../../../kate/app/kateapp.cpp:390
#18 0x00007fbe33c921b3 in KateSessionManager::activateSession (this=0x28674c0, session=<optimized out>, closeLast=<optimized out>, saveLast=<optimized out>, loadNew=true) at ../../../kate/app/katesession.cpp:338
#19 0x00007fbe33c9359e in KateSessionManager::chooseSession (this=0x28674c0) at ../../../kate/app/katesession.cpp:470
#20 0x00007fbe33c6ae85 in KateApp::startupKate (this=0x7fff7cacb960) at ../../../kate/app/kateapp.cpp:182
#21 0x00007fbe33c6b235 in KateApp::initKate (this=0x7fff7cacb960) at ../../../kate/app/kateapp.cpp:135
#22 0x00007fbe33c6b452 in KateApp::KateApp (this=0x7fff7cacb960, args=<optimized out>) at ../../../kate/app/kateapp.cpp:72
#23 0x00007fbe3540fce6 in kdemain (argc=<optimized out>, argv=<optimized out>) at ../../../kate/app/katemain.cpp:373
#24 0x00007fbe3508c30d in __libc_start_main (main=0x400630 <main(int, char**)>, argc=1, ubp_av=0x7fff7cacce98, init=<optimized out>, fini=<optimized out>, rtld_fini=<optimized out>, stack_end=0x7fff7cacce88) at libc-start.c:226
#25 0x0000000000400661 in _start ()

Possible duplicates by query: bug 285396, bug 284179, bug 281181, bug 277322, bug 267977.

Reported using DrKonqi
Comment 1 Dominik Haumann 2012-07-05 11:50:54 UTC
Sorry, but this will never works. I'm not sure whether you understood how the highlighting works:
If a highlighting rule matches, the rule matching starts from the beginning again. So in your case, the lookAhead rule matches, again and again, and you never proceed with highlighting... This is an endless loop, and, hence, a bug in your example.

Closing all open foldings with a single rule is not supported. You have to be very careful with the lookAhead property, as you run into infinite loops if you do not switch the context...

Hence, I close it as invalid... sorry.