aboutsummaryrefslogtreecommitdiff
path: root/gcc
diff options
context:
space:
mode:
authorArthur Cohen <arthur.cohen@embecosm.com>2022-03-02 15:18:40 +0100
committerArthur Cohen <arthur.cohen@embecosm.com>2022-03-02 15:18:40 +0100
commit45eac568686745af73848f6c238fefcd87e315de (patch)
treed10f25c6473712c0709ca854b763f06952142896 /gcc
parent6cf9f8c99c5813a23d7cec473fedf00683f409e4 (diff)
downloadgcc-45eac568686745af73848f6c238fefcd87e315de.zip
gcc-45eac568686745af73848f6c238fefcd87e315de.tar.gz
gcc-45eac568686745af73848f6c238fefcd87e315de.tar.bz2
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>
Diffstat (limited to 'gcc')
-rw-r--r--gcc/rust/lex/rust-lex.h25
1 files changed, 24 insertions, 1 deletions
diff --git a/gcc/rust/lex/rust-lex.h b/gcc/rust/lex/rust-lex.h
index c50f632..039c783 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