itoa()
is safe as long as the destination buffer is big enough to receive the largest possible representation (i.e. of INT_MIN with trailing NUL). So, you can simply check the buffer size. Still, it's not a very good function to use because if you change your data type to a larger integral type, you need to change to atol
, atoll
, atoq
etc.. If you want a dynamic buffer that handles whatever type you throw at it with less maintenance issues, consider an std::ostringstream
(from the <sstream>
header).
getchar()
has no "secure counterpart" - it's not insecure to begin with and has no buffer overrun potential.
Re memset()
: it's dangerous in that it accepts the programmers judgement that memory should be overwritten without any confirmation of the content/address/length, but when used properly it leaves no issue, and sometimes it's the best tool for the job even in modern C++ programming. To check security issues with this, you need to inspect the code and ensure it's aimed at a suitable buffer or object to be 0ed, and that the length is computed properly (hint: use sizeof where possible).
strcat()
can be dangerous if the strings being concatenated aren't known to fit into the destination buffer. For example: char buf[16]; strcpy(buf, "one,"); strcat(buf, "two");
is all totally safe (but fragile, as further operations or changing either string might require more than 16 chars and the compiler won't warn you), whereas strcat(buf, argv[0])
is not. The best replacement tends to be a std::ostringstream, although that can require significant reworking of the code. You may get away using strncat()
, or even - if you have it - asprintf("%s%s", first, second)
, which will allocate the required amount of memory on the heap (do remember to free()
it). You could also consider std::string and use operator+ to concatenate strings.