From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Stefan Monnier via "Bug reports for GNU Emacs, the Swiss army knife of text editors" Newsgroups: gmane.emacs.bugs Subject: bug#56469: 29.0.50; Unibyte dir in directory_files_internal Date: Sat, 09 Jul 2022 13:44:52 -0400 Message-ID: Reply-To: Stefan Monnier Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: quoted-printable Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="9209"; mail-complaints-to="usenet@ciao.gmane.io" To: 56469@debbugs.gnu.org Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Sat Jul 09 19:46:17 2022 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oAEX2-00028M-Gd for geb-bug-gnu-emacs@m.gmane-mx.org; Sat, 09 Jul 2022 19:46:16 +0200 Original-Received: from localhost ([::1]:33464 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oAEX0-0006ON-Vt for geb-bug-gnu-emacs@m.gmane-mx.org; Sat, 09 Jul 2022 13:46:15 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:60778) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oAEWo-0006O8-Ew for bug-gnu-emacs@gnu.org; Sat, 09 Jul 2022 13:46:02 -0400 Original-Received: from debbugs.gnu.org ([209.51.188.43]:41967) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1oAEWo-0005kg-6j for bug-gnu-emacs@gnu.org; Sat, 09 Jul 2022 13:46:02 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1oAEWo-0006Qy-1w for bug-gnu-emacs@gnu.org; Sat, 09 Jul 2022 13:46:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Stefan Monnier Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Sat, 09 Jul 2022 17:46:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: report 56469 X-GNU-PR-Package: emacs X-Debbugs-Original-To: bug-gnu-emacs@gnu.org Original-Received: via spool by submit@debbugs.gnu.org id=B.165738872124665 (code B ref -1); Sat, 09 Jul 2022 17:46:01 +0000 Original-Received: (at submit) by debbugs.gnu.org; 9 Jul 2022 17:45:21 +0000 Original-Received: from localhost ([127.0.0.1]:35860 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1oAEW8-0006Pl-NS for submit@debbugs.gnu.org; Sat, 09 Jul 2022 13:45:21 -0400 Original-Received: from lists.gnu.org ([209.51.188.17]:59458) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1oAEW6-0006Pd-En for submit@debbugs.gnu.org; Sat, 09 Jul 2022 13:45:18 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:60654) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oAEW3-0006MA-QD for bug-gnu-emacs@gnu.org; Sat, 09 Jul 2022 13:45:18 -0400 Original-Received: from mailscanner.iro.umontreal.ca ([132.204.25.50]:45963) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oAEW0-0005bk-UP for bug-gnu-emacs@gnu.org; Sat, 09 Jul 2022 13:45:14 -0400 Original-Received: from pmg1.iro.umontreal.ca (localhost.localdomain [127.0.0.1]) by pmg1.iro.umontreal.ca (Proxmox) with ESMTP id 42D05100182 for ; Sat, 9 Jul 2022 13:45:10 -0400 (EDT) Original-Received: from mail01.iro.umontreal.ca (unknown [172.31.2.1]) by pmg1.iro.umontreal.ca (Proxmox) with ESMTP id 752B410012B for ; Sat, 9 Jul 2022 13:45:04 -0400 (EDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=iro.umontreal.ca; s=mail; t=1657388704; bh=ADZsJdqYhkryUf4oGzhRT0jk+3ogCDsQVs6wL90dkN0=; h=From:To:Subject:Date:From; b=K5Ot9bninuciVARa3mhu/gDmmvVaKKLX6lb7XWcnty0R6MpVY6vnBleKWThDx3Isb vsFY4ZcMOPiU1HiBuptZSgLPiNc4ciwq/2A7op1W+zfzfCJbHOG1no02rxXzPyGnOR 0mw+ZepCDstuzoXCTbM3jPLDKhWX9wWNBONyeiqB/iBBfqFiuKMvhY2xThxm2H3qOM KtcZhOITv9o4XO1IrSE2TK2JMiwCRDr2pf5ZzUTHMFsZAsLA0hNJd9BFgm4eWBBQOQ LWT1Ag8H/cS5+SrFFiJyTDk6dYA4iZUGCt3JHfqvm0UgcWmVIo0b6ULI9aPkcLmhm+ LcdTKLV+WSDKQ== Original-Received: from pastel (unknown [45.72.196.165]) by mail01.iro.umontreal.ca (Postfix) with ESMTPSA id 1DF7F1204F3 for ; Sat, 9 Jul 2022 13:45:04 -0400 (EDT) Received-SPF: pass client-ip=132.204.25.50; envelope-from=monnier@iro.umontreal.ca; helo=mailscanner.iro.umontreal.ca X-Spam_score_int: -42 X-Spam_score: -4.3 X-Spam_bar: ---- X-Spam_report: (-4.3 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: "bug-gnu-emacs" Xref: news.gmane.io gmane.emacs.bugs:236527 Archived-At: Package: Emacs Version: 29.0.50 If you have a directory named "/tmp/\303a" with a file named "f=E9e" inside, then (directory-files "/tmp/\303a" 'full) is likely to return a funny string which is multibyte but contains an invalid utf-8 sequence (its bytes spell "/tmp/\303a/f\303\251e"). That strings seems to be printed as "/tmp/=A1/f=E9e" which corresponds to "/tmp/\303\241/f\303\251e". Such a string with an invalid UTF-8 sequence is handled quite graciously by Emacs, so I wasn't able to get an actual crash out of it, but it's still something we should avoid. I suggest the patch below. In a comment I suggest we don't try to use unibyte strings when a multibyte string would work as well. This is because for those ASCII-only strings, it's cheaper to test bytes=3D=3Dchars to (re)discover that they are ASCII-only (when they're multibyte) than having to loop through the bytes (when they're unibyte). Stefan diff --git a/src/dired.c b/src/dired.c index 6bb8c2fcb9f..33ddfafd8e7 100644 --- a/src/dired.c +++ b/src/dired.c @@ -219,6 +219,13 @@ directory_files_internal (Lisp_Object directory, Lisp_= Object full, } #endif =20 + if (!NILP (full) && !STRING_MULTIBYTE (directory)) + { /* We will be concatenating 'directory' with local file name. + We always decode local file names, so in order to safely concaten= ate + them we need 'directory' to be multibyte. */ + directory =3D Fstring_to_multibyte (directory); + } + ptrdiff_t directory_nbytes =3D SBYTES (directory); re_match_object =3D Qt; =20 @@ -263,9 +270,10 @@ directory_files_internal (Lisp_Object directory, Lisp_= Object full, ptrdiff_t name_nbytes =3D SBYTES (name); ptrdiff_t nbytes =3D directory_nbytes + needsep + name_nbytes; ptrdiff_t nchars =3D SCHARS (directory) + needsep + SCHARS (name); - finalname =3D make_uninit_multibyte_string (nchars, nbytes); - if (nchars =3D=3D nbytes) - STRING_SET_UNIBYTE (finalname); + /* FIXME: Why not make them all multibyte? */ + finalname =3D (nchars =3D=3D nbytes) + ? make_uninit_string (nchars, nbytes) + : make_uninit_multibyte_string (nchars, nbytes); memcpy (SDATA (finalname), SDATA (directory), directory_nbytes); if (needsep) SSET (finalname, directory_nbytes, DIRECTORY_SEP);