Skip to content

Commit b9a4578

Browse files
authored
Merge pull request #5 from gergness/fixes
Fix for segfaults with `read_list()`. Thanks @gergness! I confirmed that your changes fix #2 and #3 on my machine as well.
2 parents 4df69a7 + 5140f86 commit b9a4578

2 files changed

Lines changed: 33 additions & 1 deletion

File tree

src/read_full.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,10 @@ RObject read_list(
172172

173173
if (cur_pos_rt[rt_index] >= out[rt_index][0]->size()) {
174174
// Resize by guessing from the progress bar
175-
resizeAllColumns(out[rt_index], static_cast<int>((cur_pos_rt[rt_found] / data->progress_info().first) * 1.1));
175+
double resize_scale = 1.1 / data->progress_info().first;
176+
// But make sure you at least 1.5x size because it's better to overallocate
177+
resize_scale = std::max(1.5, resize_scale);
178+
resizeAllColumns(out[rt_index], static_cast<int>(cur_pos_rt[rt_index] * resize_scale));
176179
}
177180

178181
// Check if raw line is long enough

tests/testthat/test-list-full.R

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,32 @@ test_that("basic example matches long format", {
5555
)
5656
})
5757

58+
test_that("Can read rectangular into a list including with an allocation", {
59+
# Need to have at least 501 rows to guarantee rows are re-allocated
60+
temp_file <- tempfile()
61+
writeLines(
62+
paste0(
63+
rep(c("abcd", "efgh", "ijkl", "mnop", "qrst", "uvwx"), 100),
64+
collapse = "\n"
65+
),
66+
temp_file
67+
)
68+
actual <- hipread_list(
69+
temp_file,
70+
hip_fwf_widths(
71+
c(1, 2, 1),
72+
c("var1", "var2", "var3"),
73+
c("character", "character", "character")
74+
),
75+
hip_rt(1, 0)
76+
)
77+
78+
expect_true(is.list(actual))
79+
expect_equal(length(actual), 1)
80+
expect_equal(nrow(actual[[1]]), 600)
81+
expect_equal(ncol(actual[[1]]), 3)
82+
expect_equal(
83+
actual[[1]]$var1[1:7],
84+
c("a", "e", "i", "m", "q", "u", "a")
85+
)
86+
})

0 commit comments

Comments
 (0)