From: Pierre-Emmanuel Patry Date: Tue, 23 May 2023 14:45:08 +0000 (+0200) Subject: gccrs: libproc_macro: Fix Tokenstream growth X-Git-Tag: basepoints/gcc-15~2483 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=cf58150bea85a633247c77f1989c6389c8eb7974;p=thirdparty%2Fgcc.git gccrs: libproc_macro: Fix Tokenstream growth TokenStream did not copy back enough old data to the new location. This commit also add more explicit memcpy usages in order to facilitate change to utf-8 later. libgrust/ChangeLog: * libproc_macro/ffistring.cc (FFIString::make_ffistring): Add explicit sizeof and utf-8 warning. (FFIString::clone): Add explicit sizeof and utf-8 warning. * libproc_macro/ident.cc (Ident::clone): Likewise. (Ident::make_ident): Likewise. * libproc_macro/tokenstream.cc (TokenStream::grow): Fix vector growth. (TokenStream__clone): Add explicit sizeof. Signed-off-by: Pierre-Emmanuel Patry --- diff --git a/libgrust/libproc_macro/ffistring.cc b/libgrust/libproc_macro/ffistring.cc index 06e4e81dc245..2de674cfcfbc 100644 --- a/libgrust/libproc_macro/ffistring.cc +++ b/libgrust/libproc_macro/ffistring.cc @@ -42,7 +42,8 @@ FFIString FFIString::make_ffistring (const unsigned char *data, std::uint64_t len) { unsigned char *inner = new unsigned char[len]; - std::memcpy (inner, data, len); + // FIXME: UTF-8 Update this with sizeof codepoint instead + std::memcpy (inner, data, len * sizeof (unsigned char)); return {inner, len}; } @@ -50,7 +51,8 @@ FFIString FFIString::clone () const { unsigned char *inner = new unsigned char[this->len]; - std::memcpy (inner, this->data, this->len); + // FIXME: UTF-8 Update this with sizeof codepoint instead + std::memcpy (inner, this->data, this->len * sizeof (unsigned char)); return {inner, this->len}; } diff --git a/libgrust/libproc_macro/ident.cc b/libgrust/libproc_macro/ident.cc index 236970519da4..6c8472dad208 100644 --- a/libgrust/libproc_macro/ident.cc +++ b/libgrust/libproc_macro/ident.cc @@ -56,8 +56,9 @@ Ident Ident::clone () const { unsigned char *val = new unsigned char[this->len]; - std::memcpy (val, this->val, this->len); - return {this->is_raw, val, this->len, this->span}; + // FIXME: UTF-8 Update this with sizeof codepoint instead + std::memcpy (val, this->val, this->len * sizeof (char)); + return {this->is_raw, val, this->len}; } Ident @@ -73,8 +74,9 @@ Ident::make_ident (const unsigned char *str, std::uint64_t len, Span span, bool raw) { unsigned char *val = new unsigned char[len]; - std::memcpy (val, str, len); - return {raw, val, len, span}; + // FIXME: UTF-8 Update this with sizeof codepoint instead + std::memcpy (val, str, len * sizeof (char)); + return {raw, val, len}; } void diff --git a/libgrust/libproc_macro/tokenstream.cc b/libgrust/libproc_macro/tokenstream.cc index 25e42dc3dc91..d1116fff7ee6 100644 --- a/libgrust/libproc_macro/tokenstream.cc +++ b/libgrust/libproc_macro/tokenstream.cc @@ -51,7 +51,7 @@ TokenStream::grow (std::uint64_t delta) auto new_capacity = capacity + (delta != 0 ? delta : 1); auto *new_data = new TokenTree[new_capacity]; capacity = new_capacity; - std::memcpy (new_data, data, size); + std::memcpy (new_data, data, size * sizeof (TokenTree)); delete[] data; data = new_data; } @@ -107,7 +107,7 @@ extern "C" TokenStream TokenStream__clone (const TokenStream *ts) { auto *data = new TokenTree[ts->capacity]; - std::memcpy (data, ts->data, ts->size); + std::memcpy (data, ts->data, ts->size * sizeof (TokenTree)); return {data, ts->size, ts->capacity}; }