Note that there are some explanatory texts on larger screens.

plurals
  1. POBorland string::find bug
    primarykey
    data
    text
    <p>I'm supporting a C++ application written using Borland C++ Builder 5.02 (from 1997). The find() method on the Borland string class does not behave how I would expect:</p> <pre><code>#include &lt;cstring&gt; #include &lt;iostream&gt; int main (int argc, char *argv[]) { string needle = "length == eighteen"; string haystack = "&lt;" + needle + "&gt;"; if (haystack.find(needle) != NPOS) cout &lt;&lt; "Found it!" &lt;&lt; endl; else cout &lt;&lt; "Not found" &lt;&lt; endl; return 0; } </code></pre> <p>This program outputs <code>Not found</code>. If I change the needle to something shorter it outputs <code>Found it!</code>. If I exchange the angle brackets for some other characters it finds it. Spaces work, but parentheses also don't.</p> <p>Note that I am using the Borland string library here: if I <code>#include &lt;string&gt;</code> and use <code>std::string</code> instead then it works exactly how I would expect. Sadly changing the whole application to use STL strings is not a feasible answer!</p> <p>From the documentation it seems that Borland uses a hash-based algorithm for string search. I can't find any more details about this, and I've stepped through the disassembly but am not much the wiser.</p> <p>I find it very hard to believe that this is really a bug in the string library, particularly since if it were then I would expect to be able to find an article or something about it. I can't find any such information.</p> <p>However, I've run out of ideas! Is this a known bug? Is there a fix?</p> <p>EDIT: Having looked again at the disassembly, I think it's trying to do something like the Rabin-Karp algorithm, where the hash function is calculated mod 33554393 (the largest prime &lt; 2^25). It could well be the polynomial hash function with a base of 32 (i.e. a_0 + 32 a_1 + 32^2 a_2 + .. + 32^n a_n) but that's just a hunch. Sounds like a possible overflow as Daniel Fischer suggested.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload