vsprintf - safe alternative?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Thomas Rogg

    vsprintf - safe alternative?

    Hello NG,

    in my program I use the following function:

    void write_log(char *format, ...)
    {
    va_list arg;
    char txt[512];

    // Get string
    va_start(arg, format);
    vsprintf(txt, format, arg); // TODO: txt might overflow
    va_end(arg);

    // Write to stdout
    if(debug_consol e)
    write_console(d ebug_console, txt);
    printf(txt);
    }

    Is there any safe alternative to vsprintf? I do not want to change the
    way I am passing the parameters, because I used this function in
    thousands of code lines of a project I've been writing for two years now.

    Thank you,

    Thomas Rogg
  • Christopher Benson-Manica

    #2
    Re: vsprintf - safe alternative?

    Thomas Rogg <youdontwanttok now@web.de> spoke thus:
    [color=blue]
    > Is there any safe alternative to vsprintf? I do not want to change the
    > way I am passing the parameters, because I used this function in
    > thousands of code lines of a project I've been writing for two years now.[/color]

    Sounds like you want vsnprintf().

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cybers pace.org | don't, I need to know. Flames welcome.

    Comment

    • Richard Tobin

      #3
      Re: vsprintf - safe alternative?

      In article <cfvjcj$5bv$04$ 1@news.t-online.com>,
      Thomas Rogg <youdontwanttok now@web.de> wrote:
      [color=blue]
      >Is there any safe alternative to vsprintf?[/color]

      C99 has vsnprintf, and many non-C99 systems also provide it.

      -- Richard

      Comment

      • Peter Ammon

        #4
        Re: vsprintf - safe alternative?

        Thomas Rogg wrote:
        [color=blue]
        > Hello NG,
        >
        > in my program I use the following function:
        >
        > void write_log(char *format, ...)
        > {
        > va_list arg;
        > char txt[512];
        >
        > // Get string
        > va_start(arg, format);
        > vsprintf(txt, format, arg); // TODO: txt might overflow
        > va_end(arg);
        >
        > // Write to stdout
        > if(debug_consol e)
        > write_console(d ebug_console, txt);
        > printf(txt);
        > }
        >
        > Is there any safe alternative to vsprintf? I do not want to change the
        > way I am passing the parameters, because I used this function in
        > thousands of code lines of a project I've been writing for two years now.
        >
        > Thank you,
        >
        > Thomas Rogg[/color]

        None in C89 AFAIK, but C99 has vsnprintf()

        vsprintf(txt, sizeof txt, format, arg);

        GNU's libc also has the supremely convenient but nonstandard asprintf()
        and vasprintf(), which malloc() the buffer for you. Use only if
        portability isn't important.

        -Peter

        Comment

        • Moonie

          #5
          Re: vsprintf - safe alternative?


          INSTEAD OF USING VSPRINTF YOU CAN USE SNPRINTF

          SNPRINTF( BUF, SIZE, FORMAT, ARGS );

          IT IS FOUND IN A WINDOW ENVIRONMENT AND LINUX BUT NOT DOS.
          THERE MAY BE AN OPEN SOURCE FUNCTION AVAILABLE SOMEWHERE IF YOU DO A
          SEARCH IN GOOGLE.COM FOR IT.

          I HOPE THIS HELPS. YOU DIDN'T SPECIFY PLATFORM AND COMPILER.



          --
          Moonie
          ------------------------------------------------------------------------
          Posted via http://www.codecomments.com
          ------------------------------------------------------------------------

          Comment

          • Moonie

            #6
            Re: vsprintf - safe alternative?


            _vsnprintf or vsnprintf is lile vsprintf

            snprintf or _snprintf is like sprintf

            my mistake oops. sorry.



            --
            Moonie
            ------------------------------------------------------------------------
            Posted via http://www.codecomments.com
            ------------------------------------------------------------------------

            Comment

            Working...