Subject: bug#71935: split-string-and-unquote mishandles dired-listing-switches with '

From: Eli Zaretskii <[email-masked]>
Date: Thu, 04 Jul 2024 11:10:04 +0300

> From: Juri Linkov <[email-masked]>
> Date: Thu, 04 Jul 2024 09:51:10 +0300
> 
> 0. emacs -Q
> 1. (setopt dired-listing-switches "-al --block-size='1")
> 2. C-x d /tmp/*
> 
>   /tmp:
>   wildcard *
>   /bin/bash: -c: line 0: unexpected EOF while looking for matching `''
>   /bin/bash: -c: line 1: syntax error: unexpected end of file
> 
> 3. (setopt dired-listing-switches "-al --block-size=\\'1")
> 4. C-x d /tmp/
> 
> Debugger entered--Lisp error: (error "Listing directory failed but ‘access-file’ worked")
>   error("Listing directory failed but `access-file' worked")
>   insert-directory("/tmp/" "--dired -N -al --block-size=\\'1" nil t)
>   dired-insert-directory("/tmp/" "-al --block-size=\\'1" nil nil t)
>   dired-readin-insert()
>   dired-readin()
>   dired-internal-noselect("/tmp/" nil)
>   dired-noselect("/tmp/" nil)
>   dired("/tmp/" nil)
>   funcall-interactively(dired "/tmp/" nil)
>   command-execute(dired)
> 
> because 'split-string-and-unquote' in 'insert-directory'
> doesn't turn "--block-size=\\'1" into "--block-size='1".

I think the part of insert-directory that deals with wildcard lacks
the call to shell-quote-argument here:

		      ;; NB since switches is passed to the shell, be
		      ;; careful of malicious values, eg "-l;reboot".
		      ;; See eg dired-safe-switches-p.
		      (call-process
		       shell-file-name nil t nil
		       shell-command-switch
		       (concat (if (memq system-type '(ms-dos windows-nt))
				   ""
				 "\\") ; Disregard Unix shell aliases!
			       insert-directory-program
			       " -d "
			       (if (stringp switches)
				   switches
				 (mapconcat #'identity switches " "))

Can you try running each switch through shell-quote-argument?