Skip to content

Commit

Permalink
Merge pull request #94 from pesco/fix-49-stringmap-segfault
Browse files Browse the repository at this point in the history
Fix #49 stringmap segfault
  • Loading branch information
Meredith L. Patterson committed Apr 10, 2014
2 parents f95dc6c + 6875dc3 commit 70da67a
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/backends/llk.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,12 @@ static void *combine_entries(HHashSet *workset, void *dst, const void *src)

// add the mappings of src to dst, marking conflicts and adding the conflicting
// values to workset.
// note: reuses parts of src to build dst!
static void stringmap_merge(HHashSet *workset, HStringMap *dst, HStringMap *src)
{
if(src->epsilon_branch) {
if(dst->epsilon_branch)
dst->epsilon_branch =
combine_entries(workset, dst->epsilon_branch, src->epsilon_branch);
combine_entries(workset, dst->epsilon_branch, src->epsilon_branch);
else
dst->epsilon_branch = src->epsilon_branch;
} else {
Expand All @@ -101,7 +100,7 @@ static void stringmap_merge(HHashSet *workset, HStringMap *dst, HStringMap *src)
if(src->end_branch) {
if(dst->end_branch)
dst->end_branch =
combine_entries(workset, dst->end_branch, src->end_branch);
combine_entries(workset, dst->end_branch, src->end_branch);
else
dst->end_branch = src->end_branch;
}
Expand All @@ -118,10 +117,13 @@ static void stringmap_merge(HHashSet *workset, HStringMap *dst, HStringMap *src)

if(src_) {
HStringMap *dst_ = h_hashtable_get(dst->char_branches, (void *)c);
if(dst_)
if(dst_) {
stringmap_merge(workset, dst_, src_);
else
} else {
if(src_->arena != dst->arena)
src_ = h_stringmap_copy(dst->arena, src_);
h_hashtable_put(dst->char_branches, (void *)c, src_);
}
}
}
}
Expand Down
7 changes: 7 additions & 0 deletions src/cfgrammar.c
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,13 @@ void h_stringmap_update(HStringMap *m, const HStringMap *n)
h_hashtable_merge(combine_stringmap, m->char_branches, n->char_branches);
}

HStringMap *h_stringmap_copy(HArena *a, const HStringMap *m)
{
HStringMap *res = h_stringmap_new(a);
h_stringmap_update(res, m);
return res;
}

/* Replace all occurances of old in m with new.
* If old is NULL, replace all values in m with new.
* If new is NULL, remove the respective values.
Expand Down
1 change: 1 addition & 0 deletions src/cfgrammar.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ typedef struct HStringMap_ {
} HStringMap;

HStringMap *h_stringmap_new(HArena *a);
HStringMap *h_stringmap_copy(HArena *a, const HStringMap *m);
void h_stringmap_put_end(HStringMap *m, void *v);
void h_stringmap_put_epsilon(HStringMap *m, void *v);
void h_stringmap_put_after(HStringMap *m, uint8_t c, HStringMap *ends);
Expand Down

0 comments on commit 70da67a

Please sign in to comment.