From fb62846b68bd12f90dc16f1b0dd326546d41ec49 Mon Sep 17 00:00:00 2001 From: Alan Third Date: Tue, 8 Jun 2021 20:08:34 +0100 Subject: [PATCH] Fix image filename encoding issues (bug#48902) * src/image.c (image_find_image_fd): Don't return an encoded filename string. * src/nsfns.m: ([NSString stringWithLispString:]): Clarify usage comment. * src/nsimage.m ([EmacsImage allocInitFromFile:]): No need to encode the filename when converting to NSString. --- src/image.c | 23 ++++++++++------------- src/nsfns.m | 3 ++- src/nsimage.m | 9 ++++----- 3 files changed, 16 insertions(+), 19 deletions(-) diff --git a/src/image.c b/src/image.c index b34dc3e9161..07de4d31aa8 100644 --- a/src/image.c +++ b/src/image.c @@ -3153,19 +3153,16 @@ image_find_image_fd (Lisp_Object file, int *pfd) /* Try to find FILE in data-directory/images, then x-bitmap-file-path. */ fd = openp (search_path, file, Qnil, &file_found, pfd ? Qt : make_fixnum (R_OK), false, false); - if (fd >= 0 || fd == -2) + if (fd == -2) { - file_found = ENCODE_FILE (file_found); - if (fd == -2) - { - /* The file exists locally, but has a file name handler. - (This happens, e.g., under Auto Image File Mode.) - 'openp' didn't open the file, so we should, because the - caller expects that. */ - fd = emacs_open (SSDATA (file_found), O_RDONLY, 0); - } + /* The file exists locally, but has a file name handler. + (This happens, e.g., under Auto Image File Mode.) + 'openp' didn't open the file, so we should, because the + caller expects that. */ + Lisp_Object encoded_name = ENCODE_FILE (file_found); + fd = emacs_open (SSDATA (encoded_name), O_RDONLY, 0); } - else /* fd < 0, but not -2 */ + else if (fd < 0) return Qnil; if (pfd) *pfd = fd; @@ -3173,8 +3170,8 @@ image_find_image_fd (Lisp_Object file, int *pfd) } /* Find image file FILE. Look in data-directory/images, then - x-bitmap-file-path. Value is the encoded full name of the file - found, or nil if not found. */ + x-bitmap-file-path. Value is the full name of the file found, or + nil if not found. */ Lisp_Object image_find_image_file (Lisp_Object file) diff --git a/src/nsfns.m b/src/nsfns.m index d14f7b51eaf..98801d8526f 100644 --- a/src/nsfns.m +++ b/src/nsfns.m @@ -3024,7 +3024,8 @@ all_nonzero_ascii (unsigned char *str, ptrdiff_t n) } @implementation NSString (EmacsString) -/* Make an NSString from a Lisp string. */ +/* Make an NSString from a Lisp string. STRING must not be in an + encoded form (e.g. UTF-8). */ + (NSString *)stringWithLispString:(Lisp_Object)string { /* Shortcut for the common case. */ diff --git a/src/nsimage.m b/src/nsimage.m index fa81a41a519..3c16cd371e6 100644 --- a/src/nsimage.m +++ b/src/nsimage.m @@ -254,15 +254,15 @@ ns_image_size_in_bytes (void *img) NSImageRep *imgRep; Lisp_Object found; EmacsImage *image; + NSString *filename; /* Search bitmap-file-path for the file, if appropriate. */ found = image_find_image_file (file); if (!STRINGP (found)) return nil; - found = ENCODE_FILE (found); + filename = [NSString stringWithLispString:found]; - image = [[EmacsImage alloc] initByReferencingFile: - [NSString stringWithLispString: found]]; + image = [[EmacsImage alloc] initByReferencingFile:filename]; image->bmRep = nil; #ifdef NS_IMPL_COCOA @@ -277,8 +277,7 @@ ns_image_size_in_bytes (void *img) } [image setSize: NSMakeSize([imgRep pixelsWide], [imgRep pixelsHigh])]; - - [image setName: [NSString stringWithLispString: file]]; + [image setName:filename]; return image; } -- 2.39.5