Buffer overrun - exit or abort?

Discussion in 'C++' started by Martin Eisenberg, Jan 16, 2006.

  1. 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

    --
    Quidquid latine scriptum sit, altum viditur.
     
    Martin Eisenberg, Jan 16, 2006
    #1
    1. Advertising

  2. Martin Eisenberg wrote:
    > 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
     
    Victor Bazarov, Jan 16, 2006
    #2
    1. Advertising

  3. Victor Bazarov wrote:

    > Martin Eisenberg wrote:
    >> 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?


    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

    --
    Quidquid latine scriptum sit, altum viditur.
     
    Martin Eisenberg, Jan 17, 2006
    #3
  4. Martin Eisenberg wrote:
    > Victor Bazarov wrote:
    >
    >> Martin Eisenberg wrote:
    >>> 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?

    >
    > 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
     
    Victor Bazarov, Jan 17, 2006
    #4
  5. Martin Eisenberg

    Ian Collins Guest

    Martin Eisenberg wrote:
    > 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.

    --
    Ian Collins.
     
    Ian Collins, Jan 17, 2006
    #5
  6. Martin Eisenberg

    Jack Klein Guest

    On 16 Jan 2006 23:09:31 GMT, Martin Eisenberg
    <> wrote in comp.lang.c++:

    > 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().

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://c-faq.com/
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
     
    Jack Klein, Jan 17, 2006
    #6
  7. Martin Eisenberg <> schrieb:

    > 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
     
    Markus Becker, Jan 17, 2006
    #7
  8. Markus Becker wrote:

    > Martin Eisenberg <> schrieb:
    >
    >> 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.


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


    Martin

    --
    Quidquid latine scriptum sit, altum viditur.
     
    Martin Eisenberg, Jan 18, 2006
    #8
  9. Martin Eisenberg

    Earl Purple Guest

    Martin Eisenberg wrote:
    > Markus Becker wrote:
    >
    > > Martin Eisenberg <> schrieb:
    > >
    > >> 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.

    >
    > 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.
     
    Earl Purple, Jan 18, 2006
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. JKop
    Replies:
    8
    Views:
    476
  2. Anthony Baxter
    Replies:
    0
    Views:
    272
    Anthony Baxter
    Oct 12, 2006
  3. Nico
    Replies:
    2
    Views:
    307
    Salt_Peter
    Oct 15, 2006
  4. =?Utf-8?B?TGl0dGxlbXV6?=

    buffer overrun

    =?Utf-8?B?TGl0dGxlbXV6?=, Aug 27, 2007, in forum: ASP .Net
    Replies:
    3
    Views:
    1,890
    =?Utf-8?B?SXBvZHVzZXI=?=
    Nov 11, 2007
  5. Julia

    exit() or abort()???

    Julia, Jun 7, 2004, in forum: Javascript
    Replies:
    3
    Views:
    176
    Lasse Reichstein Nielsen
    Jun 7, 2004
Loading...

Share This Page