From ca2f6674749ed83e27ee04d2d8a10ec97f617f70 Mon Sep 17 00:00:00 2001 From: pd3 Date: Mon, 16 Dec 2019 11:57:55 +0100 Subject: [PATCH] Polish a few minor details - bug fix in `bcf_dec_typed_int1_safe()`, 64-bit values could not have been read correctly by this function - cosmetic change to declare val1 always as int64_t and explicit cast to int32_t and add explicit LL to a 64-bit constant in case some compilers were fussy (cherry picked from commit ecc69cf62cb1d08129c0a3f158081a299884a08f) --- Makefile | 3 ++- vcf.c | 20 ++++++++++---------- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/Makefile b/Makefile index 3a772d4e3..6bdd47166 100644 --- a/Makefile +++ b/Makefile @@ -30,7 +30,8 @@ RANLIB = ranlib htslib_default_libs = -lz -lm -lbz2 -llzma -lcurl CPPFLAGS = -# TODO: make the 64-bit support for VCF optional via configure, for now add -DVCF_ALLOW_INT64=1 to CFLAGS manually +# TODO: make the 64-bit support for VCF optional via configure, for now add -DVCF_ALLOW_INT64 +# to CFLAGS manually, here or in config.mk if the latter exists. # TODO: probably update cram code to make it compile cleanly with -Wc++-compat # For testing strict C99 support add -std=c99 -D_XOPEN_SOURCE=600 #CFLAGS = -g -Wall -O2 -pedantic -std=c99 -D_XOPEN_SOURCE=600 diff --git a/vcf.c b/vcf.c index d41a788e8..5df668fe3 100644 --- a/vcf.c +++ b/vcf.c @@ -72,8 +72,8 @@ static bcf_idinfo_t bcf_idinfo_def = { .info = { 15, 15, 15 }, .hrec = { NULL, N - experimental, use at your risk */ #ifdef VCF_ALLOW_INT64 - #define BCF_MAX_BT_INT64 (0x7fffffffffffffff) /* INT64_MAX, for internal use only */ - #define BCF_MIN_BT_INT64 -9223372036854775800 /* INT64_MIN + 8, for internal use only */ + #define BCF_MAX_BT_INT64 (0x7fffffffffffffff) /* INT64_MAX, for internal use only */ + #define BCF_MIN_BT_INT64 -9223372036854775800LL /* INT64_MIN + 8, for internal use only */ #endif #define BCF_IS_64BIT (1<<30) @@ -1269,9 +1269,11 @@ static int bcf_dec_typed_int1_safe(uint8_t *p, uint8_t *end, uint8_t **q, *val = le_to_i32(p); #ifdef VCF_ALLOW_INT64 } else if (t == BCF_BT_INT64) { - if (end - p < 4) return -1; - *q = p + 4; - *val = le_to_i32(p); + // This case should never happen because there should be no 64-bit BCFs + // at all, definitely not coming from htslib + if (end - p < 8) return -1; + *q = p + 8; + *val = le_to_i64(p); #endif } else { return -1; @@ -2726,9 +2728,9 @@ int vcf_parse(kstring_t *s, const bcf_hdr_t *h, bcf1_t *v) } if ((y>>4&0xf) == BCF_HT_INT) { i = 0, t = val; + int64_t val1; #ifdef VCF_ALLOW_INT64 int is_int64 = 0; - int64_t val1; if ( n_val==1 ) { errno = 0; @@ -2749,8 +2751,6 @@ int vcf_parse(kstring_t *s, const bcf_hdr_t *h, bcf1_t *v) t = te; i = 1; // this is just to avoid adding another nested block... } -#else - int32_t val1; #endif for (; i < n_val; ++i, ++t) { @@ -2777,10 +2777,10 @@ int vcf_parse(kstring_t *s, const bcf_hdr_t *h, bcf1_t *v) bcf_enc_long1(str, val1); } else - bcf_enc_int1(str, val1); + bcf_enc_int1(str, (int32_t)val1); #else val1 = val_a[0]; - bcf_enc_int1(str, val1); + bcf_enc_int1(str, (int32_t)val1); #endif } else { bcf_enc_vint(str, n_val, val_a, -1);