This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

libstdc++/9168: codecvt<char, char, mbstate_t> overwrites output buffers


>Number:         9168
>Category:       libstdc++
>Synopsis:       codecvt<char, char, mbstate_t> overwrites output buffers
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    unassigned
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Sat Jan 04 01:56:00 PST 2003
>Closed-Date:
>Last-Modified:
>Originator:     peturr02@ru.is
>Release:        gcc-3.2.1
>Organization:
>Environment:
Red Hat Linux 8.0
>Description:
codecvt<char, char, mbstate_t>::in and codecvt<char, char, mbstate_t>::out copy the input buffer to the output buffer, but should not do so.

According to the resolution to library defect report 19 <http://anubis.dkuug.dk/jtc1/sc22/wg21/docs/lwg-defects.html#19>, codecvt<>::in and codecvt<>::out should not overwrite the output buffers when they return noconv:

"If returns noconv [...] there are no changes to the values in [to, to_limit)."

The resolution to defect report 305 is also clear:

"codecvt<char,char,mbstate_t> stores no characters."
>How-To-Repeat:
See attachment
>Fix:
Remove the memcpy calls from in and out.
>Release-Note:
>Audit-Trail:
>Unformatted:
----gnatsweb-attachment----
Content-Type: text/plain; name="defect19.cc"
Content-Disposition: inline; filename="defect19.cc"

#include <locale>
#include <cwchar>
#include <cstdlib>
#include <cstring>
#include <algorithm>

int main()
{
	typedef std::codecvt<char, char, std::mbstate_t> codecvt_type;

	std::mbstate_t state;
	std::codecvt_base::result res;

	const char* from = "foo";
	const char* from_end = from + strlen(from);
	const char* from_next;

	const std::size_t to_size = 256;
	char to[to_size];
	char* to_end = to + to_size;
	char* to_next;

	std::mbstate_t state0;
	std::memset(&state0, 0, sizeof(state0));

	std::memset(to, 'X', to_size);
	std::memset(&state, 0, sizeof(state));

	std::locale loc (std::locale::classic());
	const codecvt_type& cvt = std::use_facet<codecvt_type>(loc);
	assert(cvt.always_noconv());

	res = cvt.in(state, from, from_end, from_next,
		     to, to_end, to_next);
	assert(res == std::codecvt_base::noconv);
	assert(to_next == to);
	assert(std::memcmp(&state, &state0, sizeof(state)) == 0);
	assert(std::count(to, to_end, 'X') == to_size);

	res = cvt.out(state, from, from_end, from_next,
		      to, to_end, to_next);
	assert(res == std::codecvt_base::noconv);
	assert(to_next == to);
	assert(std::memcmp(&state, &state0, sizeof(state)) == 0);
	assert(std::count(to, to_end, 'X') == to_size);

	return 0;
}


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]