Re:Knuth-Morris-Pratt string-searching algorithm: DFA-less v...
by mlyle on Friday January 22, 2021 @04:36PM
(#25876614)
The issue is: the two functions that we're comparing for correctness treat lengths very differently: one scans len-1 bytes, the other scans len+1 bytes.
Even with a null terminator, there isn't a length that would work for both without overrunning the buffer.
printf("%d %d\n", search_ok_1("mok", 2), search_ok_2("mok", 2));
printf("%d %d\n", search_ok_1("mok", 3), search_ok_2("mok", 3)); /* Overruns without null term */
printf("%d %d\n", search_ok_1("mok", 4), search_ok_2("mok", 4)); /* Overruns even WITH null term */
Even with a null terminator, there isn't a length that would work for both without overrunning the buffer.
Prints out: