Null-byte delimit envvar file lines

This prevents issues with multi-line envvar values.
This commit is contained in:
Henrik Lissner 2020-04-29 19:39:35 -04:00
parent 079b748217
commit f61fa50336
No known key found for this signature in database
GPG key ID: 5F6C0EA160557395
2 changed files with 22 additions and 31 deletions

View file

@ -113,10 +113,11 @@ default, on Linux, this is '$SHELL -ic /usr/bin/env'. Variables in
"# run 'doom sync'. To create a safe-to-edit envvar file use:\n#\n" "# run 'doom sync'. To create a safe-to-edit envvar file use:\n#\n"
"# doom env -o ~/.doom.d/myenv\n#\n" "# doom env -o ~/.doom.d/myenv\n#\n"
"# And load it with (doom-load-envvars-file \"~/.doom.d/myenv\").\n") "# And load it with (doom-load-envvars-file \"~/.doom.d/myenv\").\n")
(concat "# This file is safe to edit by hand, but needs to be loaded manually with:\n#\n" (concat "# This file is safe to edit by hand, but remember to preserve the null bytes at\n"
"# the end of each line! needs to be loaded manually with:\n#\n"
"# (doom-load-envvars-file \"path/to/this/file\")\n#\n" "# (doom-load-envvars-file \"path/to/this/file\")\n#\n"
"# Use 'doom env -o path/to/this/file' to regenerate it.")) "# Use 'doom env -o path/to/this/file' to regenerate it."))
"# ---------------------------------------------------------------------------\n\n")) "# ---------------------------------------------------------------------------\n\0\n"))
;; We assume that this noninteractive session was spawned from the ;; We assume that this noninteractive session was spawned from the
;; user's interactive shell, therefore we just dump ;; user's interactive shell, therefore we just dump
;; `process-environment' to a file. ;; `process-environment' to a file.
@ -124,7 +125,7 @@ default, on Linux, this is '$SHELL -ic /usr/bin/env'. Variables in
(if (cl-find-if (doom-rpartial #'string-match-p (car (split-string env "="))) (if (cl-find-if (doom-rpartial #'string-match-p (car (split-string env "=")))
doom-env-ignored-vars) doom-env-ignored-vars)
(print! (info "Ignoring %s") env) (print! (info "Ignoring %s") env)
(insert env "\n"))) (insert env "\0\n")))
(print! (success "Successfully generated %S") (print! (success "Successfully generated %S")
(path env-file)) (path env-file))
t)))))) t))))))

View file

@ -465,34 +465,24 @@ If NOERROR is non-nil, don't throw an error if the file doesn't exist or is
unreadable. Returns the names of envvars that were changed." unreadable. Returns the names of envvars that were changed."
(if (not (file-readable-p file)) (if (not (file-readable-p file))
(unless noerror (unless noerror
(signal 'file-error (list "Couldn't read envvar file" file))) (signal 'file-error (list "No envvar file exists" file)))
(let (envvars environment) (when-let
(with-temp-buffer (env
(save-excursion (with-temp-buffer
(insert "\n") (save-excursion
(insert-file-contents file)) (insert "\0\n") ; to prevent off-by-one
(while (re-search-forward "\n *\\([^#= \n]*\\)=" nil t) (insert-file-contents file))
(push (match-string 1) envvars) (save-match-data
(push (buffer-substring (when (re-search-forward "\0\n *\\([^#= \n]*\\)=" nil t)
(match-beginning 1) (setq
(1- (or (save-excursion env (split-string (buffer-substring (match-beginning 1) (point-max))
(when (re-search-forward "^\\([^= ]+\\)=" nil t) "\0\n"
(line-beginning-position))) 'omit-nulls))))))
(point-max)))) (setq process-environment (append (nreverse env) process-environment)
environment))) exec-path (append (split-string (getenv "PATH") path-separator t)
(when environment (list exec-directory))
(setq process-environment shell-file-name (or (getenv "SHELL") shell-file-name))
(append (nreverse environment) process-environment) env)))
exec-path
(if (member "PATH" envvars)
(append (split-string (getenv "PATH") path-separator t)
(list exec-directory))
exec-path)
shell-file-name
(if (member "SHELL" envvars)
(or (getenv "SHELL") shell-file-name)
shell-file-name))
envvars))))
(defun doom-initialize (&optional force-p noerror) (defun doom-initialize (&optional force-p noerror)
"Bootstrap Doom, if it hasn't already (or if FORCE-P is non-nil). "Bootstrap Doom, if it hasn't already (or if FORCE-P is non-nil).