Closed zaeleus closed 2 years ago
I think this was probably something that I tightened up in the spec with an explicit "not permitted to have nsym > 16 or nsym == 0" statement at a later stage.
The bug is really one of not knowing how best to deal with errors; eg failed to allocate memory (hard error) vs inappropriate data (soft error). I think I took the wrong choice in making it return a buffer in the latter case, as it's both wasteful of CPU cycles and also punts the error handling elsewhere. Specifically here: https://github.com/samtools/htscodecs/blob/843d4f63b1c64905881b4648916a4d027baa1a1c/htscodecs/rANS_static4x16pr.c#L1267 (which should check for 0 too, as stated in the specification).
However given the handling of the NULL case anyway, hts_pack
should probably just treat all error types the same. The caller can, if it chooses, always look at errno for ENOMEM to distinguish.
Thanks for the bug report.
Encoders that use
hts_pack
disable bit packing if there are more than 16 symbols, e.g., rANS Nx16 and the arithmetic range coder. However, the checks that disable bit packing compare against a potentially truncated value. When attempting to bit pack inputs with 256 symbols,out[c_meta_len]
is 0,out[c_meta_len] > 16
fails, and the encoders do not disable bit packing.An example using the rANS Nx16 test program (version 1.3.0):