Commit 678b6b789 for imagemagick.org
commit 678b6b78985f0c4d508351492e23d02783bd1a81
Author: Cristy <urban-warrior@imagemagick.org>
Date: Sat Dec 5 07:52:40 2026 -0500
https://github.com/ImageMagick/ImageMagick/issues/9179
diff --git a/MagickCore/blob.c b/MagickCore/blob.c
index bed5907eb..65736638d 100644
--- a/MagickCore/blob.c
+++ b/MagickCore/blob.c
@@ -1522,7 +1522,7 @@ MagickExport void *FileToBlob(const char *filename,const size_t extent,
*/
offset=(MagickOffsetType) lseek(file,0,SEEK_SET);
quantum=(size_t) MagickMaxBufferExtent;
- if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
+ if ((fstat_utf8(file,&file_stats) == 0) && (file_stats.st_size > 0))
quantum=(size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
blob=(unsigned char *) AcquireQuantumMemory(quantum,sizeof(*blob));
for (i=0; blob != (unsigned char *) NULL; i+=(size_t) count)
@@ -1720,7 +1720,7 @@ MagickExport MagickBooleanType FileToImage(Image *image,const char *filename,
ThrowPolicyException(filename,MagickFalse);
}
quantum=(size_t) MagickMaxBufferExtent;
- if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
+ if ((fstat_utf8(file,&file_stats) == 0) && (file_stats.st_size > 0))
quantum=(size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
blob=(unsigned char *) AcquireQuantumMemory(quantum,sizeof(*blob));
if (blob == (unsigned char *) NULL)
@@ -1969,7 +1969,7 @@ MagickExport MagickSizeType GetBlobSize(const Image *image)
file_descriptor=fileno(blob_info->file_info.file);
if (file_descriptor == -1)
break;
- if (fstat(file_descriptor,&blob_info->properties) == 0)
+ if (fstat_utf8(file_descriptor,&blob_info->properties) == 0)
extent=(MagickSizeType) blob_info->properties.st_size;
break;
}
@@ -2448,7 +2448,7 @@ MagickExport MagickBooleanType ImageToFile(Image *image,char *filename,
return(MagickFalse);
}
quantum=(size_t) MagickMaxBufferExtent;
- if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
+ if ((fstat_utf8(file,&file_stats) == 0) && (file_stats.st_size > 0))
quantum=(size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
buffer=(unsigned char *) AcquireQuantumMemory(quantum,sizeof(*buffer));
if (buffer == (unsigned char *) NULL)
@@ -2901,7 +2901,7 @@ MagickExport MagickBooleanType InjectImageBlob(const ImageInfo *image_info,
return(MagickFalse);
}
quantum=(size_t) MagickMaxBufferExtent;
- if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
+ if ((fstat_utf8(file,&file_stats) == 0) && (file_stats.st_size > 0))
quantum=(size_t) MagickMin(file_stats.st_size,MagickMaxBufferExtent);
buffer=(unsigned char *) AcquireQuantumMemory(quantum,sizeof(*buffer));
if (buffer == (unsigned char *) NULL)
@@ -3593,6 +3593,11 @@ MagickExport MagickBooleanType OpenBlob(const ImageInfo *image_info,
unsigned char
magick[3];
+ /*
+ Take the file identity from the open descriptor, not the path,
+ so the properties describe the file actually being read.
+ */
+ (void) fstat_utf8(file,&blob_info->properties);
blob_info->type=FileStream;
(void) SetStreamBuffering(image_info,blob_info);
(void) memset(magick,0,sizeof(magick));
diff --git a/MagickCore/nt-base-private.h b/MagickCore/nt-base-private.h
index 7113eeb2d..ff54d2821 100644
--- a/MagickCore/nt-base-private.h
+++ b/MagickCore/nt-base-private.h
@@ -163,6 +163,7 @@ extern MagickExport FILE
extern MagickExport int
NTAccessWide(const char *,int),
+ NTFileStat(int,struct stat *),
NTOpenWide(const char *,int,mode_t),
NTRemoveWide(const char *),
NTRenameWide(const char *, const char *),
diff --git a/MagickCore/nt-base.c b/MagickCore/nt-base.c
index 3e987118b..972fe7977 100644
--- a/MagickCore/nt-base.c
+++ b/MagickCore/nt-base.c
@@ -2748,8 +2748,6 @@ static inline _ino_t MapFileIndexToIno(uint64_t fileIndex)
return((_ino_t) fileIndex);
}
-MagickExport int NTStatWide(const char *path,struct stat *attributes)
-{
#ifndef S_IFDIR
#define S_IFDIR 0040000 /* directory */
#endif
@@ -2766,6 +2764,32 @@ MagickExport int NTStatWide(const char *path,struct stat *attributes)
#define S_IXUSR 0000100 /* owner has execute/search permission */
#endif
+static void NTSetFileIdentity(HANDLE handle,struct stat *attributes)
+{
+ BY_HANDLE_FILE_INFORMATION
+ file_info;
+
+ /*
+ POSIX emulation of Windows file attributes. Shared by NTStatWide() and
+ NTFileStat() so that a path and an open handle to the same file report
+ the same st_dev, st_ino, and st_mode.
+ */
+ if (GetFileInformationByHandle(handle,&file_info) == 0)
+ return;
+ attributes->st_dev=(dev_t) file_info.dwVolumeSerialNumber;
+ attributes->st_ino=MapFileIndexToIno((((uint64_t) file_info.nFileIndexHigh)
+ << 32) | (uint64_t) file_info.nFileIndexLow);
+ attributes->st_mode=0;
+ if ((file_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0)
+ attributes->st_mode|=S_IFDIR | S_IRUSR | S_IWUSR | S_IXUSR;
+ else
+ attributes->st_mode|=S_IFREG | S_IRUSR | S_IWUSR;
+ if ((file_info.dwFileAttributes & FILE_ATTRIBUTE_READONLY) != 0)
+ attributes->st_mode&=~S_IWUSR;
+}
+
+MagickExport int NTStatWide(const char *path,struct stat *attributes)
+{
int
status;
@@ -2783,32 +2807,57 @@ MagickExport int NTStatWide(const char *path,struct stat *attributes)
OPEN_EXISTING,FILE_FLAG_BACKUP_SEMANTICS,NULL);
if (handle != INVALID_HANDLE_VALUE)
{
- BY_HANDLE_FILE_INFORMATION
- file_info;
-
- /*
- POSIX emulation of Windows file attributes.
- */
- if (GetFileInformationByHandle(handle,&file_info) != 0)
- {
- attributes->st_dev=(dev_t) file_info.dwVolumeSerialNumber;
- attributes->st_ino=MapFileIndexToIno((((uint64_t)
- file_info.nFileIndexHigh) << 32) | (uint64_t)
- file_info.nFileIndexLow);
- attributes->st_mode=0;
- if ((file_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0)
- attributes->st_mode|=S_IFDIR | S_IRUSR | S_IWUSR | S_IXUSR;
- else
- attributes->st_mode|=S_IFREG | S_IRUSR | S_IWUSR;
- if ((file_info.dwFileAttributes & FILE_ATTRIBUTE_READONLY) != 0)
- attributes->st_mode&=~S_IWUSR;
- }
+ NTSetFileIdentity(handle,attributes);
CloseHandle(handle);
}
}
path_wide=(WCHAR *) RelinquishMagickMemory(path_wide);
return(status);
}
+
+/*
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+% %
+% %
+% %
+% N T F i l e S t a t %
+% %
+% %
+% %
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%
+% NTFileStat() gets the file attributes for an open file descriptor. The
+% device, inode, and mode fields are filled exactly as NTStatWide() fills
+% them, so the identity of an open file can be compared with that of a path.
+%
+% The format of the NTFileStat method is:
+%
+% int NTFileStat(int file,struct stat *attributes)
+%
+% A description of each parameter follows:
+%
+% o file: the file descriptor.
+%
+% o attributes: the file attributes.
+%
+*/
+MagickExport int NTFileStat(int file,struct stat *attributes)
+{
+ HANDLE
+ handle;
+
+ int
+ status;
+
+ status=fstat(file,attributes);
+ if (status != 0)
+ return(status);
+ handle=(HANDLE) _get_osfhandle(file);
+ if (handle == INVALID_HANDLE_VALUE)
+ return(-1);
+ NTSetFileIdentity(handle,attributes);
+ return(0);
+}
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
diff --git a/MagickCore/utility-private.h b/MagickCore/utility-private.h
index 965cfd985..062959522 100644
--- a/MagickCore/utility-private.h
+++ b/MagickCore/utility-private.h
@@ -72,6 +72,23 @@ static inline int access_utf8(const char *path,int mode)
#define close_utf8 close
#endif
+static inline int fstat_utf8(int file,struct stat *attributes)
+{
+ /*
+ Under Windows, the device, inode, and mode fields match stat_utf8().
+ */
+ if (attributes == (struct stat *) NULL)
+ {
+ errno=EINVAL;
+ return(-1);
+ }
+#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
+ return(fstat(file,attributes));
+#else
+ return(NTFileStat(file,attributes));
+#endif
+}
+
static inline FILE *fopen_utf8(const char *path,const char *mode)
{
if ((path == (const char *) NULL) || (mode == (const char *) NULL))