diff options
author | Kaz Kylheku <kaz@kylheku.com> | 2021-09-25 13:09:01 -0700 |
---|---|---|
committer | Kaz Kylheku <kaz@kylheku.com> | 2021-09-25 13:09:01 -0700 |
commit | ba75167d1e7c609b829978b3f4a748ce3cf69ad9 (patch) | |
tree | d7f4342eec79bd472ac0b0409346c679946f0442 /lib.c | |
parent | d95be98dd8b2feaffc296a19f9a6e59f76133568 (diff) | |
download | txr-ba75167d1e7c609b829978b3f4a748ce3cf69ad9.tar.gz txr-ba75167d1e7c609b829978b3f4a748ce3cf69ad9.tar.bz2 txr-ba75167d1e7c609b829978b3f4a748ce3cf69ad9.zip |
New function: replace-env.
Using this new function together with env, it's now possible
to save the set of environment variables, clobber it to a
specified set (possibly empty) and then restore it.
Useful for improved security in running child processes.
* lib.[ch] (chk_substrdup_utf8): New function.
* sysif.c (replace_env): New function.
(sysif_init): Register replace-env intrinsic.
* sysif.h (replace_env): Declared.
* txr.1: Documented.
* stdlib/doc-syms.tl: Updated.
Diffstat (limited to 'lib.c')
-rw-r--r-- | lib.c | 15 |
1 files changed, 15 insertions, 0 deletions
@@ -4293,6 +4293,21 @@ char *chk_strdup_utf8(const char *str) return copy; } +char *chk_substrdup_utf8(const char *str, size_t off, size_t len) +{ + size_t size = strlen(str) + 1, nchar; + char *copy; + if (off >= size - 1) + return chk_strdup_utf8(""); + if (off + len < off) + uw_throw(error_s, lit("string size overflow")); + nchar = min(size - off, len + 1); + copy = coerce(char *, chk_malloc(nchar)); + memcpy(copy, str, nchar - 1); + copy[nchar - 1] = 0; + return copy; +} + unsigned char *chk_strdup_8bit(const wchar_t *str) { size_t nchar = wcslen(str) + 1, i; |