~ubuntu-branches/ubuntu/trusty/postgis/trusty-security

« back to all changes in this revision

Viewing changes to liblwgeom/vsprintf.c

  • Committer: Bazaar Package Importer
  • Author(s): Francesco Paolo Lovergine
  • Date: 2009-12-11 13:10:34 UTC
  • mfrom: (1.1.9 upstream) (5.2.1 experimental)
  • Revision ID: james.westby@ubuntu.com-20091211131034-wmsz69wxvt95pe5r
Tags: 1.4.0-2
* Upload to unstable.
* Better parameterized debian/rules against postgis $(VERSION).
* Added dblatex and libcunit1-dev among build-deps.
* Added postgis_comments.sql to contrib/ SQL templates.
* Dropping 8.3 support, no more supported for squeeze.
  (closes: #559587)
* Do not stop on error in postrm if the target dir does not exist.
  (closes: #560409)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Like vsprintf but provides a pointer to malloc'd storage, which must
 
2
   be freed by the caller.
 
3
   Copyright (C) 1994, 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
 
4
 
 
5
This program is free software; you can redistribute it and/or modify
 
6
it under the terms of the GNU General Public License as published by
 
7
the Free Software Foundation; either version 2, or (at your option)
 
8
any later version.
 
9
 
 
10
This program is distributed in the hope that it will be useful,
 
11
but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
GNU General Public License for more details.
 
14
 
 
15
You should have received a copy of the GNU General Public License
 
16
along with this program; if not, write to the Free Software
 
17
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
 
18
 
 
19
#ifdef HAVE_CONFIG_H
 
20
# include <config.h>
 
21
#endif
 
22
 
 
23
#include <stdio.h>
 
24
#include <string.h>
 
25
#include <stdlib.h>
 
26
 
 
27
#if __STDC__
 
28
# include <stdarg.h>
 
29
#else
 
30
# include <varargs.h>
 
31
#endif
 
32
 
 
33
#include <math.h>
 
34
 
 
35
#ifdef TEST
 
36
int global_total_width;
 
37
#endif
 
38
 
 
39
/* Make sure we have a va_copy that will work on all platforms */
 
40
#ifndef va_copy
 
41
# ifdef __va_copy
 
42
#  define va_copy(d, s)         __va_copy((d), (s))
 
43
# else
 
44
#  define va_copy(d, s)         memcpy(&(d), &(s), sizeof(va_list))
 
45
# endif
 
46
#endif
 
47
 
 
48
int lw_vasprintf (char **result, const char *format, va_list args);
 
49
int lw_asprintf
 
50
#if __STDC__
 
51
(char **result, const char *format, ...);
 
52
#else
 
53
(result, va_alist);
 
54
char **result;
 
55
va_dcl
 
56
#endif
 
57
 
 
58
 
 
59
static int
 
60
int_vasprintf (result, format, args)
 
61
char **result;
 
62
const char *format;
 
63
va_list *args;
 
64
{
 
65
        const char *p = format;
 
66
        /* Add one to make sure that it is never zero, which might cause malloc
 
67
           to return NULL.  */
 
68
        int total_width = strlen (format) + 1;
 
69
        va_list ap;
 
70
 
 
71
        memcpy (&ap, args, sizeof (va_list));
 
72
 
 
73
        while (*p != '\0')
 
74
        {
 
75
                if (*p++ == '%')
 
76
                {
 
77
                        while (strchr ("-+ #0", *p))
 
78
                                ++p;
 
79
                        if (*p == '*')
 
80
                        {
 
81
                                ++p;
 
82
                                total_width += abs (va_arg (ap, int));
 
83
                        }
 
84
                        else
 
85
                                total_width += strtoul (p, (char **) &p, 10);
 
86
                        if (*p == '.')
 
87
                        {
 
88
                                ++p;
 
89
                                if (*p == '*')
 
90
                                {
 
91
                                        ++p;
 
92
                                        total_width += abs (va_arg (ap, int));
 
93
                                }
 
94
                                else
 
95
                                        total_width += strtoul (p, (char **) &p, 10);
 
96
                        }
 
97
                        while (strchr ("hlLjtz", *p))
 
98
                                ++p;
 
99
                        /* Should be big enough for any format specifier except %s
 
100
                           and floats.  */
 
101
                        total_width += 30;
 
102
                        switch (*p)
 
103
                        {
 
104
                        case 'd':
 
105
                        case 'i':
 
106
                        case 'o':
 
107
                        case 'u':
 
108
                        case 'x':
 
109
                        case 'X':
 
110
                        case 'c':
 
111
                                (void) va_arg (ap, int);
 
112
                                break;
 
113
                        case 'f':
 
114
                        {
 
115
                                double arg = va_arg (ap, double);
 
116
                                if (arg >= 1.0 || arg <= -1.0)
 
117
                                        /* Since an ieee double can have an exponent of 307, we'll
 
118
                                           make the buffer wide enough to cover the gross case. */
 
119
                                        total_width += 307;
 
120
                        }
 
121
                        break;
 
122
                        case 'e':
 
123
                        case 'E':
 
124
                        case 'g':
 
125
                        case 'G':
 
126
                                (void) va_arg (ap, double);
 
127
                                break;
 
128
                        case 's':
 
129
                                total_width += strlen (va_arg (ap, char *));
 
130
                                break;
 
131
                        case 'p':
 
132
                        case 'n':
 
133
                                (void) va_arg (ap, char *);
 
134
                                break;
 
135
                        }
 
136
                        p++;
 
137
                }
 
138
        }
 
139
#ifdef TEST
 
140
        global_total_width = total_width;
 
141
#endif
 
142
        *result = malloc (total_width);
 
143
        if (*result != NULL)
 
144
                return vsprintf (*result, format, *args);
 
145
        else
 
146
                return 0;
 
147
}
 
148
 
 
149
int
 
150
lw_vasprintf (result, format, args)
 
151
char **result;
 
152
const char *format;
 
153
va_list args;
 
154
{
 
155
        va_list temp;
 
156
 
 
157
        va_copy(temp, args);
 
158
 
 
159
        return int_vasprintf (result, format, &temp);
 
160
}
 
161
 
 
162
int
 
163
lw_asprintf
 
164
#if __STDC__
 
165
(char **result, const char *format, ...)
 
166
#else
 
167
(result, va_alist)
 
168
char **result;
 
169
va_dcl
 
170
#endif
 
171
{
 
172
        va_list args;
 
173
        int done;
 
174
 
 
175
#if __STDC__
 
176
        va_start (args, format);
 
177
#else
 
178
        char *format;
 
179
        va_start (args);
 
180
        format = va_arg (args, char *);
 
181
#endif
 
182
        done = lw_vasprintf (result, format, args);
 
183
        va_end (args);
 
184
 
 
185
        return done;
 
186
}