Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 21 additions & 13 deletions src/fstapi.c
Original file line number Diff line number Diff line change
Expand Up @@ -6136,7 +6136,7 @@ char *fstReaderGetValueFromHandleAtTime(fstReaderContext *xc,
fstHandle facidx,
char *buf)
{
fst_off_t blkpos = 0, prev_blkpos;
fst_off_t blkpos = 0;
uint64_t beg_tim, end_tim, beg_tim2, end_tim2;
int sectype;
#ifdef FST_DEBUG
Expand Down Expand Up @@ -6180,7 +6180,7 @@ char *fstReaderGetValueFromHandleAtTime(fstReaderContext *xc,
xc->rvat_chain_pos_valid = 0;

for (;;) {
fstReaderFseeko(xc, xc->f, (prev_blkpos = blkpos), SEEK_SET);
fstReaderFseeko(xc, xc->f, blkpos, SEEK_SET);

sectype = fgetc(xc->f);
seclen = fstReaderUint64(xc->f);
Expand All @@ -6202,23 +6202,31 @@ char *fstReaderGetValueFromHandleAtTime(fstReaderContext *xc,
if ((beg_tim <= tim) && (tim <= end_tim)) {
if ((tim == end_tim) && (tim != xc->end_time)) {
fst_off_t cached_pos = ftello(xc->f);
fstReaderFseeko(xc, xc->f, blkpos, SEEK_SET);
fst_off_t next_blkpos = blkpos + seclen;
int sectype2;
uint64_t seclen2;

fstReaderFseeko(xc, xc->f, next_blkpos, SEEK_SET);

sectype = fgetc(xc->f);
seclen = fstReaderUint64(xc->f);
sectype2 = fgetc(xc->f);
seclen2 = fstReaderUint64(xc->f);

beg_tim2 = fstReaderUint64(xc->f);
end_tim2 = fstReaderUint64(xc->f);

if (((sectype != FST_BL_VCDATA) && (sectype != FST_BL_VCDATA_DYN_ALIAS) &&
(sectype != FST_BL_VCDATA_DYN_ALIAS2)) ||
(!seclen) || (beg_tim2 != tim)) {
blkpos = prev_blkpos;
break;
if (((sectype2 != FST_BL_VCDATA) && (sectype2 != FST_BL_VCDATA_DYN_ALIAS) &&
(sectype2 != FST_BL_VCDATA_DYN_ALIAS2)) ||
(!seclen2) || (beg_tim2 != tim)) {
/* keep this section, and the read position within it */
fstReaderFseeko(xc, xc->f, cached_pos, SEEK_SET);
} else {
/* next section have the time, use it instead */
sectype = sectype2;
seclen = seclen2;
beg_tim = beg_tim2;
end_tim = end_tim2;
blkpos = next_blkpos + 1;
}
beg_tim = beg_tim2;
end_tim = end_tim2;
fstReaderFseeko(xc, xc->f, cached_pos, SEEK_SET);
}
break;
}
Expand Down
1 change: 1 addition & 0 deletions tests/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ libfst_tests = [
'empty_file',
'mask_no_variables',
'packed_extent_wraps',
'rvat_section_boundary',
'write_and_read',
]

Expand Down
54 changes: 54 additions & 0 deletions tests/rvat_section_boundary.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#include <fstapi.h>
#include <glib.h>

// Random access reads at the end time of a value change section: one where
// the next section begins at that same time, and one at the end of the last
// value change section, in a file that ends later.

int main(int argc, char **argv)
{
(void)argc;
(void)argv;

fstWriterContext *writer = fstWriterCreate("rvat_section_boundary.fst", 1);
g_assert_true(writer != NULL);

fstHandle var = fstWriterCreateVar(writer, FST_VT_VCD_WIRE, FST_VD_IMPLICIT, 1, "var", 0);

fstWriterEmitTimeChange(writer, 0);
fstWriterEmitValueChange(writer, var, "0");
fstWriterEmitTimeChange(writer, 1);
fstWriterEmitValueChange(writer, var, "1");
fstWriterEmitTimeChange(writer, 2);
fstWriterEmitValueChange(writer, var, "0");

// The section is flushed at the next time change: it ends at 2, and the
// next one begins at 2.
fstWriterFlushContext(writer);

fstWriterEmitTimeChange(writer, 3);
fstWriterEmitValueChange(writer, var, "1");
fstWriterEmitTimeChange(writer, 4);
fstWriterEmitValueChange(writer, var, "0");

// Same again, ending the second section at 4; the third one holds no value
// changes, so it is not written, but the file still ends at 5.
fstWriterFlushContext(writer);

fstWriterEmitTimeChange(writer, 5);

fstWriterClose(writer);

fstReaderContext *reader = fstReaderOpen("rvat_section_boundary.fst");
g_assert_true(reader != NULL);
g_assert_cmpint(fstReaderGetEndTime(reader), ==, 5);

char buf[64];

g_assert_cmpstr(fstReaderGetValueFromHandleAtTime(reader, 2, var, buf), ==, "0");
g_assert_cmpstr(fstReaderGetValueFromHandleAtTime(reader, 4, var, buf), ==, "0");

fstReaderClose(reader);

return 0;
}
Loading