1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 06:58:11 +00:00

Shell: Store LocalFrames as NonnullOwnPtr<LocalFrame> instead

As Vector<T> will relocate objects to resize, we cannot assume that the
address of a specific LocalFrame will stay constant, or that the
reference will not be dangling to begin with.
Fixes an assertion that fires when a frame push causes the Vector to
reallocate.
This commit is contained in:
AnotherTest 2020-11-01 13:28:11 +03:30 committed by Andreas Kling
parent a7f4f6afc3
commit bedad383b5
3 changed files with 28 additions and 12 deletions

View file

@ -30,6 +30,7 @@
#include "Parser.h"
#include <AK/CircularQueue.h>
#include <AK/HashMap.h>
#include <AK/NonnullOwnPtrVector.h>
#include <AK/String.h>
#include <AK/StringBuilder.h>
#include <AK/Types.h>
@ -114,11 +115,18 @@ public:
RefPtr<Line::Editor> editor() const { return m_editor; }
struct LocalFrame {
LocalFrame(const String& name, HashMap<String, RefPtr<AST::Value>> variables)
: name(name)
, local_variables(move(variables))
{
}
String name;
HashMap<String, RefPtr<AST::Value>> local_variables;
};
struct Frame {
Frame(Vector<LocalFrame>& frames, const LocalFrame& frame)
Frame(NonnullOwnPtrVector<LocalFrame>& frames, const LocalFrame& frame)
: frames(frames)
, frame(frame)
{
@ -128,12 +136,12 @@ public:
void leak_frame() { should_destroy_frame = false; }
private:
Vector<LocalFrame>& frames;
NonnullOwnPtrVector<LocalFrame>& frames;
const LocalFrame& frame;
bool should_destroy_frame { true };
};
[[nodiscard]] Frame push_frame();
[[nodiscard]] Frame push_frame(String name);
void pop_frame();
static String escape_token(const String& token);
@ -247,7 +255,7 @@ private:
};
HashMap<String, ShellFunction> m_functions;
Vector<LocalFrame> m_local_frames;
NonnullOwnPtrVector<LocalFrame> m_local_frames;
NonnullRefPtrVector<AST::Redirection> m_global_redirections;
HashMap<String, String> m_aliases;