Skip to content

Commit a0969f4

Browse files
committed
Fix a missing bgzf->uncompressed_address incr in bgzf_read_small
This bug crept in with #1772 which was added since last release, so there is no regression. Fixes #1798 with thanks to John Marshall
1 parent 61b922b commit a0969f4

File tree

2 files changed

+52
-3
lines changed

2 files changed

+52
-3
lines changed

htslib/bgzf.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ static inline ssize_t bgzf_read_small(BGZF *fp, void *data, size_t length) {
157157
(uint8_t *)fp->uncompressed_block + fp->block_offset,
158158
length);
159159
fp->block_offset += length;
160+
fp->uncompressed_address += length;
160161
return length;
161162
} else {
162163
return bgzf_read(fp, data, length);
@@ -179,7 +180,8 @@ static inline ssize_t bgzf_read_small(BGZF *fp, void *data, size_t length) {
179180
* bgzf_write optimised for small quantities, as a static inline
180181
* See bgzf_write() normal function for return values.
181182
*/
182-
static inline ssize_t bgzf_write_small(BGZF *fp, void *data, size_t length) {
183+
static inline
184+
ssize_t bgzf_write_small(BGZF *fp, const void *data, size_t length) {
183185
if (fp->is_compressed && BGZF_BLOCK_SIZE - fp->block_offset > length) {
184186
// Short cut the common and easy mode
185187
memcpy((uint8_t *)fp->uncompressed_block + fp->block_offset,

test/test_bgzf.c

+49-2
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ static int try_bgzf_close(BGZF **bgz, const char *name, const char *func, int ex
179179

180180
static ssize_t try_bgzf_read(BGZF *fp, void *data, size_t length,
181181
const char *name, const char *func) {
182-
ssize_t got = bgzf_read(fp, data, length);
182+
ssize_t got = bgzf_read_small(fp, data, length);
183183
if (got < 0) {
184184
fprintf(stderr, "%s : Error from bgzf_read %s : %s\n",
185185
func, name, strerror(errno));
@@ -189,7 +189,7 @@ static ssize_t try_bgzf_read(BGZF *fp, void *data, size_t length,
189189

190190
static ssize_t try_bgzf_write(BGZF *fp, const void *data, size_t length,
191191
const char *name, const char *func) {
192-
ssize_t put = bgzf_write(fp, data, length);
192+
ssize_t put = bgzf_write_small(fp, data, length);
193193
if (put < (ssize_t) length) {
194194
fprintf(stderr, "%s : %s %s : %s\n",
195195
func, put < 0 ? "Error writing to" : "Short write on",
@@ -878,6 +878,49 @@ static int test_tell_read(Files *f, const char *mode) {
878878
return -1;
879879
}
880880

881+
static int test_useek_read_small(Files *f, const char *mode) {
882+
883+
BGZF* bgz = NULL;
884+
char bg_buf[99];
885+
886+
bgz = try_bgzf_open(f->tmp_bgzf, mode, __func__);
887+
if (!bgz) goto fail;
888+
889+
890+
if (try_bgzf_write(bgz, "#>Hello, World!\n", 16,
891+
f->tmp_bgzf, __func__) != 16)
892+
goto fail;
893+
if (try_bgzf_close(&bgz, f->tmp_bgzf, __func__, 0) != 0) goto fail;
894+
895+
bgz = try_bgzf_open(f->tmp_bgzf, "r", __func__);
896+
if (!bgz) goto fail;
897+
898+
if (try_bgzf_getc(bgz, 0, '#', f->tmp_bgzf, __func__) < 0 ||
899+
try_bgzf_getc(bgz, 1, '>', f->tmp_bgzf, __func__) < 0)
900+
goto fail;
901+
902+
if (try_bgzf_read(bgz, bg_buf, 5, f->tmp_bgzf, __func__) != 5)
903+
goto fail;
904+
if (memcmp(bg_buf, "Hello", 5) != 0)
905+
goto fail;
906+
907+
if (try_bgzf_useek(bgz, 9, SEEK_SET, f->tmp_bgzf, __func__) < 0)
908+
goto fail;
909+
910+
if (try_bgzf_read(bgz, bg_buf, 5, f->tmp_bgzf, __func__) != 5)
911+
goto fail;
912+
if (memcmp(bg_buf, "World", 5) != 0)
913+
goto fail;
914+
915+
if (try_bgzf_close(&bgz, f->tmp_bgzf, __func__, 0) != 0) goto fail;
916+
return 0;
917+
918+
fail:
919+
fprintf(stderr, "%s: failed\n", __func__);
920+
if (bgz) bgzf_close(bgz);
921+
return -1;
922+
}
923+
881924
static int test_bgzf_getline(Files *f, const char *mode, int nthreads) {
882925
BGZF* bgz = NULL;
883926
ssize_t bg_put;
@@ -1098,6 +1141,10 @@ int main(int argc, char **argv) {
10981141
if (test_tell_read(&f, "w") != 0) goto out;
10991142
if (test_tell_read(&f, "wu") != 0) goto out;
11001143

1144+
// bgzf_useek and bgzf_read_small
1145+
if (test_useek_read_small(&f, "w") != 0) goto out;
1146+
if (test_useek_read_small(&f, "wu") != 0) goto out;
1147+
11011148
// getline
11021149
if (test_bgzf_getline(&f, "w", 0) != 0) goto out;
11031150
if (test_bgzf_getline(&f, "w", 1) != 0) goto out;

0 commit comments

Comments
 (0)