Skip to content

Commit

Permalink
Block over growth during insert opcode
Browse files Browse the repository at this point in the history
  • Loading branch information
mhosken committed Mar 15, 2016
1 parent 18a0544 commit 7187f8b
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 8 deletions.
6 changes: 3 additions & 3 deletions src/Silf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -355,10 +355,10 @@ uint16 Silf::getClassGlyph(uint16 cid, unsigned int index) const
bool Silf::runGraphite(Segment *seg, uint8 firstPass, uint8 lastPass, int dobidi) const
{
assert(seg != 0);
SlotMap map(*seg, m_dir);
unsigned int maxSize = seg->slotCount() * MAX_SEG_GROWTH_FACTOR;
SlotMap map(*seg, m_dir, maxSize);
FiniteStateMachine fsm(map, seg->getFace()->logger());
vm::Machine m(map);
unsigned int initSize = seg->slotCount();
uint8 lbidi = m_bPass;
#if !defined GRAPHITE2_NTRACING
json * const dbgout = seg->getFace()->logger();
Expand Down Expand Up @@ -424,7 +424,7 @@ bool Silf::runGraphite(Segment *seg, uint8 firstPass, uint8 lastPass, int dobidi
return false;
// only subsitution passes can change segment length, cached subsegments are short for their text
if (m.status() != vm::Machine::finished
|| (seg->slotCount() && seg->slotCount() * MAX_SEG_GROWTH_FACTOR < initSize))
|| (seg->slotCount() && seg->slotCount() > maxSize))
return false;
}
return true;
Expand Down
9 changes: 6 additions & 3 deletions src/inc/Rule.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ class SlotMap
{
public:
enum {MAX_SLOTS=64};
SlotMap(Segment & seg, uint8 direction);
SlotMap(Segment & seg, uint8 direction, int maxSize);

Slot * * begin();
Slot * * end();
Expand All @@ -121,13 +121,15 @@ class SlotMap
void highpassed(bool v) { m_highpassed = v; }

uint8 dir() const { return m_dir; }
int decMax() { return --m_maxSize; }

Segment & segment;
private:
Slot * m_slot_map[MAX_SLOTS+1];
unsigned short m_size;
unsigned short m_precontext;
Slot * m_highwater;
int m_maxSize;
uint8 m_dir;
bool m_highpassed;
};
Expand Down Expand Up @@ -242,8 +244,9 @@ void FiniteStateMachine::Rules::accumulate_rules(const State &state)
}

inline
SlotMap::SlotMap(Segment & seg, uint8 direction)
: segment(seg), m_size(0), m_precontext(0), m_highwater(0), m_dir(direction), m_highpassed(false)
SlotMap::SlotMap(Segment & seg, uint8 direction, int maxSize)
: segment(seg), m_size(0), m_precontext(0), m_highwater(0),
m_maxSize(maxSize), m_dir(direction), m_highpassed(false)
{
m_slot_map[0] = 0;
}
Expand Down
2 changes: 1 addition & 1 deletion src/inc/Segment.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ of the License or (at your option) any later version.
#include "inc/List.h"
#include "inc/Collider.h"

#define MAX_SEG_GROWTH_FACTOR 256
#define MAX_SEG_GROWTH_FACTOR 64

namespace graphite2 {

Expand Down
1 change: 1 addition & 0 deletions src/inc/opcodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ STARTOP(put_copy)
ENDOP

STARTOP(insert)
if (smap.decMax() <= 0) DIE;
Slot *newSlot = seg.newSlot();
if (!newSlot) DIE;
Slot *iss = is;
Expand Down
2 changes: 1 addition & 1 deletion tests/vm/basic_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ int main(int argc, char *argv[])
Segment seg;
Slot s1;
uint32 ret = 0;
SlotMap smap(seg, 0);
SlotMap smap(seg, 0, 0);
Machine m(smap);
smap.pushSlot(&s1);
slotref * map = smap.begin();
Expand Down

0 comments on commit 7187f8b

Please sign in to comment.