-
Notifications
You must be signed in to change notification settings - Fork 67
Avoid 0-length copy with potentially undefined behavior #1968
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
I added the |
Added reference to a relevant r-devel thread (h/t |
It's rather annoying that this change ultimately means you can't call That's extremely annoying ergonomics wise. Ideally algorithmic code should be able to work with any size from void fn() {
R_xlen_t size = Rf_xlength(x);
const double* p_x = REAL(x);
for (R_xlen_t i = 0; i < size; ++i) {
double elt = p_x[i];
}
} Relevant commits: Which ultimately turn on here: We call |
Actually I guess calling So it's a matter of ensuring you don't use that invalid pointer anywhere. So, for example, the above loop is still fine, I guess. But
https://www.reddit.com/r/Cprog/comments/33p5ho/is_it_legal_to_call_memcpy_with_zero_length_on_a/ And I would have thought most implementations of So maybe a more holistic and ergonomic solution for rlang and vctrs is to introduce void* r_memcpy(void* dest, const void* src, size_t count) {
if (count) {
memcpy(dest, src, count);
}
} And just switch to that everywhere SEXP memory is involved. |
That works for me. Agree about the awkward ergonomics. I am still trying to track down exactly what it is about our setup that leads to the divergent behavior vs. CRAN. I'll report back if that ever bears fruit. Ivan mentioned |
Updating R 4.4.1 to R 4.5.0 broke {vctrs} suites pretty badly (segfaults).
To be a bit more specific, this test breaks, though there may be others:
vctrs/tests/testthat/test-cast.R
Line 302 in 78d9f2b
Stack trace hidden but available below.
r-devel thread:
https://stat.ethz.ch/pipermail/r-devel/2024-June/083456.html
In sum:
--enable-strict-barrier
the default, i.e., strictly speaking this affects all R versions-fsanitize-trap
) makes this segfault for us where CRAN does notstack trace...