<font face="Verdana,Arial,Helvetica,sans-serif" size="2"><div>Hi Philippe, other interested folks:</div><div><br></div><div><br></div><div>This code that tries to create an NSRegularExpression out of an invalid pattern will crash due to a HALT:</div><div><br></div><div><i>do {</i></div><div><i> let re = try NSRegularExpression(pattern: "\\", options: [])</i></div><div><i>} catch {</i></div><div><i> print("uh oh")</i></div><div><i>}</i></div><div><br></div><div><br></div><div>I dug around CoreFoundation and have the following failure hypothesis:</div><div><br></div><div>1. In CFRegularExpressionCreate when uregex_open() returns an error, we construct a CFDictionary for the CFError, like this:<br></div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre"> </span> <i>CFStringRef keys[] = {CFSTR("NSInvalidValue”)};</i></div><div><i><span class="Apple-tab-span" style="white-space:pre"> </span> CFTypeRef values[] = {pattern};</i></div><div><i><span class="Apple-tab-span" style="white-space:pre"> </span> CFDictionaryRef userInfo = CFDictionaryCreate(kCFAllocatorSystemDefault, (const void **)keys, (const void **)values, …….)</i></div><div><span class="Apple-tab-span" style="white-space:pre"> </span> </div><div>2. In CFDictionaryCreate( ) each key-value pair is added via the CFAddBasicHashValue( ) function, which eventually tries to calculate the hash of the key. </div><div><br></div><div>3. The hash of the key is calculated like this (all of this is done in CFHash() in CFRuntime.c):</div><div> 3.1) extract the typeID from the key </div><div> 3.2) using the typeID as an index into the CFRuntimeClassTable, get the related CFRuntimeClass (c)</div><div> 3.3) get the has function pointer c->hash</div><div> 3.4) invoke the hash function</div><div> </div><div>4. Now going back to (1), the key here is a CFString returned by CFSTR(). This instance is NOT created using the _CFRuntimeCreateInstance() function</div><div> and hence it does not embed a typeID (the typeID will be 0).</div><div><br></div><div>5. Applying the sequence in (3) to a CFString returned by CFSTR() yields:</div><div> 3.1) typeID = 0</div><div> 3.2) index = 0, c = _CFNotATypeClass is present at index = 0</div><div> 3.3) c->hash = HALT</div><div> 3.4) we call HALT and hence stop</div><div><br></div><div><br></div><div>A validation of the above hypothesis (and a way to work around this is) is by creating the key in (1) this way:</div><div><br></div><div><i>CFStringRef keys[] = { CFStringCreateWithCString(kCFAllocatorSystemDefault, "NSInvalidValue", kCFStringEncodingUTF8) };</i></div><div><br></div><div>The crash goes away.</div><div><br></div><div>I see a lot of places in CoreFoundation where CFString instances returned by CFSTR() are used as keys into a CFDictionary. I would say all of them are potentials points of failure per the above hypothesis.</div><font face="Verdana,Arial,Helvetica,sans-serif" size="2"><font><font><div class="socmaildefaultfont" dir="ltr"><div class="socmaildefaultfont" dir="ltr"><div class="socmaildefaultfont" dir="ltr"><div dir="ltr" style="font-style: normal;"><br></div><div dir="ltr" style="font-style: normal;"><br></div><div dir="ltr" style="font-style: normal;">Do you agree with the failure theory? What do you think we should be doing to fix this? </div><div dir="ltr" style="font-style: normal;"><br></div><div dir="ltr" style="font-style: normal;"><br><font face="Sans Serif, Verdana, Arial, Helvetica, sans-serif">Pushkar N Kulkarni,</font></div>
<div dir="ltr" style="font-style: normal;"><font face="Sans Serif, Verdana, Arial, Helvetica, sans-serif">IBM Runtimes</font></div><div dir="ltr" style="font-style: normal;"><font face="Sans Serif, Verdana, Arial, Helvetica, sans-serif"><br></font></div><div dir="ltr"><font face="serif, Times New Roman, Times, serif"><i>Simplicity is prerequisite for reliability - Edsger W. Dijkstra</i></font></div>
<div dir="ltr" style="font-style: normal; font-size: 10.5pt; font-family: Arial;"><br></div></div></div></div></font></font></font></font><BR>