Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Creating return buffers with the right size based on va_list.

Hello all!

I'm currently seeking for solutions or advices on how to fix my problem.

I have a logger that has a Write method having a variable argument list like so:
[code]void Logger::Write( S32 p_LogType, CZSTRING p_Msg, ... )[/code]

Currently the function is defined like this:
[code]
...

va_list va_args;
va_start( va_args, p_Msg );
char szBuffer[ LOGGER_STRINGBUFFERSIZE ];
vsprintf( szBuffer, p_Msg, va_args );

...

m_EngineLog << szBuffer << "
";
m_EngineLog.flush();
[/code]

As you can see, I have defined a macro LOGGER_STRINGBUFFERSIZE to define the size of the char buffers to be created. Though, I have problems on when the arguments or the string itself is beyond the size. vsprintf causes a crash -- and I know why (buffer overflow). It's just I don't know my options.

On other code, I saw that a size parameter is required on the method but that's not an option for me, I really need it not to be explicitly stated. I tried to make a dynamic-sized array on-the-fly using new or malloc but I need to know the total size of the whole new string before even using vsprintf, which I don't know how. Any takes?

Any help is greatly appreciated.

Comments

  • tsagldtsagld Member Posts: 621
    : Hello all!
    :
    : I'm currently seeking for solutions or advices on how to fix my
    : problem.
    :
    : I have a logger that has a Write method having a variable argument
    : list like so:
    : [code]: void Logger::Write( S32 p_LogType, CZSTRING p_Msg, ... )[/code]:
    :
    : Currently the function is defined like this:
    : [code]:
    : ...
    :
    : va_list va_args;
    : va_start( va_args, p_Msg );
    : char szBuffer[ LOGGER_STRINGBUFFERSIZE ];
    : vsprintf( szBuffer, p_Msg, va_args );
    :
    : ...
    :
    : m_EngineLog << szBuffer << "
    ";
    : m_EngineLog.flush();
    : [/code]:
    :
    : As you can see, I have defined a macro LOGGER_STRINGBUFFERSIZE to
    : define the size of the char buffers to be created. Though, I have
    : problems on when the arguments or the string itself is beyond the
    : size. vsprintf causes a crash -- and I know why (buffer overflow).
    : It's just I don't know my options.
    :
    : On other code, I saw that a size parameter is required on the method
    : but that's not an option for me, I really need it not to be
    : explicitly stated. I tried to make a dynamic-sized array on-the-fly
    : using new or malloc but I need to know the total size of the whole
    : new string before even using vsprintf, which I don't know how. Any
    : takes?
    :
    : Any help is greatly appreciated.

    Use _vscprintf first. This function doesn't write to a buffer but returns the number of characters needed for vsprintf.

    Greets,
    Eric Goldstein
    http://www.gollie.nl
  • AndreiVictorAndreiVictor Member Posts: 9
    Thank you very much! That did the job! :D

    It's not portable, but I'll deal with the cross-platform issue myself. ;)
Sign In or Register to comment.