Commit 4cdbcd46d for imagemagick.org
commit 4cdbcd46d9f87fcec256708d22add039eea685ba
Author: Cristy <urban-warrior@imagemagick.org>
Date: Fri Jun 19 22:10:13 2026 -0400
introducing StrictReadImage() private method
diff --git a/MagickCore/constitute-private.h b/MagickCore/constitute-private.h
index c0518b67f..a831c4163 100644
--- a/MagickCore/constitute-private.h
+++ b/MagickCore/constitute-private.h
@@ -22,6 +22,42 @@
extern "C" {
#endif
+#include "MagickCore/constitute.h"
+#include "MagickCore/exception.h"
+#include "MagickCore/log.h"
+#include "MagickCore/utility.h"
+
+static inline Image *StrictReadImage(const ImageInfo *image_info,
+ ExceptionInfo *exception)
+{
+ char
+ magic[MagickPathExtent];
+
+ struct stat
+ file_info;
+
+ (void) GetPathComponent(image_info->filename,MagickPath,magic);
+ if (*magic != '\0')
+ {
+ (void) ThrowMagickException(exception, GetMagickModule(), OptionError,
+ "ExplicitCoderNotAllowed","`%s'",image_info->filename);
+ return((Image *) NULL);
+ }
+ if (stat(image_info->filename,&file_info) != 0)
+ {
+ (void) ThrowMagickException(exception,GetMagickModule(),FileOpenError,
+ "UnableToOpenFile","`%s'",image_info->filename);
+ return((Image *) NULL);
+ }
+ if (S_ISREG(file_info.st_mode) == 0)
+ {
+ (void) ThrowMagickException(exception,GetMagickModule(),OptionError,
+ "NotARegularFile", "`%s'",image_info->filename);
+ return((Image *) NULL);
+ }
+ return(ReadImage(image_info,exception));
+}
+
#if defined(__cplusplus) || defined(c_plusplus)
}
#endif
diff --git a/MagickCore/identify.c b/MagickCore/identify.c
index 062ee9ba9..71f612186 100644
--- a/MagickCore/identify.c
+++ b/MagickCore/identify.c
@@ -54,6 +54,7 @@
#include "MagickCore/color.h"
#include "MagickCore/configure.h"
#include "MagickCore/constitute.h"
+#include "MagickCore/constitute-private.h"
#include "MagickCore/decorate.h"
#include "MagickCore/delegate.h"
#include "MagickCore/draw.h"
@@ -1369,18 +1370,7 @@ MagickExport MagickBooleanType IdentifyImage(Image *image,FILE *file,
d=q;
(void) FormatLocaleFile(file," %s",image_info->filename);
handler=SetWarningHandler((WarningHandler) NULL);
- tile=(Image *) NULL;
- {
- char
- magic[MagickPathExtent] = { '\0' };
-
- GetPathComponent(image_info->filename,MagickPath,magic);
- if (*magic == '\0')
- tile=ReadImage(image_info,exception);
- else
- (void) ThrowMagickException(exception,GetMagickModule(),
- FileOpenError,"UnableToOpenFile","`%s'",image_info->filename);
- }
+ tile=StrictReadImage(image_info,exception);
(void) SetWarningHandler(handler);
if (tile == (Image *) NULL)
{
diff --git a/coders/json.c b/coders/json.c
index b7d042d99..cd886485b 100644
--- a/coders/json.c
+++ b/coders/json.c
@@ -48,6 +48,7 @@
#include "MagickCore/colorspace.h"
#include "MagickCore/colorspace-private.h"
#include "MagickCore/constitute.h"
+#include "MagickCore/constitute-private.h"
#include "MagickCore/exception.h"
#include "MagickCore/exception-private.h"
#include "MagickCore/feature.h"
@@ -1552,18 +1553,7 @@ static MagickBooleanType EncodeImageAttributes(Image *image,FILE *file,
JSONFormatLocaleFile(file,"{\n \"name\": %s",
image_info->filename);
handler=SetWarningHandler((WarningHandler) NULL);
- tile=(Image *) NULL;
- {
- char
- magic[MagickPathExtent] = { '\0' };
-
- GetPathComponent(image_info->filename,MagickPath,magic);
- if (*magic == '\0')
- tile=ReadImage(image_info,exception);
- else
- (void) ThrowMagickException(exception,GetMagickModule(),
- FileOpenError,"UnableToOpenFile","`%s'",image_info->filename);
- }
+ tile=StrictReadImage(image_info,exception);
(void) SetWarningHandler(handler);
if (tile == (Image *) NULL)
{
diff --git a/coders/svg.c b/coders/svg.c
index acc95259c..42f62b009 100644
--- a/coders/svg.c
+++ b/coders/svg.c
@@ -48,6 +48,7 @@
#include "MagickCore/blob-private.h"
#include "MagickCore/cache.h"
#include "MagickCore/constitute.h"
+#include "MagickCore/constitute-private.h"
#include "MagickCore/composite-private.h"
#include "MagickCore/delegate.h"
#include "MagickCore/delegate-private.h"
@@ -2725,18 +2726,7 @@ static void SVGEndElement(void *context,const xmlChar *name)
if (LocaleNCompare(image_info->filename,"data:",5) == 0)
image=ReadInlineImage(image_info,svg_info->url,svg_info->exception);
else
- {
- char
- magic[MagickPathExtent] = { '\0' };
-
- GetPathComponent(image_info->filename,MagickPath,magic);
- if (*magic == '\0')
- image=ReadImage(image_info,svg_info->exception);
- else
- (void) ThrowMagickException(svg_info->exception,
- GetMagickModule(),FileOpenError,"UnableToOpenFile","`%s'",
- image_info->filename);
- }
+ image=StrictReadImage(image_info,svg_info->exception);
image_info=DestroyImageInfo(image_info);
if (image != (Image *) NULL)
image=DestroyImage(image);
diff --git a/coders/yaml.c b/coders/yaml.c
index 7a7518c42..bc35a93c1 100644
--- a/coders/yaml.c
+++ b/coders/yaml.c
@@ -48,6 +48,7 @@
#include "MagickCore/colorspace.h"
#include "MagickCore/colorspace-private.h"
#include "MagickCore/constitute.h"
+#include "MagickCore/constitute-private.h"
#include "MagickCore/exception.h"
#include "MagickCore/exception-private.h"
#include "MagickCore/feature.h"
@@ -1521,18 +1522,7 @@ static MagickBooleanType EncodeImageAttributes(Image *image,FILE *file,
YAMLFormatLocaleFile(file,"\n - name: %s",
image_info->filename);
handler=SetWarningHandler((WarningHandler) NULL);
- tile=(Image *) NULL;
- {
- char
- magic[MagickPathExtent] = { '\0' };
-
- GetPathComponent(image_info->filename,MagickPath,magic);
- if (*magic == '\0')
- tile=ReadImage(image_info,exception);
- else
- (void) ThrowMagickException(exception,GetMagickModule(),
- FileOpenError,"UnableToOpenFile","`%s'",image_info->filename);
- }
+ tile=StrictReadImage(image_info,exception);
(void) SetWarningHandler(handler);
if (tile == (Image *) NULL)
{