3 ms·
Your code also fails (i.e., reads and writes past the end of the string) if c == '\0' and s is "\0".
by jemfinch 16y ago
Your code also fails (i.e., reads and writes past the end of the string) if c == '\0' and s is "\0".
- loup-vaillant 16y agoI considered the `c=='\0'` case bogus anyway, so I was OK with a partial function. (I wouldn't have been for production code, though.)
- RiderOfGiraffes 16y agoAre you saying that you actually considered that case, and then wrote code that didn't deal with it?
- loup-vaillant 16y agoYes I did, and then yes I did. I didn't fully investigated that case, but I suspected there could be a problem with `c=='\0'`. I suspected that the `while (s[from] == c) from ++;` line could shoot past the termination '\0' and trigger a buffer overflow. But I didn't fully investigate, on the grounds that no sane programmer would want to remove a character that's never in a C string. In other words, I considered it was not part of the specification. In production code, I would have either thrown an exception, or returned early. And I would have asked around to know which I would chose. Could my attitude have influenced my chances, if I had applied?