"C library functions such as strcpy (), strcat (), sprintf () and vsprintf () operate on null terminated strings and perform no bounds checking." "snprintf is safer than sprintf" What do these statements really mean? int sprintf ( char * str , const char * format , ... ) int snprintf ( char * s, size_t n, const char * format, ... ); char * strcpy ( char * destination, const char * source ); char * strncpy ( char * destination, const char * source, size_t num ); The usage is something like; char* msg1 = new char[10]; strcpy(msg1, "test"); // 1 char buffer[128]; sprintf(buffer, "%s", msg); //2 strcpy : Copies bytes until it finds a 0-byte in the source code. The string literal "test" has 4 characters and a terminating null character at end, therefore needs 5 characters at least on msg1. Is this dangerous? Yes, because if the source message is not null terminated it will read until a null character ...
Comments
Post a Comment