Another one of those silly problems in C programming that occasionally crops up.
Depending on the compiler, declaring a char * variable pointing to a string constant can cause that string to go into the .text segment of the application. This segment is marked as readable and executable; but explicitly not writeable.
When you try to write to this address it issues a memory protection fault (SEGV on linux). Solutions are:
- copy the string using strdup before writing to it. This means you need to remember to free the memory once the function has completed.
- use alloca and strcpy to populate the string. This eliminates the need to free the memory, but adds a bit to the set-up overhead
Nontheless, you should never be writing to values obtained from a constant; you never know where it’s been (poor blighter).