Fixed bug in bcf1_sync. The main cause of the bug is as follows:
bcf_unpack(bcf_rec, BCF_UN_STR);
bcf_update_id(bcf_rec, id);
This causes bcf_rec->d.shared_dirty to be set.
duplicate = bcf_dup(bcf_rec);
This calls bcf1_sync(). Since bcf_rec->d.shared_dirty is set, the shared
data is re-created in tmp.s, bcf_rec->shared.s is freed and
bcf_rec->shared.s = tmp.s. However, bcf_rec->unpack_ptr and
bcf_rec->d.info[i].vptr still point to the old shared.s region which is
invalidated.
The fix is done by assigning bcf_rec->unpack_ptr and the vptr fields
after tmp.s is fully constructed in bcf1_sync. I use unpack_offset and
offset_array to track the offsets of these fields with respect to tmp.s
I have added a test case that exercises the bug in the directory
memcheck. Just type 'make' in the directory. It's a simple example.
The easiest way to see the bug is to run the program with valgrind. When
linked with the buggy htslib, you will see a number of illegal memory
accesses and illegal frees (possibly SEGFAULT also). In my environment,
the END field for the duplicate was modified to the same value as the
original! Your results may vary
valgrind --tool=memcheck ./memcheck s6.vcf.gz