On 07/29/2010 04:32 PM, Pádraig Brady wrote:
No! You can cheat!:) Just pass 4 to u8_mbtouc. There will be no
out-of-bounds access: if the string ends before that, you get either a
complete character or an EILSEQ.

You don't get EILSEQ for u8_mbtouc(&uc,"aa",6);

But you get a complete character and 1 is returned. The point is that
u8_mbtouc will look only one byte past the end of a (valid or invalid)
character, and that one byte will be the nul in the u8_strstr case.

Like this:

  if (!needle[0])
    return haystack;

  n = u8_mbtouc(&uc, needle, 4);
  if (uc == 0xfffd && n != 3)
    return NULL; /* invalid, cannot be incomplete since we pass n=4 */
  if (needle[n])
    return strstr(haystack, needle);
  else
    return u8_strchr(haystack, uc); /* inlining would avoid uctomb */

Paolo


Reply via email to