
Efficient strlen implementation in C (2007) - geospeck
https://romain.blogreen.org/blog/2007/08/efficient-strlen-implementation-in-c/
======
CarolineW
Quoting:

 _> As you can see, it can be totally transparent for the user. You just have
to take care ..._

It's that word _just_ that bothers me.

"Programming is simple, you just have to write the code."

"Bug-free programming is easy, you just have to avoid putting bugs in."

Yes, this code "just" requires some care.

------
tqh
Broken. This will generate page faults if your string ends at a page-boundry
and start address isn't aligned on four bytes.

~~~
CarolineW
You are absolutely right, but it appears that you didn't read to the end,
stopping at the broken implementation that the author says is broken.

But you're right, and it does put the entire article in question. I'm tempted
to use this as an interview question:

 _There 's a bug in this code - can you find it? Talk to me about why this is
a bad thing to do? How can we fix it? What's the most important thing to be
thinking about?_

I was also interested to read the follow-up linked to from the bottom of the
article, but that's a 404.

~~~
tqh
I can't see where that was written. Only that malicious users may read past
the end.

~~~
CarolineW
OK, I don't know what you mean by:

    
    
        > I can't see where that was written
                            ^^^^
    

To what exactly are you referring by the "that"?

    
    
        > Only that malicious users may read
        > past the end.
    

There's an entire section after that:

    
    
        Reducing complexity
    
        Improvement is not that much effective?
        Actually, we are optimizing the wrong way!
        Really ;)! When optimizing code, you have
        to, in order:
    
            reduce complexity;
            tune your compiler;
            optimize your code.
    
        So, how can we reduce complexity (Currently
        O(n) for both algorithms)? Simply by storing
        the string length somewhere! The complexity
        is then O(1)
    

_etc._

But it's all there, and either you've read it and I don't know what you mean,
or you haven't read it, and I've provided the pointer for you.

