News for hackers. Stuff that matters.
Re:Sentinels Can Be Faster by jiggawatts on Wednesday September 09, 2020 @07:51PM (#24427353)
EDIT: Never mind, I mis-read the code in the blog...
Re:Sentinels Can Be Faster by klodolph on Wednesday September 09, 2020 @08:13PM (#24427514)
I think you may have misinterpreted the code.

> Zero spaces is a number of leading spaces. If you feed his efficient solution a string without a leading space, it'll read past the end of the string and into undefined memory until the program either crashes or reads some arbitrary space character (0x20) from some other string. If it contains spaces but doesn't start with one, you'll think it has more leading spaces than it actually does.

I don't know what code you are referring to, but I'm looking at this code:

    const char * skip_leading_spaces(const char * start) {
      while(is_space(*start)) {
        start++;
      }
      return start;
    }
And this correctly handles the case with zero leading spaces. It correctly handles empty strings.

Recall that is_space('\0') is false (or at least isspace('\0') is false). Also recall that it is valid to read the \0 byte at the end of a string.

I find it a bit unusual that you are capitalizing NULL. My interpretation is that you are talking about the byte '\0', which is also called the null byte, and not the NULL pointer.

Re:Sentinels Can Be Faster by burntsushi on Wednesday September 09, 2020 @08:14PM (#24427520)
Can you double check the code? I just stared at the code snippets for five minutes and couldn't find a single error.

The functions do assume the parameters are non-null pointers to NUL terminated strings, but that kind of assumption is pretty standard in C APIs. So other than that, I don't see any missing NULL check nor do I see the bug you refer to that occurs when there are no leading spaces.