Buffer overrun - exit or abort?

M

Martin Eisenberg

Hi,

If I want to terminate a program upon finding that sprintf has
overrun its output buffer, should I prefer exit or abort from
cstdlib? Thanks.


Martin
 
V

Victor Bazarov

Martin said:
If I want to terminate a program upon finding that sprintf has
overrun its output buffer, should I prefer exit or abort from
cstdlib? Thanks.

This is a C question, is it not?

V
 
M

Martin Eisenberg

Victor said:
This is a C question, is it not?

No. I'm not using C and I have no reason to presume that it's just
the same or that a C user would know all that may be relevant in the
C++ context. Some also like to point out in such cases that the
mentioned functions do belong to C++...

While I'm at it, I anticipate being told just to use iostream -- the
response to that is that I really want to know, I can second-guess
myself just fine, and will do so at least twice in any case.


Martin
 
V

Victor Bazarov

Martin said:
No. I'm not using C and I have no reason to presume that it's just
the same or that a C user would know all that may be relevant in the
C++ context. Some also like to point out in such cases that the
mentioned functions do belong to C++...

While I'm at it, I anticipate being told just to use iostream -- the
response to that is that I really want to know, I can second-guess
myself just fine, and will do so at least twice in any case.

The reason I asked was simple: 'sprintf', 'exit', and 'abort' are
all Standard C library functions, and they (C folks) probably know
more what's going to happen and what to do to rectify things. I
only know one thing: if the buffer gets overrun, the behaviour of
the program in which it happens is undefined. For all we know, if
you call 'exit', it might instead behave as if you called 'abort'
and vice versa...

V
 
I

Ian Collins

Martin said:
Hi,

If I want to terminate a program upon finding that sprintf has
overrun its output buffer, should I prefer exit or abort from
cstdlib? Thanks.
Depends on your platform I guess, if abort provides some form of
post-mortem debug (like a UNIX core file), use it. This more of an
exception tan an error condition, this sort of thing you'd trap with an
assert - which often calls abort.
 
J

Jack Klein

Hi,

If I want to terminate a program upon finding that sprintf has
overrun its output buffer, should I prefer exit or abort from
cstdlib? Thanks.


Martin

Once your program has written beyond memory that belongs to it, you
have entered the realm of undefined behavior. There is no guarantee
that your program will survive to detect it.

If you can't guarantee that your program will not overwrite a buffer
using sprintf(), don't use sprintf().
 
M

Markus Becker

Martin Eisenberg said:
If I want to terminate a program upon finding that sprintf has
overrun its output buffer, should I prefer exit or abort from
cstdlib? Thanks.

Don't use sprintf, use snprintf.

Markus
 
E

Earl Purple

Martin said:
Is vsnprintf also standard? GCC has it but I don't see either in the
'97 standard draft.

Why use a printf variant at all? Use streams, or boost::format if you
really want printf-style.

Better not to use variable argument lists too.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top