Skip to content

Commit

Permalink
Add an assertion if particular indices in an array/struct are duplica…
Browse files Browse the repository at this point in the history
…tes.

Fix struct/array index lookup if the previous entry used a designated
initializer, but this particular entry didn't. In that case, our index
is the previous member's index + 1, but we used to use the counter as
index, which is wrong.

Also fix the unit test, which contained code triggering both problems,
which ended up going in an infinite loop.
  • Loading branch information
rbultje committed Dec 19, 2012
1 parent eb79e74 commit 47826cc
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
22 changes: 20 additions & 2 deletions convert.c
Original file line number Diff line number Diff line change
Expand Up @@ -1233,6 +1233,17 @@ static char *find_variable_name(CursorRecursion *rec)
abort();
}

static int index_is_unique(StructArrayList *l, int idx) {
int n;

for (n = 0; n < l->n_entries; n++) {
if (l->entries[n].index == idx)
return 0;
}

return 1;
}

static enum CXChildVisitResult callback(CXCursor cursor, CXCursor parent,
CXClientData client_data)
{
Expand Down Expand Up @@ -1492,7 +1503,9 @@ static enum CXChildVisitResult callback(CXCursor cursor, CXCursor parent,
sai->value_offset.end = e;
sai->expression_offset.start = s;
sai->expression_offset.end = e;
sai->index = rec.parent->child_cntr - 1;
sai->index = parent->n_entries > 0 ?
parent->entries[parent->n_entries - 1].index + 1 :
rec.parent->child_cntr - 1;
parent_idx = parent - struct_array_lists;
}
}
Expand Down Expand Up @@ -1580,6 +1593,8 @@ static enum CXChildVisitResult callback(CXCursor cursor, CXCursor parent,
sai->value_offset.end = get_token_offset(tokens[n_tokens - 2]);
rec.data.sal_idx = rec.parent->data.sal_idx;
clang_visitChildren(cursor, callback, &rec);
assert(index_is_unique(&struct_array_lists[rec.parent->data.sal_idx],
sai->index));
struct_array_lists[rec.parent->data.sal_idx].n_entries++;
clang_disposeString(spelling);
clang_disposeString(spelling2);
Expand Down Expand Up @@ -1681,7 +1696,10 @@ static enum CXChildVisitResult callback(CXCursor cursor, CXCursor parent,
sai->value_offset.end = s;
sai->expression_offset.start = s;
sai->expression_offset.end = s;
sai->index = rec.parent->child_cntr - 1;
sai->index = parent->n_entries > 0 ?
parent->entries[parent->n_entries - 1].index + 1 :
rec.parent->child_cntr - 1;
assert(index_is_unique(parent, sai->index));
parent->n_entries++;
}
}
Expand Down
2 changes: 2 additions & 0 deletions unit2.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ typedef struct PixFmtInfo {
enum PixelFormat {
PIX_FMT_YUV420P,
PIX_FMT_YUYV422,
PIX_FMT_DUMMY3,
PIX_FMT_YUVJ420P,
PIX_FMT_RGB24,
PIX_FMT_RGB555,
PIX_FMT_RGBA,
PIX_FMT_DUMMY,
PIX_FMT_DUMMY2,
Expand Down

0 comments on commit 47826cc

Please sign in to comment.