Skip to content

Commit e35da26

Browse files
Merge #988
988: lexer: Add reference and warning documentation r=tschwinge a=CohenArthur 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> Closes #987 Co-authored-by: Arthur Cohen <arthur.cohen@embecosm.com>
2 parents 6cf9f8c + 45eac56 commit e35da26

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

gcc/rust/lex/rust-lex.h

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,30 @@ class Lexer
144144
/**
145145
* Lex the contents of a string instead of a file
146146
*/
147-
static Lexer lex_string (std::string input)
147+
// FIXME: This is unsafe!
148+
// Since we are taking a reference to the string's internal buffer, we must
149+
// ensure that the lexer does not outlive the string, which might not always
150+
// be the case.
151+
//
152+
// We could have a fix, which would include using fmemopen() to allocate a
153+
// buffer and copy the string inside it.
154+
// ```
155+
// // There will be an extra nul-terminator byte written on fclose(), so
156+
// // account for that
157+
// auto string_file = fmemopen(NULL, input.length() + 1, "wr");
158+
// fwrite(input.c_str(), sizeof(char), input.length(), string_file);
159+
// auto wrapper = RAIIFile(string_file);
160+
// ```
161+
// But sadly our RAIIFile does not support moving really well... And the
162+
// destructor, which calls fclose(), gets called, triggering a lack of a
163+
// buffer to parse :)
164+
//
165+
// We need to look into fixing the RAIIFile so that it supports this
166+
// behaviour. I'm assuming this will be something like fixing one of the copy
167+
// or move constructors, but is outside of the scope of this fix. For now,
168+
// make sure your lexers don't live longer than the strings they're trying
169+
// to lex
170+
static Lexer lex_string (std::string &input)
148171
{
149172
// We can perform this ugly cast to a non-const char* since we're only
150173
// *reading* the string. This would not be valid if we were doing any

0 commit comments

Comments
 (0)