Bug Summary

File:stlport/unxlngi6.pro/misc/build/STLport-4.5/src/locale_impl.cpp
Location:line 298, column 5
Description:Size argument is greater than the free space in the destination buffer

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# include "stlport_prefix.h"
19
20#include "locale_impl.h"
21#include <locale>
22#include <typeinfo>
23#include <stdexcept>
24#include "c_locale.h"
25#include "aligned_buffer.h"
26
27
28#include "locale_impl.h"
29#include <stl/_codecvt.h>
30#include <stl/_collate.h>
31#include <stl/_ctype.h>
32#include <stl/_monetary.h>
33#include "message_facets.h"
34
35_STLP_BEGIN_NAMESPACEnamespace _STL {
36
37_Locale_impl::_Locale_impl(const char* s) : name(s) {}
38_Locale_impl::~_Locale_impl() {}
39void _Locale_impl::incr() {}
40void _Locale_impl::decr() {}
41
42// _Locale_impl non-inline member functions.
43void _STLP_CALL
44_Locale_impl::_M_throw_bad_cast()
45{
46 _STLP_THROW(bad_cast())throw bad_cast();
47}
48
49static void
50_Stl_loc_assign_ids() {
51 // This assigns ids to every facet that is a member of a category,
52 // and also to money_get/put, num_get/put, and time_get/put
53 // instantiated using ordinary pointers as the input/output
54 // iterators. (The default is [io]streambuf_iterator.)
55
56 money_get<char, istreambuf_iterator<char, char_traits<char> > >::id._M_index = 8;
57 money_get<char, const char*>::id._M_index = 9;
58 money_put<char, ostreambuf_iterator<char, char_traits<char> > >::id._M_index = 10;
59 money_put<char, char*>::id._M_index = 11;
60
61 num_get<char, istreambuf_iterator<char, char_traits<char> > >::id._M_index = 12;
62 num_get<char, const char*>::id._M_index = 13;
63 num_put<char, ostreambuf_iterator<char, char_traits<char> > >::id._M_index = 14;
64 num_put<char, char*>::id._M_index = 15;
65 time_get<char, istreambuf_iterator<char, char_traits<char> > >::id._M_index = 16;
66 time_get<char, const char*>::id._M_index = 17;
67 time_put<char, ostreambuf_iterator<char, char_traits<char> > >::id._M_index = 18;
68 time_put<char, char*>::id._M_index = 19;
69
70# ifndef _STLP_NO_WCHAR_T
71
72 money_get<wchar_t, istreambuf_iterator<wchar_t, char_traits<wchar_t> > >::id._M_index = 27;
73 money_get<wchar_t, const wchar_t*>::id._M_index = 28;
74 money_put<wchar_t, ostreambuf_iterator<wchar_t, char_traits<wchar_t> > >::id._M_index = 29;
75 money_put<wchar_t, wchar_t*>::id._M_index = 30;
76
77 num_get<wchar_t, istreambuf_iterator<wchar_t, char_traits<wchar_t> > >::id._M_index = 31;
78 num_get<wchar_t, const wchar_t*>::id._M_index = 32;
79 num_put<wchar_t, ostreambuf_iterator<wchar_t, char_traits<wchar_t> > > ::id._M_index = 33;
80 num_put<wchar_t, wchar_t*>::id._M_index = 34;
81 time_get<wchar_t, istreambuf_iterator<wchar_t, char_traits<wchar_t> > >::id._M_index = 35;
82 time_get<wchar_t, const wchar_t*>::id._M_index = 36;
83 time_put<wchar_t, ostreambuf_iterator<wchar_t, char_traits<wchar_t> > >::id._M_index = 37;
84 time_put<wchar_t, wchar_t*>::id._M_index = 38;
85 // messages<wchar_t>::id._M_index = 38;
86# endif
87
88 // locale::id::_S_max = 39;
89}
90
91static _Stl_aligned_buffer<_Locale_impl> _S_classic_locale;
92
93static _Stl_aligned_buffer<collate<char> > _S_collate_char;
94static _Stl_aligned_buffer<ctype<char> > _S_ctype_char;
95static _Stl_aligned_buffer<codecvt<char, char, mbstate_t> > _S_codecvt_char;
96static _Stl_aligned_buffer<moneypunct<char, true> > _S_moneypunct_true_char;
97static _Stl_aligned_buffer<moneypunct<char, false> > _S_moneypunct_false_char;
98static _Stl_aligned_buffer<numpunct<char> > _S_numpunct_char;
99static _Stl_aligned_buffer<messages<char> > _S_messages_char;
100
101static _Stl_aligned_buffer<money_get<char, istreambuf_iterator<char, char_traits<char> > > > _S_money_get_char;
102static _Stl_aligned_buffer<money_put<char, ostreambuf_iterator<char, char_traits<char> > > > _S_money_put_char;
103static _Stl_aligned_buffer<num_get<char, istreambuf_iterator<char, char_traits<char> > > > _S_num_get_char;
104static _Stl_aligned_buffer<num_put<char, ostreambuf_iterator<char, char_traits<char> > > > _S_num_put_char;
105static _Stl_aligned_buffer<time_get<char, istreambuf_iterator<char, char_traits<char> > > > _S_time_get_char;
106static _Stl_aligned_buffer<time_put<char, ostreambuf_iterator<char, char_traits<char> > > > _S_time_put_char;
107
108# ifndef _STLP_NO_WCHAR_T
109static _Stl_aligned_buffer<collate<wchar_t> > _S_collate_wchar;
110static _Stl_aligned_buffer<ctype<wchar_t> > _S_ctype_wchar;
111static _Stl_aligned_buffer<codecvt<wchar_t, char, mbstate_t> > _S_codecvt_wchar;
112static _Stl_aligned_buffer<moneypunct<wchar_t, true> > _S_moneypunct_true_wchar;
113static _Stl_aligned_buffer<moneypunct<wchar_t, false> > _S_moneypunct_false_wchar;
114static _Stl_aligned_buffer<numpunct<wchar_t> > _S_numpunct_wchar;
115static _Stl_aligned_buffer<messages<wchar_t> > _S_messages_wchar;
116
117static _Stl_aligned_buffer<money_get<wchar_t, istreambuf_iterator<wchar_t, char_traits<wchar_t> > > > _S_money_get_wchar;
118static _Stl_aligned_buffer<money_put<wchar_t, ostreambuf_iterator<wchar_t, char_traits<wchar_t> > > > _S_money_put_wchar;
119static _Stl_aligned_buffer<num_get<wchar_t, istreambuf_iterator<wchar_t, char_traits<wchar_t> > > > _S_num_get_wchar;
120static _Stl_aligned_buffer<num_put<wchar_t, ostreambuf_iterator<wchar_t, char_traits<wchar_t> > > > _S_num_put_wchar;
121static _Stl_aligned_buffer<time_get<wchar_t, istreambuf_iterator<wchar_t, char_traits<wchar_t> > > > _S_time_get_wchar;
122static _Stl_aligned_buffer<time_put<wchar_t, ostreambuf_iterator<wchar_t, char_traits<wchar_t> > > > _S_time_put_wchar;
123
124# endif
125
126// static _Messages _Null_messages;
127
128static locale::facet* _S_classic_facets[] = {
129 (locale::facet*)0,
130 (locale::facet*)&_S_collate_char,
131 (locale::facet*)&_S_ctype_char,
132 (locale::facet*)&_S_codecvt_char,
133 (locale::facet*)&_S_moneypunct_true_char,
134 (locale::facet*)&_S_moneypunct_false_char,
135 (locale::facet*)&_S_numpunct_char,
136 (locale::facet*)&_S_messages_char,
137
138 (locale::facet*)&_S_money_get_char,
139 (locale::facet*)0,
140 (locale::facet*)&_S_money_put_char,
141 (locale::facet*)0,
142
143 (locale::facet*)&_S_num_get_char,
144 (locale::facet*)0,
145 (locale::facet*)&_S_num_put_char,
146 (locale::facet*)0,
147 (locale::facet*)&_S_time_get_char,
148 (locale::facet*)0,
149 (locale::facet*)&_S_time_put_char,
150 (locale::facet*)0,
151# ifndef _STLP_NO_WCHAR_T
152 (locale::facet*)&_S_collate_wchar,
153 (locale::facet*)&_S_ctype_wchar,
154 (locale::facet*)&_S_codecvt_wchar,
155 (locale::facet*)&_S_moneypunct_true_wchar,
156 (locale::facet*)&_S_moneypunct_false_wchar,
157 (locale::facet*)&_S_numpunct_wchar,
158 (locale::facet*)&_S_messages_wchar,
159
160 (locale::facet*)&_S_money_get_wchar,
161 (locale::facet*)0,
162 (locale::facet*)&_S_money_put_wchar,
163 (locale::facet*)0,
164
165 (locale::facet*)&_S_num_get_wchar,
166 (locale::facet*)0,
167 (locale::facet*)&_S_num_put_wchar,
168 (locale::facet*)0,
169 (locale::facet*)&_S_time_get_wchar,
170 (locale::facet*)0,
171 (locale::facet*)&_S_time_put_wchar,
172 (locale::facet*)0,
173# endif
174 0
175};
176
177_Locale_impl*
178_Locale_impl::make_classic_locale() {
179 // The classic locale contains every facet that belongs to a category.
180 _Locale_impl* classic = (_Locale_impl*) &_S_classic_locale;
181
182 new (classic) _Locale_impl("C");
183
184 // ctype category
185 new(&_S_ctype_char) ctype<char>(0, false, 1);
186 // collate category
187 new(&_S_collate_char) collate<char>(1);
188 new(&_S_codecvt_char) codecvt<char, char, mbstate_t>(1);
189 // monetary category
190 new (&_S_moneypunct_true_char) moneypunct<char, true>(1);
191 new (&_S_moneypunct_false_char) moneypunct<char, false>(1);
192 new (&_S_money_get_char) money_get<char, istreambuf_iterator<char, char_traits<char> > >(1);
193 new (&_S_money_put_char) money_put<char, ostreambuf_iterator<char, char_traits<char> > >(1);
194 // numeric category
195 new(&_S_numpunct_char) numpunct<char>(1);
196 new (&_S_num_get_char) num_get<char, istreambuf_iterator<char, char_traits<char> > >(1);
197 new (&_S_num_put_char) num_put<char, ostreambuf_iterator<char, char_traits<char> > >(1);
198 new (&_S_time_get_char) time_get<char, istreambuf_iterator<char, char_traits<char> > >(1);
199 new (&_S_time_put_char) time_put<char, ostreambuf_iterator<char, char_traits<char> > >(1);
200 // messages category
201 // new (&_S_messages_char)messages<char>(&_Null_messages);
202
203# ifndef _STLP_NO_WCHAR_T
204 // ctype category
205 new(&_S_ctype_wchar) ctype<wchar_t>(1);
206 // collate category
207 new(&_S_collate_wchar) collate<wchar_t>(1);
208 new(&_S_codecvt_wchar) codecvt<wchar_t, char, mbstate_t>(1);
209 // monetary category
210 new (&_S_moneypunct_true_wchar) moneypunct<wchar_t, true>(1);
211 new (&_S_moneypunct_false_wchar) moneypunct<wchar_t, false>(1);
212 new (&_S_money_get_wchar) money_get<wchar_t, istreambuf_iterator<wchar_t, char_traits<wchar_t> > >(1);
213 new (&_S_money_put_wchar) money_put<wchar_t, ostreambuf_iterator<wchar_t, char_traits<wchar_t> > >(1);
214 // numeric category
215 new(&_S_numpunct_wchar) numpunct<wchar_t>(1);
216 new (&_S_num_get_wchar) num_get<wchar_t, istreambuf_iterator<wchar_t, char_traits<wchar_t> > >(1);
217 new (&_S_num_put_wchar) num_put<wchar_t, ostreambuf_iterator<wchar_t, char_traits<wchar_t> > >(1);
218 new (&_S_time_get_wchar) time_get<wchar_t, istreambuf_iterator<wchar_t, char_traits<wchar_t> > >(1);
219 new (&_S_time_put_wchar) time_put<wchar_t, ostreambuf_iterator<wchar_t, char_traits<wchar_t> > >(1);
220 // new (&_S_messages_wchar)messages<wchar_t>(&_Null_messages);
221# endif
222
223 classic->facets = _S_classic_facets;
224 classic->_M_size = locale::id::_S_max;
225 return classic;
226}
227
228
229//----------------------------------------------------------------------
230
231// Declarations of (non-template) facets' static data members
232size_t locale::id::_S_max = 39;
233
234_STLP_STATIC_MEMBER_DECLSPEC locale::id collate<char>::id = { 1 };
235_STLP_STATIC_MEMBER_DECLSPEC locale::id ctype<char>::id = { 2 };
236
237# ifndef _STLP_NO_MBSTATE_T
238_STLP_STATIC_MEMBER_DECLSPEC locale::id codecvt<char, char, mbstate_t>::id = { 3 };
239# ifndef _STLP_NO_WCHAR_T
240_STLP_STATIC_MEMBER_DECLSPEC locale::id codecvt<wchar_t, char, mbstate_t>::id = { 22 };
241# endif
242# endif
243
244_STLP_STATIC_MEMBER_DECLSPEC locale::id moneypunct<char, true>::id = { 4 };
245_STLP_STATIC_MEMBER_DECLSPEC locale::id moneypunct<char, false>::id = { 5 };
246_STLP_STATIC_MEMBER_DECLSPEC locale::id numpunct<char>::id = { 6 } ;
247_STLP_STATIC_MEMBER_DECLSPEC locale::id messages<char>::id = { 7 };
248
249# ifndef _STLP_NO_WCHAR_T
250_STLP_STATIC_MEMBER_DECLSPEC locale::id collate<wchar_t>::id = { 20 };
251_STLP_STATIC_MEMBER_DECLSPEC locale::id ctype<wchar_t>::id = { 21 };
252
253_STLP_STATIC_MEMBER_DECLSPEC locale::id moneypunct<wchar_t, true>::id = { 23 } ;
254_STLP_STATIC_MEMBER_DECLSPEC locale::id moneypunct<wchar_t, false>::id = { 24 } ;
255
256_STLP_STATIC_MEMBER_DECLSPEC locale::id numpunct<wchar_t>::id = { 25 };
257_STLP_STATIC_MEMBER_DECLSPEC locale::id messages<wchar_t>::id = { 26 };
258# endif
259
260//
261// locale class
262//
263
264locale::facet::~facet() {}
265
266# if ! defined ( _STLP_MEMBER_TEMPLATES1 ) || defined (_STLP_INLINE_MEMBER_TEMPLATES)
267// members that fail to be templates
268bool locale::operator()(const string& __x,
269 const string& __y) const {
270 return __locale_do_operator_call(this, __x, __y);
271}
272
273# ifndef _STLP_NO_WCHAR_T
274bool locale::operator()(const wstring& __x,
275 const wstring& __y) const {
276 return __locale_do_operator_call(this, __x, __y);
277}
278# endif
279# endif
280
281
282_Locale_impl* _Stl_loc_global_impl = 0;
283locale _Stl_loc_classic_locale((_Locale_impl*) &_S_classic_locale);
284_STLP_STATIC_MUTEX_STLP_mutex_base _Stl_loc_global_locale_lock _STLP_MUTEX_INITIALIZER= { { { 0, 0, 0, 0, 0, { 0 } } } };
285
286
287//----------------------------------------------------------------------
288// class locale
289
290void _STLP_CALL
291locale::_M_throw_runtime_error(const char* name)
292{
293 char buf[256];
294
295 if (name) {
1
Assuming 'name' is non-null
2
Taking true branch
296 const char* prefix = "bad locale name: ";
297 strcpy(buf, prefix);
298 strncat(buf, name, 256 - strlen(prefix));
3
Size argument is greater than the free space in the destination buffer
299 buf[255] = '\0';
300 }
301 else {
302 strcpy(buf, "locale error");
303 }
304 _STLP_THROW(runtime_error(buf))throw runtime_error(buf);
305}
306
307#if !( defined (__BORLANDC__) && defined(_RTLDLL))
308
309long ios_base::_Loc_init::_S_count = 0;
310
311ios_base::_Loc_init::_Loc_init() {
312 if (_S_count++ == 0)
313 locale::_S_initialize();
314}
315
316ios_base::_Loc_init::~_Loc_init() {
317 if (--_S_count == 0)
318 locale::_S_uninitialize();
319}
320
321#endif /* _RTLDLL */
322
323// Initialization of the locale system. This must be called before
324// any locales are constructed. (Meaning that it must be called when
325// the I/O library itself is initialized.)
326void _STLP_CALL
327locale::_S_initialize()
328{
329 _Stl_loc_assign_ids();
330 _Stl_loc_global_impl = _Locale_impl::make_classic_locale();
331}
332
333
334
335void _STLP_CALL
336locale::_S_uninitialize()
337{
338 _Stl_loc_global_impl->decr();
339}
340
341
342// Default constructor: create a copy of the global locale.
343locale::locale() : _M_impl(0) {
344 _M_impl = _S_copy_impl(_Stl_loc_global_impl);
345}
346
347locale::locale(_Locale_impl* impl) : _M_impl(impl)
348{}
349
350// Copy constructor
351locale::locale(const locale& L) _STLP_NOTHROW
352 : _M_impl(0)
353{
354 _M_impl = _S_copy_impl(L._M_impl);
355}
356
357// Destructor.
358locale::~locale() _STLP_NOTHROW
359{
360 _M_impl->decr();
361}
362
363// Assignment operator. Much like the copy constructor: just a bit of
364// pointer twiddling.
365const locale& locale::operator=(const locale& L) _STLP_NOTHROW
366{
367 if (this->_M_impl != L._M_impl) {
368 this->_M_impl->decr();
369 this->_M_impl = _S_copy_impl(L._M_impl);
370 }
371 return *this;
372}
373
374locale::facet* locale::_M_get_facet(const locale::id& n) const
375{
376 return n._M_index < _M_impl->size()
377 ? (locale::facet*)_M_impl->facets[n._M_index]
378 : (locale::facet*) 0;
379}
380
381locale::facet* locale::_M_use_facet(const locale::id& n) const
382{
383 locale::facet* f = (n._M_index < _M_impl->size()
384 ? (locale::facet*)_M_impl->facets[n._M_index]
385 : (locale::facet*) 0);
386 if (!f)
387 _M_impl->_M_throw_bad_cast();
388 return f;
389}
390
391string locale::name() const {
392 return _M_impl->name;
393}
394
395static string _Nameless("*");
396
397// Compare two locales for equality.
398bool locale::operator==(const locale& L) const {
399
400 return this->_M_impl == L._M_impl ||
401 (this->name() == L.name() && this->name() != _Nameless);
402}
403
404bool locale::operator!=(const locale& L) const {
405 return !(*this == L);
406}
407
408// Static member functions.
409const locale& _STLP_CALL
410locale::classic() {
411 return _Stl_loc_classic_locale;
412}
413
414locale _STLP_CALL
415locale::global(const locale& L)
416{
417 locale old; // A copy of the old global locale.
418
419 L._M_impl->incr();
420 {
421 _STLP_auto_lock lock(_Stl_loc_global_locale_lock);
422 _Stl_loc_global_impl->decr(); // We made a copy, so it can't be zero.
423 _Stl_loc_global_impl = L._M_impl;
424 }
425
426 // Set the global C locale, if appropriate.
427 if (L.name() != _Nameless)
428 setlocale(LC_ALL__LC_ALL, L.name().c_str());
429
430 return old;
431}
432
433
434// static data members.
435
436# if !defined (_STLP_STATIC_CONST_INIT_BUG) && ! defined (_STLP_USE_DECLSPEC)
437
438const locale::category locale::none;
439const locale::category locale::collate;
440const locale::category locale::ctype;
441const locale::category locale::monetary;
442const locale::category locale::numeric;
443const locale::category locale::time;
444const locale::category locale::messages;
445const locale::category locale::all;
446
447# endif
448
449_STLP_END_NAMESPACE}
450
451//
452// Facets included in classic locale :
453//
454
455
456# if 0
457# include "ctype.cpp"
458# include "collate.cpp"
459# include "codecvt.cpp"
460
461# include "num_put_float.cpp"
462# include "num_put.cpp"
463# include "numpunct.cpp"
464# include "num_get.cpp"
465# include "num_get_float.cpp"
466
467# include "monetary.cpp"
468# include "time_facets.cpp"
469# include "messages.cpp"
470# endif
471