--- a/src/index.c +++ b/src/index.c @@ -875,15 +875,30 @@ @return Entry string or null if malloc failed */ char * mobi_get_cncx_string(const MOBIPdbRecord *cncx_record, const uint32_t cncx_offset) { - /* TODO: handle multiple cncx records */ + /* Tabmorph 2026: high16 selects a CNCX record, low16 its byte offset. + Reject missing records and truncated strings rather than silently reading + the first entry again after an out-of-range buffer seek. LGPL-3.0+. */ + uint32_t record_number = cncx_offset >> 16; + while (record_number-- && cncx_record) { + cncx_record = cncx_record->next; + } + const size_t record_offset = cncx_offset & 0xffff; + if (cncx_record == NULL || cncx_record->data == NULL || + record_offset >= cncx_record->size) { + return NULL; + } MOBIBuffer *buf = mobi_buffer_init_null(cncx_record->data, cncx_record->size); if (buf == NULL) { debug_print("%s\n", "Memory allocation failed"); return NULL; } - mobi_buffer_setpos(buf, cncx_offset); + mobi_buffer_setpos(buf, record_offset); size_t len = 0; const uint32_t string_length = mobi_buffer_get_varlen(buf, &len); + if (buf->error != MOBI_SUCCESS || string_length > buf->maxlen - buf->offset) { + mobi_buffer_free_null(buf); + return NULL; + } char *string = malloc(string_length + 1); if (string) { mobi_buffer_getstring(string, buf, string_length);