File: asprintf.c

package info (click to toggle)
xmlrpc-c 1.60.05-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,132 kB
  • sloc: ansic: 55,332; cpp: 13,541; sh: 3,321; makefile: 2,556; perl: 593; xml: 134
file content (199 lines) | stat: -rw-r--r-- 5,006 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#define _XOPEN_SOURCE 600  /* Make sure strdup() is in <string.h> */
#define _GNU_SOURCE  /* But only when HAVE_ASPRINTF */
#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;
        }
    }
}



static __inline__ int
simpleVasprintf(char **      const resultP,
                const char * const fmt,
                va_list            varargs) {
/*----------------------------------------------------------------------------
   This is a poor man's implementation of vasprintf(), of GNU fame.
-----------------------------------------------------------------------------*/
    int retval;
    char * buffer;
    size_t bufferSize;
    bool outOfMemory;

    for (buffer = NULL, bufferSize = 4096, outOfMemory = false;
         !buffer && !outOfMemory;
        ) {

        buffer = malloc(bufferSize);
        if (!buffer)
            outOfMemory = true;
        else {
            size_t bytesNeeded;
            newVsnprintf(buffer, bufferSize, fmt, varargs, &bytesNeeded);
            if (bytesNeeded > bufferSize) {
                free(buffer);
                buffer = NULL;
                bufferSize = bytesNeeded;
            }
        }
    }
    if (outOfMemory)
        retval = -1;
    else {
        retval = strlen(buffer);
        *resultP = buffer;
    }
    return retval;
}



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;
}



void
xmlrpc_vasprintf(const char ** const retvalP,
                 const char *  const fmt,
                 va_list             varargs) {
    
    char * string;
    int rc;

#if HAVE_ASPRINTF
    rc = vasprintf(&string, fmt, varargs);
#else
    rc = simpleVasprintf(&string, fmt, varargs);
#endif

    if (rc < 0)
        *retvalP = xmlrpc_strsol;
    else
        *retvalP = string;
}



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 */

    va_start(varargs, fmt); /* start up the mysterious variable arg facility */

    xmlrpc_vasprintf(retvalP, fmt, varargs);

    va_end(varargs);
}



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) {

    if (string)
        return strdup(string);
    else
        return NULL;
}



void
xmlrpc_strfreenull(const char * const string) {

    if (string)
        xmlrpc_strfree(string);
}