@@ -144,7 +144,30 @@ class Lexer
144
144
/* *
145
145
* Lex the contents of a string instead of a file
146
146
*/
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)
148
171
{
149
172
// We can perform this ugly cast to a non-const char* since we're only
150
173
// *reading* the string. This would not be valid if we were doing any
0 commit comments