mirror of
https://github.com/EQEmu/Server.git
synced 2025-12-21 10:11:30 +00:00
Apparently the original implementation from facebook was incorrect.
Fixed it, sadly this does mean we need to call vsnprintf twice. Once to find out the size involved, a second time to apply.
This commit is contained in:
parent
f1a487f606
commit
d41331d948
@ -15,9 +15,10 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include "StringUtil.h"
|
#include "StringUtil.h"
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <cstring> // for strncpy
|
|
||||||
#include <cstdarg>
|
#include <cstdarg>
|
||||||
|
#include <cstring> // for strncpy
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
|
|
||||||
#ifdef _WINDOWS
|
#ifdef _WINDOWS
|
||||||
@ -26,8 +27,10 @@
|
|||||||
#define snprintf _snprintf
|
#define snprintf _snprintf
|
||||||
#define strncasecmp _strnicmp
|
#define strncasecmp _strnicmp
|
||||||
#define strcasecmp _stricmp
|
#define strcasecmp _stricmp
|
||||||
|
|
||||||
#else
|
#else
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifndef va_copy
|
#ifndef va_copy
|
||||||
@ -37,53 +40,64 @@
|
|||||||
// original source:
|
// original source:
|
||||||
// https://github.com/facebook/folly/blob/master/folly/String.cpp
|
// https://github.com/facebook/folly/blob/master/folly/String.cpp
|
||||||
//
|
//
|
||||||
void StringFormat(std::string& output, const char* format, ...)
|
void vStringFormat(std::string& output, const char* format, va_list args)
|
||||||
{
|
{
|
||||||
va_list args;
|
va_list tmpargs;
|
||||||
// Tru to the space at the end of output for our output buffer.
|
|
||||||
// Find out write point then inflate its size temporarily to its
|
|
||||||
// capacity; we will later shrink it to the size needed to represent
|
|
||||||
// the formatted string. If this buffer isn't large enough, we do a
|
|
||||||
// resize and try again.
|
|
||||||
|
|
||||||
const auto write_point = output.size();
|
va_copy(tmpargs,args);
|
||||||
auto remaining = output.capacity() - write_point;
|
int characters_used = vsnprintf(nullptr, 0, format, tmpargs);
|
||||||
output.resize(output.capacity());
|
va_end(tmpargs);
|
||||||
|
|
||||||
va_start(args, format);
|
|
||||||
int bytes_used = vsnprintf(&output[write_point], remaining, format,args);
|
|
||||||
va_end(args);
|
|
||||||
if (bytes_used < 0) {
|
|
||||||
|
|
||||||
|
if (characters_used < 0) {
|
||||||
|
// Looks like we have an invalid format string.
|
||||||
|
// error out.
|
||||||
std::string errorMessage("Invalid format string; snprintf returned negative with format string: ");
|
std::string errorMessage("Invalid format string; snprintf returned negative with format string: ");
|
||||||
errorMessage.append(format);
|
errorMessage.append(format);
|
||||||
|
|
||||||
throw std::runtime_error(errorMessage);
|
throw std::runtime_error(errorMessage);
|
||||||
}
|
}
|
||||||
else if ((unsigned int)bytes_used < remaining) {
|
else if ((unsigned int)characters_used > output.capacity()) {
|
||||||
// There was enough room, just shrink and return.
|
output.resize(characters_used+1);
|
||||||
output.resize(write_point + bytes_used);
|
va_copy(tmpargs,args);
|
||||||
|
characters_used = vsnprintf(&output[0], output.capacity(), format, tmpargs);
|
||||||
|
va_end(tmpargs);
|
||||||
|
|
||||||
|
if (characters_used < 0) {
|
||||||
|
// We shouldn't have a format error by this point, but I can't imagine what error we
|
||||||
|
// could have by this point. Still, error out and report it.
|
||||||
|
std::string errorMessage("Invalid format string or unknown vsnprintf error; vsnprintf returned negative with format string: ");
|
||||||
|
errorMessage.append(format);
|
||||||
|
|
||||||
|
throw std::runtime_error(errorMessage);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
output.resize(write_point + bytes_used + 1);
|
output.resize(characters_used + 1);
|
||||||
remaining = bytes_used + 1;
|
|
||||||
|
|
||||||
va_start(args, format);
|
va_copy(tmpargs,args);
|
||||||
bytes_used = vsnprintf(&output[write_point], remaining, format, args);
|
characters_used = vsnprintf(&output[0], output.capacity(), format, tmpargs);
|
||||||
va_end(args);
|
va_end(tmpargs);
|
||||||
|
|
||||||
if ((unsigned int)(bytes_used + 1) != remaining) {
|
if (characters_used < 0) {
|
||||||
|
// We shouldn't have a format error by this point, but I can't imagine what error we
|
||||||
std::string errorMessage("vsnprint retry did not manage to work with format string: ");
|
// could have by this point. still error out and report it.
|
||||||
|
std::string errorMessage("Invalid format string or unknown vsnprintf error; vsnprintf returned negative with format string: ");
|
||||||
errorMessage.append(format);
|
errorMessage.append(format);
|
||||||
|
|
||||||
throw std::runtime_error(errorMessage);
|
throw std::runtime_error(errorMessage);
|
||||||
}
|
}
|
||||||
|
|
||||||
output.resize(write_point + bytes_used);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void StringFormat(std::string& output, const char* format, ...)
|
||||||
|
{
|
||||||
|
va_list args;
|
||||||
|
va_start(args, format);
|
||||||
|
vStringFormat(output,format,args);
|
||||||
|
va_end(args);
|
||||||
|
}
|
||||||
|
|
||||||
// normal strncpy doesnt put a null term on copied strings, this one does
|
// normal strncpy doesnt put a null term on copied strings, this one does
|
||||||
// ref: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wcecrt/htm/_wcecrt_strncpy_wcsncpy.asp
|
// ref: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wcecrt/htm/_wcecrt_strncpy_wcsncpy.asp
|
||||||
char* strn0cpy(char* dest, const char* source, uint32 size) {
|
char* strn0cpy(char* dest, const char* source, uint32 size) {
|
||||||
|
|||||||
@ -17,8 +17,11 @@
|
|||||||
#define _STRINGUTIL_H_
|
#define _STRINGUTIL_H_
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <cstdarg>
|
||||||
#include "types.h"
|
#include "types.h"
|
||||||
|
|
||||||
|
|
||||||
|
void vStringFormat(std::string& output, const char* format, va_list args);
|
||||||
void StringFormat(std::string& output, const char* format, ...);
|
void StringFormat(std::string& output, const char* format, ...);
|
||||||
//////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////
|
||||||
//
|
//
|
||||||
|
|||||||
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <cstdarg>
|
||||||
|
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
|
|
||||||
@ -12,10 +13,11 @@
|
|||||||
#define vsnprintf _vsnprintf
|
#define vsnprintf _vsnprintf
|
||||||
#define strncasecmp _strnicmp
|
#define strncasecmp _strnicmp
|
||||||
#define strcasecmp _stricmp
|
#define strcasecmp _stricmp
|
||||||
|
|
||||||
#else
|
#else
|
||||||
|
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <stdarg.h>
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include "../common/StringUtil.h"
|
#include "../common/StringUtil.h"
|
||||||
@ -150,27 +152,35 @@ bool EQEMuLog::write(LogIDs id, const char *fmt, ...) {
|
|||||||
time( &aclock ); /* Get time in seconds */
|
time( &aclock ); /* Get time in seconds */
|
||||||
newtime = localtime( &aclock ); /* Convert time to struct */
|
newtime = localtime( &aclock ); /* Convert time to struct */
|
||||||
|
|
||||||
if (dofile)
|
if (dofile) {
|
||||||
#ifndef NO_PIDLOG
|
#ifndef NO_PIDLOG
|
||||||
fprintf(fp[id], "[%02d.%02d. - %02d:%02d:%02d] ", newtime->tm_mon+1, newtime->tm_mday, newtime->tm_hour, newtime->tm_min, newtime->tm_sec);
|
fprintf(fp[id], "[%02d.%02d. - %02d:%02d:%02d] ", newtime->tm_mon+1, newtime->tm_mday, newtime->tm_hour, newtime->tm_min, newtime->tm_sec);
|
||||||
#else
|
#else
|
||||||
fprintf(fp[id], "%04i [%02d.%02d. - %02d:%02d:%02d] ", getpid(), newtime->tm_mon+1, newtime->tm_mday, newtime->tm_hour, newtime->tm_min, newtime->tm_sec);
|
fprintf(fp[id], "%04i [%02d.%02d. - %02d:%02d:%02d] ", getpid(), newtime->tm_mon+1, newtime->tm_mday, newtime->tm_hour, newtime->tm_min, newtime->tm_sec);
|
||||||
#endif
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
va_list argptr,tmpargptr;
|
va_list argptr,tmpargptr;
|
||||||
va_start(argptr, fmt);
|
|
||||||
if (dofile) {
|
if (dofile) {
|
||||||
|
va_start(argptr, fmt);
|
||||||
va_copy(tmpargptr,argptr);
|
va_copy(tmpargptr,argptr);
|
||||||
vfprintf( fp[id], fmt, tmpargptr );
|
vfprintf( fp[id], fmt, tmpargptr );
|
||||||
|
va_end(tmpargptr);
|
||||||
}
|
}
|
||||||
if(logCallbackFmt[id]) {
|
if(logCallbackFmt[id]) {
|
||||||
msgCallbackFmt p = logCallbackFmt[id];
|
msgCallbackFmt p = logCallbackFmt[id];
|
||||||
|
va_start(argptr, fmt);
|
||||||
va_copy(tmpargptr,argptr);
|
va_copy(tmpargptr,argptr);
|
||||||
p(id, fmt, tmpargptr );
|
p(id, fmt, tmpargptr );
|
||||||
|
va_end(tmpargptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string outputMessage;
|
std::string outputMessage;
|
||||||
StringFormat(outputMessage, fmt, argptr);
|
va_start(argptr, fmt);
|
||||||
|
va_copy(tmpargptr,argptr);
|
||||||
|
vStringFormat(outputMessage, fmt, tmpargptr);
|
||||||
|
va_end(tmpargptr);
|
||||||
|
|
||||||
if (pLogStatus[id] & 2) {
|
if (pLogStatus[id] & 2) {
|
||||||
if (pLogStatus[id] & 8) {
|
if (pLogStatus[id] & 8) {
|
||||||
@ -183,7 +193,6 @@ bool EQEMuLog::write(LogIDs id, const char *fmt, ...) {
|
|||||||
std::cout << outputMessage;
|
std::cout << outputMessage;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
va_end(argptr);
|
|
||||||
if (dofile)
|
if (dofile)
|
||||||
fprintf(fp[id], "\n");
|
fprintf(fp[id], "\n");
|
||||||
if (pLogStatus[id] & 2) {
|
if (pLogStatus[id] & 2) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user