Skip to content

Commit 83eca9f

Browse files
Merge pull request #15 from jeromekelleher/buffer-safety
Add paranoia to build options and fix errors
2 parents 3382a02 + 1374f27 commit 83eca9f

File tree

4 files changed

+142
-130
lines changed

4 files changed

+142
-130
lines changed

lib/meson.build

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ project('vcf_encoder', ['c'],
44

55
cc = meson.get_compiler('c')
66
m_dep = cc.find_library('m', required: false)
7-
lib_deps = [m_dep]
87

98
extra_c_args = [
109
'-Wall', '-Wextra', '-Werror', '-Wpedantic', '-W',
@@ -21,4 +20,6 @@ cunit_dep = dependency('cunit')
2120
tests = executable('tests',
2221
sources: ['tests.c', 'vcf_encoder.c'],
2322
dependencies: [cunit_dep, m_dep],
24-
)
23+
c_args: extra_c_args,
24+
)
25+
test('tests', tests)

lib/tests.c

Lines changed: 52 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,16 @@ FILE *_devnull;
1111
static void
1212
test_int_field_1d(void)
1313
{
14-
const int num_rows = 5;
14+
const size_t num_rows = 5;
1515
const int32_t data[] = { 1, 2, 12345789, -1, -100 };
1616
vcz_field_t field = { .name = "test",
1717
.type = VCZ_TYPE_INT,
1818
.item_size = 4,
1919
.num_columns = 1,
20-
.data = data };
20+
.data = (const char *) data };
2121
char buf[1000];
2222
const char *expected[] = { "1\t", "2\t", "12345789\t", ".\t", "-100\t" };
23-
int ret;
23+
int64_t ret;
2424
size_t j;
2525

2626
for (j = 0; j < num_rows; j++) {
@@ -34,16 +34,16 @@ test_int_field_1d(void)
3434
static void
3535
test_int_field_2d(void)
3636
{
37-
const int num_rows = 4;
37+
const size_t num_rows = 4;
3838
const int32_t data[] = { 1, 2, 3, 1234, 5678, -2, -1, -2, -2, -2, -2, -2 };
3939
vcz_field_t field = { .name = "test",
4040
.type = VCZ_TYPE_INT,
4141
.item_size = 4,
4242
.num_columns = 3,
43-
.data = data };
43+
.data = (const char *) data };
4444
char buf[1000];
4545
const char *expected[] = { "1,2,3\t", "1234,5678\t", ".\t", "\t" };
46-
int ret;
46+
int64_t ret;
4747
size_t j;
4848

4949
for (j = 0; j < num_rows; j++) {
@@ -58,7 +58,7 @@ static void
5858
test_string_field_1d(void)
5959
{
6060
/* item_size=3, rows=3, cols=1 */
61-
const int num_rows = 3;
61+
const size_t num_rows = 3;
6262
const char data[] = "X\0\0" /* X */
6363
"XX\0" /* XX*/
6464
"XXX"; /* XXX, */
@@ -70,7 +70,7 @@ test_string_field_1d(void)
7070
.data = data };
7171
char buf[1000];
7272
const char *expected[] = { "X\t", "XX\t", "XXX\t" };
73-
int ret;
73+
int64_t ret;
7474
size_t j;
7575

7676
CU_ASSERT_EQUAL_FATAL(sizeof(data), 10);
@@ -85,7 +85,7 @@ static void
8585
test_string_field_2d(void)
8686
{
8787
/* item_size=3, rows=3, cols=3 */
88-
const int num_rows = 3;
88+
const size_t num_rows = 3;
8989
const char data[] = "X\0\0Y\0\0\0\0\0" /* [X, Y] */
9090
"XX\0YY\0Z\0\0" /* [XX, YY, Z], */
9191
"XXX\0\0\0\0\0"; /* [XXX], */
@@ -97,7 +97,7 @@ test_string_field_2d(void)
9797
.data = data };
9898
char buf[1000];
9999
const char *expected[] = { "X,Y\t", "XX,YY,Z\t", "XXX\t" };
100-
int ret;
100+
int64_t ret;
101101
size_t j;
102102

103103
CU_ASSERT_EQUAL_FATAL(sizeof(data), 27);
@@ -113,22 +113,23 @@ static void
113113
test_variant_encoder_minimal(void)
114114
{
115115
// Two rows, one column in each field, two samples
116-
const int num_rows = 2;
116+
const size_t num_rows = 2;
117117
const char contig_data[] = "X\0YY";
118118
const int32_t pos_data[] = { 123, 45678 };
119119
const char id_data[] = "RS1RS2";
120120
const char ref_data[] = "AG";
121121
const char alt_data[] = "T";
122-
const float qual_data[] = { 9, 12.1 };
122+
const float qual_data[] = { 9, 12.1f };
123123
const char filter_id_data[] = "PASS\0FILT1";
124-
const int8_t filter_data[] = {1, 0, 0, 1};
124+
const int8_t filter_data[] = { 1, 0, 0, 1 };
125125
const int32_t an_data[] = { -1, 9 };
126-
const char* aa_data = "G.";
127-
const int8_t flag_data[] = {0, 1};
126+
const char *aa_data = "G.";
127+
const int8_t flag_data[] = { 0, 1 };
128128
const int32_t gt_data[] = { 0, 0, 0, 1, 1, 1, 1, 0 };
129129
const int8_t gt_phased_data[] = { 0, 1, 1, 0 };
130-
const int32_t hq_data[] = { 10, 15, 7, 12, -1, -1, -1, -1};
131-
int ret, j;
130+
const int32_t hq_data[] = { 10, 15, 7, 12, -1, -1, -1, -1 };
131+
int64_t ret;
132+
size_t j;
132133
vcz_variant_encoder_t writer;
133134
const char *expected[] = {
134135
"X\t123\tRS1\tA\tT\t9\tPASS\tAA=G\tGT:HQ\t0/0:10,15\t0|1:7,12",
@@ -137,30 +138,31 @@ test_variant_encoder_minimal(void)
137138
char buf[1000];
138139

139140
ret = vcz_variant_encoder_init(&writer, 2, 2, contig_data, 2, pos_data, id_data, 3,
140-
1, ref_data, 1, alt_data, 1, 1, qual_data,
141-
filter_id_data, 5, 2, filter_data);
141+
1, ref_data, 1, alt_data, 1, 1, qual_data, filter_id_data, 5, 2, filter_data);
142142
CU_ASSERT_EQUAL_FATAL(ret, 0);
143143
ret = vcz_variant_encoder_add_gt_field(&writer, gt_data, 4, 2, gt_phased_data);
144144
CU_ASSERT_EQUAL_FATAL(ret, 0);
145145
ret = vcz_variant_encoder_add_info_field(&writer, "AN", VCZ_TYPE_INT, 4, 1, an_data);
146146
CU_ASSERT_EQUAL_FATAL(ret, 0);
147-
ret = vcz_variant_encoder_add_info_field(&writer, "AA", VCZ_TYPE_STRING, 1, 1, aa_data);
147+
ret = vcz_variant_encoder_add_info_field(
148+
&writer, "AA", VCZ_TYPE_STRING, 1, 1, aa_data);
148149
CU_ASSERT_EQUAL_FATAL(ret, 0);
149-
ret = vcz_variant_encoder_add_info_field(&writer, "FLAG", VCZ_TYPE_BOOL, 1, 1, flag_data);
150+
ret = vcz_variant_encoder_add_info_field(
151+
&writer, "FLAG", VCZ_TYPE_BOOL, 1, 1, flag_data);
150152
CU_ASSERT_EQUAL_FATAL(ret, 0);
151153
ret = vcz_variant_encoder_add_format_field(
152154
&writer, "HQ", VCZ_TYPE_INT, 4, 2, hq_data);
153155
CU_ASSERT_EQUAL_FATAL(ret, 0);
154156

155157
vcz_variant_encoder_print_state(&writer, _devnull);
156-
printf("\n");
158+
/* printf("\n"); */
157159
/* vcz_variant_encoder_print_state(&writer, stdout); */
158160

159161
for (j = 0; j < num_rows; j++) {
160162
ret = vcz_variant_encoder_write_row(&writer, j, buf, 1000);
161163
/* printf("ret = %d\n", ret); */
162-
printf("GOT:%s\n", buf);
163-
printf("EXP:%s\n", expected[j]);
164+
/* printf("GOT:%s\n", buf); */
165+
/* printf("EXP:%s\n", expected[j]); */
164166
/* printf("GOT:%d\n", (int) strlen(buf)); */
165167
/* printf("EXP:%d\n", (int) strlen(expected[j])); */
166168
CU_ASSERT_EQUAL(ret, strlen(expected[j]));
@@ -186,37 +188,37 @@ test_itoa_small(void)
186188
}
187189
}
188190

189-
190191
static void
191192
test_ftoa(void)
192193
{
193194
struct test_case {
194195
float val;
195196
const char *expected;
196197
};
198+
// clang-format off
197199
struct test_case cases[] = {
198-
{0.0, "0"},
199-
{0.0001, "0"},
200-
{0.0005, "0.001"},
201-
{0.3, "0.3"},
202-
{0.32, "0.32"},
203-
{0.329, "0.329"},
204-
{0.3217, "0.322"},
205-
{8.0, "8"},
206-
{8.0001, "8"},
207-
{8.3, "8.3"},
208-
{8.32, "8.32"},
209-
{8.329, "8.329"},
210-
{8.3217, "8.322"},
211-
{443.998, "443.998"},
212-
{1028.0, "1028"},
213-
{1028.0001, "1028"},
214-
{1028.3, "1028.3"},
215-
{1028.32, "1028.32"},
216-
{1028.329, "1028.329"},
217-
{1028.3217, "1028.322"},
200+
{0.0f, "0"},
201+
{0.0001f, "0"},
202+
{0.0005f, "0.001"},
203+
{0.3f, "0.3"},
204+
{0.32f, "0.32"},
205+
{0.329f, "0.329"},
206+
{0.3217f, "0.322"},
207+
{8.0f, "8"},
208+
{8.0001f, "8"},
209+
{8.3f, "8.3"},
210+
{8.32f, "8.32"},
211+
{8.329f, "8.329"},
212+
{8.3217f, "8.322"},
213+
{443.998f, "443.998"},
214+
{1028.0f, "1028"},
215+
{1028.0001f, "1028"},
216+
{1028.3f, "1028.3"},
217+
{1028.32f, "1028.32"},
218+
{1028.329f, "1028.329"},
219+
{1028.3217f, "1028.322"},
218220
{1000000, "1000000"},
219-
{-100.0, "-100"},
221+
{-100.0f, "-100"},
220222
{NAN, "nan"},
221223
{INFINITY, "inf"},
222224
{-INFINITY, "-inf"},
@@ -225,7 +227,9 @@ test_ftoa(void)
225227
{-16777216, "-16777216"},
226228
/* TODO test extreme value here, that push against the limits of f32 */
227229
};
228-
int j, len;
230+
// clang-format on
231+
int len;
232+
size_t j;
229233
char buf[1024];
230234

231235
for (j = 0; j < sizeof(cases) / sizeof(*cases); j++) {
@@ -267,7 +271,7 @@ handle_cunit_error(void)
267271
exit(EXIT_FAILURE);
268272
}
269273

270-
int
274+
static int
271275
test_main(CU_TestInfo *tests, int argc, char **argv)
272276
{
273277
int ret;

0 commit comments

Comments
 (0)