Skip to content

Commit

Permalink
Merge pull request #158 from jeromekelleher/c_0.99.2_fixes
Browse files Browse the repository at this point in the history
C 0.99.2 fixes
  • Loading branch information
jeromekelleher authored Mar 27, 2019
2 parents 02e2421 + 5d68315 commit ed2cc1f
Show file tree
Hide file tree
Showing 6 changed files with 209 additions and 4 deletions.
9 changes: 9 additions & 0 deletions c/CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
---------------------
[0.99.2] - 2019-03-27
---------------------

Bugfix release. Changes:

- Fix incorrect errors on tbl_collection_dump (#132)
- Catch table overflows (#157)

---------------------
[0.99.1] - 2019-01-24
---------------------
Expand Down
101 changes: 101 additions & 0 deletions c/tests/test_tables.c
Original file line number Diff line number Diff line change
Expand Up @@ -274,12 +274,17 @@ test_table_collection_dump_errors(void)

ret = tsk_table_collection_init(&tables, 0);
CU_ASSERT_EQUAL_FATAL(ret, 0);
tables.sequence_length = 1.0;

ret = tsk_table_collection_dump(&tables, "/", 0);
CU_ASSERT_TRUE(tsk_is_kas_error(ret));
CU_ASSERT_EQUAL_FATAL(ret ^ (1 << TSK_KAS_ERR_BIT), KAS_ERR_IO);
str = tsk_strerror(ret);
CU_ASSERT_TRUE(strlen(str) > 0);

/* We'd like to catch close errors also, but it's hard to provoke them
* without intercepting calls to fclose() */

tsk_table_collection_free(&tables);
}
static void
Expand Down Expand Up @@ -2098,6 +2103,99 @@ test_load_reindex(void)
tsk_treeseq_free(&ts);
}

static void
test_table_overflow(void)
{
int ret;
tsk_table_collection_t tables;
tsk_size_t max_rows = ((tsk_size_t) INT32_MAX) + 1;

ret = tsk_table_collection_init(&tables, 0);
CU_ASSERT_EQUAL_FATAL(ret, 0);

/* Simulate overflows */
tables.individuals.max_rows = max_rows;
tables.individuals.num_rows = max_rows;
ret = tsk_individual_table_add_row(&tables.individuals, 0, 0, 0, NULL, 0);
CU_ASSERT_EQUAL_FATAL(ret, TSK_ERR_TABLE_OVERFLOW);

tables.nodes.max_rows = max_rows;
tables.nodes.num_rows = max_rows;
ret = tsk_node_table_add_row(&tables.nodes, 0, 0, 0, 0, NULL, 0);
CU_ASSERT_EQUAL_FATAL(ret, TSK_ERR_TABLE_OVERFLOW);

tables.edges.max_rows = max_rows;
tables.edges.num_rows = max_rows;
ret = tsk_edge_table_add_row(&tables.edges, 0, 0, 0, 0);
CU_ASSERT_EQUAL_FATAL(ret, TSK_ERR_TABLE_OVERFLOW);

tables.migrations.max_rows = max_rows;
tables.migrations.num_rows = max_rows;
ret = tsk_migration_table_add_row(&tables.migrations, 0, 0, 0, 0, 0, 0);
CU_ASSERT_EQUAL_FATAL(ret, TSK_ERR_TABLE_OVERFLOW);

tables.sites.max_rows = max_rows;
tables.sites.num_rows = max_rows;
ret = tsk_site_table_add_row(&tables.sites, 0, 0, 0, 0, 0);
CU_ASSERT_EQUAL_FATAL(ret, TSK_ERR_TABLE_OVERFLOW);

tables.mutations.max_rows = max_rows;
tables.mutations.num_rows = max_rows;
ret = tsk_mutation_table_add_row(&tables.mutations, 0, 0, 0, 0, 0, 0, 0);
CU_ASSERT_EQUAL_FATAL(ret, TSK_ERR_TABLE_OVERFLOW);

tables.provenances.max_rows = max_rows;
tables.provenances.num_rows = max_rows;
ret = tsk_provenance_table_add_row(&tables.provenances, 0, 0, 0, 0);
CU_ASSERT_EQUAL_FATAL(ret, TSK_ERR_TABLE_OVERFLOW);

tables.populations.max_rows = max_rows;
tables.populations.num_rows = max_rows;
ret = tsk_population_table_add_row(&tables.populations, 0, 0);
CU_ASSERT_EQUAL_FATAL(ret, TSK_ERR_TABLE_OVERFLOW);

tsk_table_collection_free(&tables);
}

