summaryrefslogtreecommitdiff
path: root/module/scheme/write.scm
diff options
context:
space:
mode:
authorOlivier Dion <olivier-dion@proton.me>2023-04-08 16:48:01 -0400
committerLudovic Courtès <ludo@gnu.org>2023-07-16 22:16:30 +0200
commit84bf84032208e21d20ad13f3033d4fca3a512014 (patch)
tree0c67bd8cc985e9c1330392f98ad2aff505619490 /module/scheme/write.scm
parentc7d170c5d1e90de64962d4cf3cb257364f77b08f (diff)
downloadguile-84bf84032208e21d20ad13f3033d4fca3a512014.tar.gz
Fix dangling pointers in `environ'.
When calling `environ', Guile set the global variable `environ' to a list allocated with the GC. Strings in it are also allocated with the GC. However, if an user call the Scheme setenv() procedure, the resulting call to putenv() in libc might reallocate `environ' to a new pointer while copying sub-pointers owned by Guile in it. This results in the GC marking these strings for reclamation when they are actually still present in `environ'. Thus, the values in the environment are now undefined. To fix this, Guile should only manipulate the `environ' using the standard libc functions. This ensures that concurrent modification of it is safe in multi-threaded program. Therefore, the procedure `environ' now call the libc clearenv() procedure to purge the environment. Then, the desired values are put in `environ' using scm_putenv(). At the end, no GC allocated memory is put in `environ'. Also, since `environ' can be changed at anytime in a multi-thread program, emit a warning stipulating that the result is undefined behavior if multiple threads are created in the program. Consider for example a thread iterating over `environ' while another one do a call to putenv(). The latter would do a realloc() on `environ' and thus the old array read by the former now contains garbage. On system where clearenv() is not present, an atomic store of NULL with sequential consistency to `environ' should be sufficient but see the NOTES of clearenv(3). * libguile/posix.c (scm_environ): Do not store GC allocated memory in environ. Signed-off-by: Ludovic Courtès <ludo@gnu.org>
Diffstat (limited to 'module/scheme/write.scm')
0 files changed, 0 insertions, 0 deletions