strchr() Considered Harmful

You tell me whats wrong with the following, which is the standard definition of the strchr() function:

char *strchr(const char *s, int c);

Give up? Well, think about this code:

void FunctionThatDoesntExpectItsArgumentsToChange(const char *input_string) {
char *unconst = strchr(input_string, input_string[0]);
strcpy(unconst,”You’ve been pwned”);
}

Do you know what just happened there? The compiler let me write on top of a const string, without ever having a cast from const to non-const! Take a look at all the string functions that have this hideous syntax:

strchr, strrchr, strchrnul, strpbrk, strstr.

I mean, what a huge list of suspects! This has got to be one of the biggest holes I’ve ever seen. What a mess. I’ve lost all faith in the designers of the standard C library. What a bunch of bozos.

This entry was posted in Linux Stuff. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>