From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1643) id 23361380FDDD; Wed, 8 Jun 2022 12:14:31 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 23361380FDDD Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: Thomas Schwinge To: gcc-cvs@gcc.gnu.org Subject: [gcc/devel/rust/master] lexer: Add reference and warning documentation X-Act-Checkin: gcc X-Git-Author: Arthur Cohen X-Git-Refname: refs/heads/devel/rust/master X-Git-Oldrev: 6cf9f8c99c5813a23d7cec473fedf00683f409e4 X-Git-Newrev: 45eac568686745af73848f6c238fefcd87e315de Message-Id: <20220608121431.23361380FDDD@sourceware.org> Date: Wed, 8 Jun 2022 12:14:31 +0000 (GMT) X-BeenThere: gcc-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 08 Jun 2022 12:14:31 -0000 https://gcc.gnu.org/g:45eac568686745af73848f6c238fefcd87e315de commit 45eac568686745af73848f6c238fefcd87e315de Author: Arthur Cohen Date: Wed Mar 2 15:18:40 2022 +0100 lexer: Add reference and warning documentation Fixes the -fself-test invalid memory accesses and adds documentation regarding a possible future fix. Co-authored-by: tschwinge Co-authored-by: philberty Diff: --- gcc/rust/lex/rust-lex.h | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/gcc/rust/lex/rust-lex.h b/gcc/rust/lex/rust-lex.h index c50f63208de..039c783c564 100644 --- a/gcc/rust/lex/rust-lex.h +++ b/gcc/rust/lex/rust-lex.h @@ -144,7 +144,30 @@ public: /** * Lex the contents of a string instead of a file */ - static Lexer lex_string (std::string input) + // FIXME: This is unsafe! + // Since we are taking a reference to the string's internal buffer, we must + // ensure that the lexer does not outlive the string, which might not always + // be the case. + // + // We could have a fix, which would include using fmemopen() to allocate a + // buffer and copy the string inside it. + // ``` + // // There will be an extra nul-terminator byte written on fclose(), so + // // account for that + // auto string_file = fmemopen(NULL, input.length() + 1, "wr"); + // fwrite(input.c_str(), sizeof(char), input.length(), string_file); + // auto wrapper = RAIIFile(string_file); + // ``` + // But sadly our RAIIFile does not support moving really well... And the + // destructor, which calls fclose(), gets called, triggering a lack of a + // buffer to parse :) + // + // We need to look into fixing the RAIIFile so that it supports this + // behaviour. I'm assuming this will be something like fixing one of the copy + // or move constructors, but is outside of the scope of this fix. For now, + // make sure your lexers don't live longer than the strings they're trying + // to lex + static Lexer lex_string (std::string &input) { // We can perform this ugly cast to a non-const char* since we're only // *reading* the string. This would not be valid if we were doing any