|
|
|
|
|
by ktRolster
3201 days ago
|
|
Oh yeah, you're right. Another thing I've done that will work if you have a lot of strcat(), is make a string struct: ktString {
int len;
int memlen;
char *str;
}
It keeps track of the string's actual length, and the size of the underlying buffer. Then you can 'override' the various string functions: bool ktStrcat(ktString s1, ktString s2);
bool ktSprintf(ktString s1, ...);
These functions will take care of buffer-size checking, and reallocation if necessary. For cases where you need to interface with pre-existing libraries, you can return a cstring(). Make it a function/macro to enable you to change the struct definition in the future: #define ktCstr(x) (x)->str
then you can pass it into write() or whatever you need: write(sock, ktCstr(s), s->len);
|
|
Look, I get that there is a place for C, but string manipulation is absurdly bad and error-prone.