Clean up journal transaction buffer code to avoid overflow.
This commit is contained in:
parent
71bd1c27d9
commit
942389a503
|
@ -504,27 +504,50 @@ Loggable::compact ( void )
|
||||||
|
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
|
|
||||||
/** Buffered sprintf wrapper */
|
/** Writes (part of) a line to the journal. Each separate line will be
|
||||||
|
* stored separately in _transaction until transaction is closed.
|
||||||
|
*/
|
||||||
void
|
void
|
||||||
Loggable::log ( const char *fmt, ... )
|
Loggable::log ( const char *fmt, ... )
|
||||||
{
|
{
|
||||||
|
static char * buf = NULL;
|
||||||
|
static size_t i = 0;
|
||||||
|
static size_t buf_size = 0;
|
||||||
|
|
||||||
if ( ! _fp )
|
if ( ! _fp )
|
||||||
return;
|
return;
|
||||||
|
|
||||||
/* FIXME: bogus limit */
|
if ( NULL == buf )
|
||||||
static char buf[1024];
|
{
|
||||||
static int i = 0;
|
buf_size = 1024;
|
||||||
|
buf = (char*)malloc( buf_size );
|
||||||
|
}
|
||||||
|
|
||||||
va_list args;
|
va_list args;
|
||||||
|
|
||||||
if ( fmt )
|
if ( fmt )
|
||||||
{
|
{
|
||||||
va_start( args, fmt );
|
va_start( args, fmt );
|
||||||
i += vsprintf( buf + i, fmt, args );
|
|
||||||
|
for ( ;; )
|
||||||
|
{
|
||||||
|
size_t l = vsnprintf( buf + i, buf_size - i, fmt, args );
|
||||||
|
|
||||||
|
if ( l >= buf_size - i )
|
||||||
|
{
|
||||||
|
buf = (char*)realloc( buf, buf_size += (l + 1) + buf_size );
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
i += l;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
va_end( args );
|
va_end( args );
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( rindex( buf, '\n' ) )
|
if ( '\n' == buf[i-1] )
|
||||||
{
|
{
|
||||||
_transaction.push( strdup( buf ) );
|
_transaction.push( strdup( buf ) );
|
||||||
i = 0;
|
i = 0;
|
||||||
|
|
Loading…
Reference in New Issue