On 27.02.2012 20:06, Nephi Immortal wrote:
> I am curious to ask a question. Poor buffer handling is implicated
> in many security issues that involve buffer overruns. All string
> buffers always include null terminator. What happen if you omit null
> terminator in source buffer?
> The std::string( char* ) constructor function should be removed from C+
> + Standard Library. Why do C++ Standard Library leave it alone in
> order to have legacy compatibility with C strings?
First of all there is no std::string( char* ) constructor. There is a
std::string( const char* ) constructor. And this is an important difference.
It is quite difficult to get an accidental string buffer overrun bug
without using char* for strings, because you need to have the
terminating null removed at the first place. Most compilers will at
least warn about that.
Not using char* is mostly sufficient to avoid buffer overruns.
Of course, with invalid data you might still cause memory allocation
errors or excessive memory usage. But while this might be enough for a
DOS attack, it will not help for intrusion attacks.
> The C++ Standard Library recommends to use std::string( char*, size )
> constructor function instead.
This won't help in any way, because to adapt C strings you need to write
std::string(name, strlen(name)) which shares the same problem, of
course. Writing
const char name[] = "Hello World";
std::string(name, 200);
is entirely wrong, because now you have again a buffer overrun, but this
time in the source buffer. All you can do is
std::string(name, strlen(name, 200));
> The string class uses dynamic memory allocation. Do C++ Standard
> Library offer fixed string buffers which string buffers are pushed
> into the stack? Of course, fixed string is less flexible unless large
> source buffer does not fit into small destination buffer and extra
> characters are truncated if memory reallocation is not used.
Well, going back to COBOL is definitely not what I like.
How many problems came from too small, fixed string buffers. Long names
of foreign people do not fit into the fields. Moving an application to
an active directory is prevented because the user name with domain does
no longer fit in the fields. An application, originally designed for
local files, seems to work with URLs too - fine. Unfortunately they are
truncated.
Marcel