Bug Summary

File:stlport/unxlngi6.pro/misc/build/STLport-4.5/src/ios.cpp
Location:line 182, column 1
Description:Memory is never released; potential leak

Annotated Source Code

1/*
2 * Copyright (c) 1999
3 * Silicon Graphics Computer Systems, Inc.
4 *
5 * Copyright (c) 1999
6 * Boris Fomitchev
7 *
8 * This material is provided "as is", with absolutely no warranty expressed
9 * or implied. Any use is at your own risk.
10 *
11 * Permission to use or copy this software for any purpose is hereby granted
12 * without fee, provided the above notices are retained on all copies.
13 * Permission to modify the code and to distribute modified code is granted,
14 * provided the above notices are retained, and a notice that the code was
15 * modified is included with the above copyright notice.
16 *
17 */
18
19# include "stlport_prefix.h"
20# include <algorithm>
21# include <stl/_ios.h>
22
23_STLP_BEGIN_NAMESPACEnamespace _STL {
24
25char* _STLP_CALL
26__write_integer(char* buf, ios_base::fmtflags flags, long x);
27
28//----------------------------------------------------------------------
29// ios_base members
30
31// class ios_base::failure, a subclass of exception. It's used solely
32// for reporting errors.
33
34ios_base::failure::failure(const string& s)
35 : __Named_exception(s)
36{}
37
38ios_base::failure::~failure() _STLP_NOTHROW_INHERENTLYthrow() {}
39
40#if !defined (_STLP_STATIC_CONST_INIT_BUG)
41
42// Definitions of ios_base's formatting flags.
43const ios_base::fmtflags ios_base::left;
44const ios_base::fmtflags ios_base::right;
45const ios_base::fmtflags ios_base::internal;
46const ios_base::fmtflags ios_base::dec;
47const ios_base::fmtflags ios_base::hex;
48const ios_base::fmtflags ios_base::oct;
49const ios_base::fmtflags ios_base::fixed;
50const ios_base::fmtflags ios_base::scientific;
51const ios_base::fmtflags ios_base::boolalpha;
52const ios_base::fmtflags ios_base::showbase;
53const ios_base::fmtflags ios_base::showpoint;
54const ios_base::fmtflags ios_base::showpos;
55const ios_base::fmtflags ios_base::skipws;
56const ios_base::fmtflags ios_base::unitbuf;
57const ios_base::fmtflags ios_base::uppercase;
58const ios_base::fmtflags ios_base::adjustfield;
59const ios_base::fmtflags ios_base::basefield;
60const ios_base::fmtflags ios_base::floatfield;
61
62// Definitions of ios_base's state flags.
63const ios_base::iostate ios_base::goodbit;
64const ios_base::iostate ios_base::badbit;
65const ios_base::iostate ios_base::eofbit;
66const ios_base::iostate ios_base::failbit;
67
68// Definitions of ios_base's openmode flags.
69const ios_base::openmode ios_base::app;
70const ios_base::openmode ios_base::ate;
71const ios_base::openmode ios_base::binary;
72const ios_base::openmode ios_base::in;
73const ios_base::openmode ios_base::out;
74const ios_base::openmode ios_base::trunc;
75
76// Definitions of ios_base's seekdir flags.
77const ios_base::seekdir ios_base::beg;
78const ios_base::seekdir ios_base::cur;
79const ios_base::seekdir ios_base::end;
80
81# endif /* _STLP_STATIC_CONST_INIT_BUG */
82
83// Internal functions used for managing exponentially-growing arrays of
84// POD types.
85
86// array is a pointer to N elements of type PODType. Expands the array,
87// if necessary, so that array[index] is meaningful. All new elements are
88// initialized to zero. Returns a pointer to the new array, and the new
89// size.
90template <class PODType>
91pair<PODType*, size_t>
92_Stl_expand_array(PODType* array, size_t N, int index)
93{
94 if ((int)N < index + 1) {
2
Taking true branch
95 size_t new_N = (max)(2 * N, size_t(index + 1));
96 PODType* new_array
97 = __STATIC_CAST(PODType*,realloc(array, new_N * sizeof(PODType)))static_cast<PODType*>(realloc(array, new_N * sizeof(PODType
)))
;
3
Within the expansion of the macro '__STATIC_CAST':
a
Attempt to reallocate memory
98 if (new_array) {
4
Reallocation failed
5
Taking false branch
99 fill(new_array + N, new_array + new_N, PODType());
100 return pair<PODType*, size_t>(new_array, new_N);
101 }
102 else
103 return pair<PODType*, size_t>(__STATIC_CAST(PODType*,0)static_cast<PODType*>(0), 0);
104 }
105 else
106 return pair<PODType*, size_t>(array, N);
107}
108
109// array is a pointer to N elements of type PODType. Allocate a new
110// array of N elements, copying the values from the old array to the new.
111// Return a pointer to the new array. It is assumed that array is non-null
112// and N is nonzero.
113template <class PODType>
114PODType* _Stl_copy_array(const PODType* array, size_t N) {
115 PODType* result = __STATIC_CAST(PODType*,malloc(N * sizeof(PODType)))static_cast<PODType*>(malloc(N * sizeof(PODType)));
116 if (result)
117 copy(array, array + N, result);
118 return result;
119}
120
121locale ios_base::imbue(const locale& loc) {
122 locale previous = _M_locale;
123 _M_locale = loc;
124 _M_invoke_callbacks(imbue_event);
125 return previous;
126}
127
128int ios_base::_S_index = 0;
129
130int _STLP_CALL ios_base::xalloc()
131{
132 static _STLP_STATIC_MUTEX_STLP_mutex_base L _STLP_MUTEX_INITIALIZER= { { { 0, 0, 0, 0, 0, { 0 } } } };
133 _STLP_auto_lock sentry(L);
134 return _S_index++;
135}
136
137long& ios_base::iword(int index) {
138 static long dummy = 0;
139
140 pair<long*, size_t> tmp = _Stl_expand_array(_M_iwords, _M_num_iwords, index);
141 if (tmp.first) { // The allocation, if any, succeeded.
142 _M_iwords = tmp.first;
143 _M_num_iwords = tmp.second;
144 return _M_iwords[index];
145 }
146 else {
147 _M_setstate_nothrow(badbit);
148 _M_check_exception_mask();
149 return dummy;
150 }
151}
152
153
154void*& ios_base::pword(int index) {
155 static void* dummy = 0;
156
157 pair<void**, size_t> tmp = _Stl_expand_array(_M_pwords, _M_num_pwords, index);
158 if (tmp.first) { // The allocation, if any, succeeded.
159 _M_pwords = tmp.first;
160 _M_num_pwords = tmp.second;
161 return _M_pwords[index];
162 }
163 else {
164 _M_setstate_nothrow(badbit);
165 _M_check_exception_mask();
166 return dummy;
167 }
168}
169
170void ios_base::register_callback(event_callback __fn, int index) {
171 pair<pair<event_callback, int>*, size_t> tmp
172 = _Stl_expand_array(_M_callbacks, _M_num_callbacks, (int)_M_callback_index /* fbp: index ??? */ );
1
Calling '_Stl_expand_array'
6
Reallocation of 1st parameter failed
173 if (tmp.first) {
7
Taking false branch
174 _M_callbacks = tmp.first;
175 _M_num_callbacks = tmp.second;
176 _M_callbacks[_M_callback_index++] = make_pair(__fn, index);
177 }
178 else {
179 _M_setstate_nothrow(badbit);
180 _M_check_exception_mask();
181 }
182}
8
Memory is never released; potential leak
183
184// Invokes all currently registered callbacks for a particular event.
185// Behaves correctly even if one of the callbacks adds a new callback.
186void ios_base::_M_invoke_callbacks(event E) {
187 for (size_t i = _M_callback_index; i > 0; --i) {
188 event_callback f = _M_callbacks[i-1].first;
189 int n = _M_callbacks[i-1].second;
190 f(E, *this, n);
191 }
192}
193
194// This function is called if the state, rdstate(), has a bit set
195// that is also set in the exception mask exceptions().
196void ios_base::_M_throw_failure() {
197 const char* arg ;
198# if 0
199 char buffer[256];
200 char* ptr;
201 strcpy(buffer, "ios failure: rdstate = 0x");
202 ptr = __write_integer(buffer+strlen(buffer), ios_base::hex, __STATIC_CAST(unsigned long,_M_iostate)static_cast<unsigned long>(_M_iostate));
203 strcpy(ptr, " mask = 0x");
204 ptr = __write_integer(buffer+strlen(buffer), ios_base::hex, __STATIC_CAST(unsigned long,_M_exception_mask)static_cast<unsigned long>(_M_exception_mask));
205 *ptr = 0;
206 arg = buffer;
207# else
208 arg = "ios failure";
209# endif
210
211# ifndef _STLP_USE_EXCEPTIONS
212 fputs(arg, stderrstderr);
213# else
214 throw failure(arg);
215# endif
216}
217
218// Copy x's state to *this. This member function is used in the
219// implementation of basic_ios::copyfmt. Does not copy _M_exception_mask
220// or _M_iostate.
221void ios_base::_M_copy_state(const ios_base& x) {
222 _M_fmtflags = x._M_fmtflags; // Copy the flags, except for _M_iostate
223 _M_openmode = x._M_openmode; // and _M_exception_mask.
224 _M_seekdir = x._M_seekdir;
225 _M_precision = x._M_precision;
226 _M_width = x._M_width;
227
228 if (_M_locale != x._M_locale) {
229 _M_locale = x._M_locale;
230 _M_cached_ctype = x._M_cached_ctype;
231 _M_cached_numpunct = x._M_cached_numpunct;
232 }
233
234 if (x._M_callbacks) {
235 pair<event_callback, int>* tmp = _Stl_copy_array(x._M_callbacks, x._M_callback_index);
236 if (tmp) {
237 free(_M_callbacks);
238 _M_callbacks = tmp;
239 _M_num_callbacks = _M_callback_index = x._M_callback_index;
240 }
241 else {
242 _M_setstate_nothrow(badbit);
243 _M_check_exception_mask();
244 }
245 }
246
247 if (x._M_iwords) {
248 long* tmp = _Stl_copy_array(x._M_iwords, x._M_num_iwords);
249 if (tmp) {
250 free(_M_iwords);
251 _M_iwords = tmp;
252 _M_num_iwords = x._M_num_iwords;
253 }
254 else {
255 _M_setstate_nothrow(badbit);
256 _M_check_exception_mask();
257 }
258 }
259
260 if (x._M_pwords) {
261 void** tmp = _Stl_copy_array(x._M_pwords, x._M_num_pwords);
262 if (tmp) {
263 free(_M_pwords);
264 _M_pwords = tmp;
265 _M_num_pwords = x._M_num_pwords;
266 }
267 else {
268 _M_setstate_nothrow(badbit);
269 _M_check_exception_mask();
270 }
271 }
272}
273
274
275// ios's (protected) default constructor. The standard says that all
276// fields have indeterminate values; we initialize them to zero for
277// simplicity. The only thing that really matters is that the arrays
278// are all initially null pointers, and the array element counts are all
279// initially zero.
280ios_base::ios_base()
281 : _M_fmtflags(0), _M_iostate(0), _M_openmode(0), _M_seekdir(0),
282 _M_exception_mask(0),
283 _M_precision(0), _M_width(0),
284 _M_locale(),
285 _M_callbacks(0), _M_num_callbacks(0), _M_callback_index(0),
286 _M_iwords(0), _M_num_iwords(0),
287 _M_pwords(0),
288 _M_num_pwords(0) , _M_cached_ctype(0), _M_cached_numpunct(0)
289{ }
290
291// ios's destructor.
292ios_base::~ios_base() {
293 _M_invoke_callbacks(erase_event);
294 free(_M_callbacks);
295 free(_M_iwords);
296 free(_M_pwords);
297}
298
299//----------------------------------------------------------------------
300// Force instantiation of basic_ios
301// For DLL exports, they are already instantiated.
302# if !defined(_STLP_NO_FORCE_INSTANTIATE)
303template class _STLP_CLASS_DECLSPEC basic_ios<char, char_traits<char> >;
304# ifndef _STLP_NO_WCHAR_T
305template class _STLP_CLASS_DECLSPEC basic_ios<wchar_t, char_traits<wchar_t> >;
306# endif /* _STLP_NO_WCHAR_T */
307# endif
308
309_STLP_END_NAMESPACE}
310
311// Local Variables:
312// mode:C++
313// End: