FS-2746 --resolve large xmlrpc update thanks garmt

This commit is contained in:
Jeff Lenk
2012-10-13 11:37:25 -05:00
parent 37ecad9903
commit 6b6c83a718
397 changed files with 41822 additions and 33841 deletions

View File

@@ -1,10 +1,61 @@
//#define _GNU_SOURCE
#define _XOPEN_SOURCE 600 /* Make sure strdup() is in <string.h> */
#ifndef _GNU_SOURCE
#define _GNU_SOURCE /* But only when HAVE_ASPRINTF */
#endif
#include <stdarg.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include "xmlrpc_config.h" /* For HAVE_ASPRINTF, __inline__ */
#include "xmlrpc-c/string_int.h"
#include "bool.h"
static __inline__ void
newVsnprintf(char * const buffer,
size_t const bufferSize,
const char * const fmt,
va_list varargs,
size_t * const formattedSizeP) {
/*----------------------------------------------------------------------------
This is vsnprintf() with the new behavior, where not fitting in the buffer
is not a failure.
Unfortunately, we can't practically return the size of the formatted string
if the C library has old vsnprintf() and the formatted string doesn't fit
in the buffer, so in that case we just return something larger than the
buffer.
-----------------------------------------------------------------------------*/
if (bufferSize > INT_MAX/2) {
/* There's a danger we won't be able to coerce the return value
of XMLRPC_VSNPRINTF to an integer (which we have to do because,
while for POSIX its return value is ssize_t, on Windows it is int),
or return double the buffer size.
*/
*formattedSizeP = 0;
} else {
int rc;
rc = XMLRPC_VSNPRINTF(buffer, bufferSize, fmt, varargs);
if (rc < 0) {
/* We have old vsnprintf() (or Windows) and the formatted value
doesn't fit in the buffer, but we don't know how big a buffer it
needs.
*/
*formattedSizeP = bufferSize * 2;
} else {
/* Either the string fits in the buffer or we have new vsnprintf()
which tells us how big the string is regardless.
*/
*formattedSizeP = rc;
}
}
}
@@ -15,29 +66,24 @@ simpleVasprintf(char ** const retvalP,
/*----------------------------------------------------------------------------
This is a poor man's implementation of vasprintf(), of GNU fame.
-----------------------------------------------------------------------------*/
size_t const initialSize = 4096;
char * result;
size_t bufferSize;
bool outOfMemory;
result = malloc(initialSize);
if (result != NULL) {
size_t bytesNeeded;
bytesNeeded = XMLRPC_VSNPRINTF(result, initialSize, fmt, varargs);
if (bytesNeeded > initialSize) {
free(result);
result = malloc(bytesNeeded);
if (result != NULL)
XMLRPC_VSNPRINTF(result, bytesNeeded, fmt, varargs);
} else if (bytesNeeded == initialSize) {
if (result[initialSize-1] != '\0') {
/* This is one of those old systems where vsnprintf()
returns the number of bytes it used, instead of the
number that it needed, and it in fact needed more than
we gave it. Rather than mess with this highly unlikely
case (old system and string > 4095 characters), we just
treat this like an out of memory failure.
*/
for (result = NULL, bufferSize = 4096, outOfMemory = false;
!result && !outOfMemory;
) {
result = malloc(bufferSize);
if (!result)
outOfMemory = true;
else {
size_t bytesNeeded;
newVsnprintf(result, bufferSize, fmt, varargs, &bytesNeeded);
if (bytesNeeded > bufferSize) {
free(result);
result = NULL;
bufferSize = bytesNeeded;
}
}
}
@@ -46,7 +92,28 @@ simpleVasprintf(char ** const retvalP,
const char * const xmlrpc_strsol = "[insufficient memory to build string]";
static const char * const xmlrpc_strsol =
"[insufficient memory to build string]";
bool
xmlrpc_strnomem(const char * const string) {
/*----------------------------------------------------------------------------
The string 'string' was generated by a function in this file because it
couldn't get enough memory to generate the string that it was supposed to
generate. I.e. a preceding call to a string function failed.
-----------------------------------------------------------------------------*/
return string == xmlrpc_strsol;
}
const char *
xmlrpc_strnomemval() {
return xmlrpc_strsol;
}
@@ -71,7 +138,7 @@ xmlrpc_vasprintf(const char ** const retvalP,
void GNU_PRINTF_ATTR(2,3)
void XMLRPC_PRINTF_ATTR(2,3)
xmlrpc_asprintf(const char ** const retvalP, const char * const fmt, ...) {
va_list varargs; /* mysterious structure used by variable arg facility */
@@ -85,6 +152,27 @@ xmlrpc_asprintf(const char ** const retvalP, const char * const fmt, ...) {
const char *
xmlrpc_strdupsol(const char * const string) {
const char * retvalOrNull;
retvalOrNull = strdup(string);
return retvalOrNull ? retvalOrNull : xmlrpc_strsol;
}
void
xmlrpc_strfree(const char * const string) {
if (string != xmlrpc_strsol)
free((void *)string);
}
const char *
xmlrpc_strdupnull(const char * const string) {
@@ -96,15 +184,6 @@ xmlrpc_strdupnull(const char * const string) {
void
xmlrpc_strfree(const char * const string) {
if (string != xmlrpc_strsol)
free((void *)string);
}
void
xmlrpc_strfreenull(const char * const string) {