From 08ffef072df82ad6b622a5dce0e55ff591ff7441 Mon Sep 17 00:00:00 2001 From: LibretroAdmin Date: Sat, 1 Aug 2026 08:30:25 +0000 Subject: [PATCH] libspectrum: avoid log(0) when a TZX 0x19 data alphabet is empty bits_per_symbol for a Generalised Data Block (TZX ID 0x19) was computed as ceil( log( symbols_in_table ) / M_LN2 ) without guarding symbols_in_table == 0, so an empty data alphabet evaluated log(0) (undefined) and cast the resulting -inf to int. Compute 0 bits directly in that case; an empty alphabet carries no data symbols, so downstream data_count is 0 and the existing bounds check is unaffected. The one-symbol case still yields 0 bits, matching the TZX specification. --- libspectrum/tzx_read.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/libspectrum/tzx_read.c b/libspectrum/tzx_read.c index 8e8b241..962df82 100644 --- a/libspectrum/tzx_read.c +++ b/libspectrum/tzx_read.c @@ -635,7 +635,11 @@ tzx_read_generalised_data( libspectrum_tape *tape, symbol_count2 = libspectrum_tape_generalised_data_symbol_table_symbols_in_table( table ); - bits_per_symbol = ceil( log( symbol_count2 ) / M_LN2 ); + /* ceil( log2( n ) ) bits are needed to select one of n symbols. Guard + against log( 0 ), which is undefined; an empty alphabet needs no bits + (and, per the symbol-table parameter parsing, implies no data symbols). */ + bits_per_symbol = symbol_count2 ? + (int) ceil( log( symbol_count2 ) / M_LN2 ) : 0; libspectrum_tape_block_set_bits_per_data_symbol( block, bits_per_symbol );