Skip to content

Commit e65fa4c

Browse files
Fix missing checks on GT
1 parent e1b0b43 commit e65fa4c

File tree

2 files changed

+34
-21
lines changed

2 files changed

+34
-21
lines changed

lib/tests.c

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -280,13 +280,13 @@ test_variant_encoder_minimal(void)
280280
const int32_t hq_data[] = { 10, 15, 7, 12, -1, -1, -1, -1 };
281281
const float gl_data[] = { 1, 2, 3, 4, 1.1f, 1.2f, 1.3f, 1.4f };
282282
int64_t ret;
283-
size_t j;
283+
size_t j, buflen;
284284
vcz_variant_encoder_t writer;
285285
const char *expected[] = {
286286
"X\t123\tRS1\tA\tT\t9\tPASS\tAA=G\tGT:HQ:GL\t0/0:10,15:1,2\t0|1:7,12:3,4",
287287
"YY\t45678\tRS2\tG\t.\t12.1\tFILT1\tAN=9;FLAG\tGT:GL\t1|1:1.1,1.2\t1/0:1.3,1.4",
288288
};
289-
char buf[1000];
289+
char *buf;
290290

291291
ret = vcz_variant_encoder_init(&writer, 2, 2, contig_data, 2, pos_data, id_data, 3,
292292
1, ref_data, 1, alt_data, 1, 1, qual_data, filter_id_data, 5, 2, filter_data);
@@ -313,14 +313,27 @@ test_variant_encoder_minimal(void)
313313
/* vcz_variant_encoder_print_state(&writer, stdout); */
314314

315315
for (j = 0; j < num_rows; j++) {
316-
ret = vcz_variant_encoder_write_row(&writer, j, buf, 1000);
316+
for (buflen = 0;; buflen++) {
317+
/* printf("buflen = %d\n", (int) buflen); */
318+
buf = malloc(buflen);
319+
CU_ASSERT_FATAL(buf != NULL);
320+
ret = vcz_variant_encoder_write_row(&writer, j, buf, buflen);
321+
if (ret < 0) {
322+
free(buf);
323+
CU_ASSERT_EQUAL_FATAL(ret, VCZ_ERR_BUFFER_OVERFLOW);
324+
} else {
325+
break;
326+
}
327+
}
328+
CU_ASSERT_FATAL(buflen >= strlen(expected[j]));
317329
/* printf("ret = %d\n", (int) ret); */
318330
/* printf("GOT:%s\n", buf); */
319331
/* printf("EXP:%s\n", expected[j]); */
320332
/* printf("GOT:%d\n", (int) strlen(buf)); */
321333
/* printf("EXP:%d\n", (int) strlen(expected[j])); */
322334
CU_ASSERT_EQUAL(ret, strlen(expected[j]));
323335
CU_ASSERT_STRING_EQUAL(buf, expected[j]);
336+
free(buf);
324337
}
325338
vcz_variant_encoder_free(&writer);
326339
}

lib/vcf_encoder.c

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,8 @@ append_int(char *restrict buf, int32_t value, int64_t offset, int64_t buflen)
144144
}
145145

146146
static inline int64_t
147-
append_float(char *restrict buf, int32_t int32_value, float value, int64_t offset,
148-
int64_t buflen)
147+
append_float(
148+
char *restrict buf, int32_t int32_value, float value, int64_t offset, int64_t buflen)
149149
{
150150
if (int32_value == VCZ_FLOAT32_MISSING_AS_INT32) {
151151
return append_char(buf, '.', offset, buflen);
@@ -204,8 +204,8 @@ string_all_missing(const char *restrict data, size_t item_size, size_t n)
204204
}
205205

206206
static int64_t
207-
string_field_write_entry(const vcz_field_t *self, const void *data, char *buf,
208-
int64_t buflen, int64_t offset)
207+
string_field_write_entry(
208+
const vcz_field_t *self, const void *data, char *buf, int64_t buflen, int64_t offset)
209209
{
210210
const char *source = (const char *) data;
211211
size_t column, byte;
@@ -297,8 +297,8 @@ float32_field_write_entry(const vcz_field_t *self, const void *restrict data,
297297
}
298298

299299
static int64_t
300-
vcz_field_write_entry(const vcz_field_t *self, const void *data, char *buf,
301-
int64_t buflen, int64_t offset)
300+
vcz_field_write_entry(
301+
const vcz_field_t *self, const void *data, char *buf, int64_t buflen, int64_t offset)
302302
{
303303
if (self->type == VCZ_TYPE_INT) {
304304
if (self->item_size == 4) {
@@ -403,7 +403,7 @@ vcz_field_init(vcz_field_t *self, const char *name, int type, size_t item_size,
403403

404404
static int64_t
405405
vcz_variant_encoder_write_sample_gt(const vcz_variant_encoder_t *self, size_t variant,
406-
size_t sample, char *buf, int64_t VCZ_UNUSED(buflen), int64_t offset)
406+
size_t sample, char *buf, int64_t buflen, int64_t offset)
407407
{
408408
const size_t ploidy = self->gt.num_columns;
409409
size_t source_offset = variant * self->num_samples * ploidy + sample * ploidy;
@@ -420,18 +420,19 @@ vcz_variant_encoder_write_sample_gt(const vcz_variant_encoder_t *self, size_t va
420420

421421
for (ploid = 0; ploid < ploidy; ploid++) {
422422
value = source[ploid];
423-
if (value != VCZ_INT_FILL) {
424-
if (ploid > 0) {
425-
buf[offset] = sep;
426-
offset++;
427-
}
428-
if (value == VCZ_INT_MISSING) {
429-
buf[offset] = '.';
430-
offset++;
431-
} else {
432-
offset += vcz_itoa(buf + offset, value);
423+
if (value == VCZ_INT_FILL) {
424+
break;
425+
}
426+
if (ploid > 0) {
427+
offset = append_char(buf, sep, offset, buflen);
428+
if (offset < 0) {
429+
goto out;
433430
}
434431
}
432+
offset = append_int(buf, value, offset, buflen);
433+
if (offset < 0) {
434+
goto out;
435+
}
435436
}
436437
out:
437438
return offset;
@@ -543,7 +544,6 @@ vcz_variant_encoder_write_format_fields(const vcz_variant_encoder_t *self,
543544
}
544545
}
545546
} else {
546-
547547
if (!gt_missing) {
548548
offset = append_string(buf, "GT:", 3, offset, buflen);
549549
if (offset < 0) {

0 commit comments

Comments
 (0)