public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc/devel/rust/master] lexer: Add reference and warning documentation
@ 2022-06-08 12:14 Thomas Schwinge
  0 siblings, 0 replies; only message in thread
From: Thomas Schwinge @ 2022-06-08 12:14 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:45eac568686745af73848f6c238fefcd87e315de

commit 45eac568686745af73848f6c238fefcd87e315de
Author: Arthur Cohen <arthur.cohen@embecosm.com>
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 <thomas@schwinge.name>
    Co-authored-by: philberty <philip.herron@embecosm.com>

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


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2022-06-08 12:14 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-06-08 12:14 [gcc/devel/rust/master] lexer: Add reference and warning documentation Thomas Schwinge

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).