static void
test_column_overflow(void)
{
int ret;
tsk_table_collection_t tables;
tsk_size_t too_big = ((tsk_size_t) INT32_MAX) + 2;

ret = tsk_table_collection_init(&tables, 0);
CU_ASSERT_EQUAL_FATAL(ret, 0);

ret = tsk_individual_table_add_row(&tables.individuals, 0, NULL, too_big, NULL, 0);
CU_ASSERT_EQUAL_FATAL(ret, TSK_ERR_COLUMN_OVERFLOW);
ret = tsk_individual_table_add_row(&tables.individuals, 0, NULL, 0, NULL, too_big);
CU_ASSERT_EQUAL_FATAL(ret, TSK_ERR_COLUMN_OVERFLOW);

ret = tsk_node_table_add_row(&tables.nodes, 0, 0, 0, 0, NULL, too_big);
CU_ASSERT_EQUAL_FATAL(ret, TSK_ERR_COLUMN_OVERFLOW);

ret = tsk_site_table_add_row(&tables.sites, 0, NULL, too_big, NULL, 0);
CU_ASSERT_EQUAL_FATAL(ret, TSK_ERR_COLUMN_OVERFLOW);
ret = tsk_site_table_add_row(&tables.sites, 0, NULL, 0, NULL, too_big);
CU_ASSERT_EQUAL_FATAL(ret, TSK_ERR_COLUMN_OVERFLOW);

ret = tsk_mutation_table_add_row(&tables.mutations, 0, 0, 0, NULL, 0, NULL, too_big);
CU_ASSERT_EQUAL_FATAL(ret, TSK_ERR_COLUMN_OVERFLOW);
ret = tsk_mutation_table_add_row(&tables.mutations, 0, 0, 0, NULL, too_big, NULL, 0);
CU_ASSERT_EQUAL_FATAL(ret, TSK_ERR_COLUMN_OVERFLOW);

ret = tsk_provenance_table_add_row(&tables.provenances, NULL, too_big, NULL, 0);
CU_ASSERT_EQUAL_FATAL(ret, TSK_ERR_COLUMN_OVERFLOW);
ret = tsk_provenance_table_add_row(&tables.provenances, NULL, 0, NULL, too_big);
CU_ASSERT_EQUAL_FATAL(ret, TSK_ERR_COLUMN_OVERFLOW);

ret = tsk_population_table_add_row(&tables.populations, NULL, too_big);
CU_ASSERT_EQUAL_FATAL(ret, TSK_ERR_COLUMN_OVERFLOW);

tsk_table_collection_free(&tables);
}

int
main(int argc, char **argv)
{
Expand All @@ -2124,8 +2222,11 @@ main(int argc, char **argv)
{"test_dump_load_empty", test_dump_load_empty},
{"test_dump_load_unsorted", test_dump_load_unsorted},
{"test_load_reindex", test_load_reindex},
{"test_table_overflow", test_table_overflow},
{"test_column_overflow", test_column_overflow},
{NULL, NULL},
};


return test_main(tests, argc, argv);
}
6 changes: 6 additions & 0 deletions c/tskit/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,12 @@ tsk_strerror_internal(int err)
case TSK_ERR_TABLES_NOT_INDEXED:
ret = "Table collection must be indexed";
break;
case TSK_ERR_TABLE_OVERFLOW:
ret = "Table too large; cannot allocate more than 2**31 rows.";
break;
case TSK_ERR_COLUMN_OVERFLOW:
ret = "Table column too large; cannot be more than 2**31 bytes.";
break;

/* Limitations */
case TSK_ERR_ONLY_INFINITE_SITES:
Expand Down
4 changes: 3 additions & 1 deletion c/tskit/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ to the API or ABI are introduced, i.e., the addition of a new function.
The library patch version. Incremented when any changes not relevant to the
to the API or ABI are introduced, i.e., internal refactors of bugfixes.
*/
#define TSK_VERSION_PATCH 1
#define TSK_VERSION_PATCH 2
/** @} */

/* Node flags */
Expand Down Expand Up @@ -189,6 +189,8 @@ of tskit.
#define TSK_ERR_BAD_TABLE_POSITION -700
#define TSK_ERR_BAD_SEQUENCE_LENGTH -701
#define TSK_ERR_TABLES_NOT_INDEXED -702
#define TSK_ERR_TABLE_OVERFLOW -703
#define TSK_ERR_COLUMN_OVERFLOW -704

/* Limitations */
#define TSK_ERR_ONLY_INFINITE_SITES -800
Expand Down
Loading

0 comments on commit ed2cc1f

Please sign in to comment.