Commit dc9623dc4 for imagemagick.org
commit dc9623dc48e426b0f980192f5ec6b6fe7477d90a
Author: Cristy <urban-warrior@imagemagick.org>
Date: Tue Dec 1 18:55:17 2026 -0500
AppendImageFilenameScene() leaves filename as an empty string on failure
diff --git a/coders/coders-private.h b/coders/coders-private.h
index 4c220e6a8..c00740f1b 100644
--- a/coders/coders-private.h
+++ b/coders/coders-private.h
@@ -36,8 +36,7 @@ extern ModuleExport size_t \
extern ModuleExport void \
Unregister ## coder ## Image(void);
-static inline ImageType IdentifyImageCoderType(const Image *image,
- ExceptionInfo *exception)
+static inline ImageType IdentifyImageCoderType(const Image *image,ExceptionInfo *exception)
{
const char
*value;
@@ -48,8 +47,7 @@ static inline ImageType IdentifyImageCoderType(const Image *image,
return(IdentifyImageType(image,exception));
}
-static inline ImageType IdentifyImageCoderGrayType(const Image *image,
- ExceptionInfo *exception)
+static inline ImageType IdentifyImageCoderGrayType(const Image *image,ExceptionInfo *exception)
{
const char
*value;
@@ -60,8 +58,7 @@ static inline ImageType IdentifyImageCoderGrayType(const Image *image,
return(IdentifyImageGray(image,exception));
}
-static inline MagickBooleanType IdentifyImageCoderGray(const Image *image,
- ExceptionInfo *exception)
+static inline MagickBooleanType IdentifyImageCoderGray(const Image *image,ExceptionInfo *exception)
{
ImageType
type;
@@ -70,8 +67,7 @@ static inline MagickBooleanType IdentifyImageCoderGray(const Image *image,
return(IsGrayImageType(type));
}
-static inline MagickBooleanType SetImageCoderGray(Image *image,
- ExceptionInfo *exception)
+static inline MagickBooleanType SetImageCoderGray(Image *image,ExceptionInfo *exception)
{
ImageType
type;
diff --git a/coders/ghostscript-private.h b/coders/ghostscript-private.h
index a5c907cb1..75d102370 100644
--- a/coders/ghostscript-private.h
+++ b/coders/ghostscript-private.h
@@ -13,8 +13,8 @@
See the License for the specific language governing permissions and
limitations under the License.
*/
-#ifndef MAGICK_GHOSTSCRIPT_BUFFER_PRIVATE_H
-#define MAGICK_GHOSTSCRIPT_BUFFER_PRIVATE_H
+#ifndef MAGICK_GHOSTSCRIPT_PRIVATE_H
+#define MAGICK_GHOSTSCRIPT_PRIVATE_H
#include "MagickCore/delegate.h"
#include "MagickCore/delegate-private.h"
@@ -23,8 +23,12 @@
#include "MagickCore/profile-private.h"
#include "coders/bytebuffer-private.h"
-static inline size_t AppendImageFilenameScene(const char *format,int value,
- char *filename)
+/*
+ Expand %d, %o and %x scene specifiers (with optional width) in format into
+ filename, which must hold at least MagickPathExtent bytes. Returns the
+ length of the result, or 0 on failure (filename is then an empty string).
+*/
+static inline size_t AppendImageFilenameScene(const char *format,int value,char *filename)
{
char
*p = filename,
@@ -35,6 +39,7 @@ static inline size_t AppendImageFilenameScene(const char *format,int value,
assert(format != (const char *) NULL);
assert(filename != (char *) NULL);
+ *filename='\0';
while ((*cursor != '\0') && ((p-filename) < ((ssize_t) MagickPathExtent-1)))
{
const char
@@ -73,17 +78,17 @@ static inline size_t AppendImageFilenameScene(const char *format,int value,
ssize_t
count;
- (void) snprintf(format_specifier,sizeof(format_specifier),
- "%%%.*s%c",(int) length,specifier_start,*cursor);
- count=FormatLocaleString(pattern,sizeof(pattern),format_specifier,
- value);
+ (void) snprintf(format_specifier,sizeof(format_specifier),"%%%.*s%c",(int) length,
+ specifier_start,*cursor);
+ count=FormatLocaleString(pattern,sizeof(pattern),format_specifier,value);
pattern_length=strlen(pattern);
- if ((count <= 0) || ((size_t) count != pattern_length))
- return(0);
- if ((p-filename+(ssize_t) pattern_length) >= MagickPathExtent)
- return(0);
- (void) CopyMagickString(p,pattern,(size_t) (MagickPathExtent-
- (p-filename)));
+ if ((count <= 0) || ((size_t) count != pattern_length) ||
+ ((p-filename+(ssize_t) pattern_length) >= MagickPathExtent))
+ {
+ *filename='\0';
+ return(0);
+ }
+ (void) CopyMagickString(p,pattern,(size_t) (MagickPathExtent-(p-filename)));
p+=pattern_length;
cursor++;
continue;
@@ -91,7 +96,7 @@ static inline size_t AppendImageFilenameScene(const char *format,int value,
else
{
/*
- Invalid specifier — treat as literal.
+ Invalid specifier - treat as literal.
*/
cursor=start;
*p++=(*cursor++);
@@ -99,20 +104,26 @@ static inline size_t AppendImageFilenameScene(const char *format,int value,
}
}
/*
- Invalid or unsupported specifier — treat as literal.
+ Invalid or unsupported specifier - treat as literal.
*/
cursor=start;
- if ((p-filename+1) >= MagickPathExtent)
- return(0);
*p++=(*cursor++);
}
+ if (*cursor != '\0')
+ {
+ /*
+ Output would not fit; do not return a silently truncated filename.
+ */
+ *filename='\0';
+ return(0);
+ }
*p='\0';
- return(strlen(filename));
+ return((size_t) (p-filename));
}
#if defined(MAGICKCORE_GS_DELEGATE) || defined(MAGICKCORE_WINDOWS_SUPPORT)
-static int MagickDLLCall GhostscriptDelegateMessage(void *handle,
- const char *message,int length)
+static inline int MagickDLLCall GhostscriptDelegateMessage(void *handle,const char *message,
+ int length)
{
char
**messages;
@@ -122,13 +133,16 @@ static int MagickDLLCall GhostscriptDelegateMessage(void *handle,
offset=0;
messages=(char **) handle;
+ if ((messages == (char **) NULL) || (message == (const char *) NULL) || (length <= 0))
+ return(0);
if (*messages == (char *) NULL)
- *messages=(char *) AcquireQuantumMemory((size_t) length+1,sizeof(char *));
+ *messages=(char *) AcquireQuantumMemory((size_t) length+1,
+ sizeof(**messages));
else
{
offset=(ssize_t) strlen(*messages);
- *messages=(char *) ResizeQuantumMemory(*messages,(size_t) (offset+length+
- 1),sizeof(char *));
+ *messages=(char *) ResizeQuantumMemory(*messages,(size_t) (offset+length+1),
+ sizeof(**messages));
}
if (*messages == (char *) NULL)
return(0);
@@ -137,61 +151,69 @@ static int MagickDLLCall GhostscriptDelegateMessage(void *handle,
return(length);
}
-static double GhostscriptVersion(const GhostInfo *ghost_info)
+static inline double GhostscriptVersion(const GhostInfo *ghost_info)
{
gsapi_revision_t
revision;
+ if (ghost_info->revision == (int (*)(gsapi_revision_t *,int)) NULL)
+ return(0.0);
if ((ghost_info->revision)(&revision,(int) sizeof(revision)) != 0)
return(0.0);
if (revision.revision > 1000)
return((double) revision.revision/1000.0);
return((double) revision.revision/100.0);
}
+
+/*
+ Return the argument portion of a delegate command (everything after the program name).
+ Never returns NULL.
+*/
+static inline const char *GhostscriptCommandArguments(const char *command)
+{
+ const char
+ *args_start;
+
+ if (*command != '"')
+ args_start=strchr(command,' ');
+ else
+ {
+ args_start=strchr(command+1,'"');
+ if (args_start != (const char *) NULL)
+ args_start++;
+ }
+ if (args_start == (const char *) NULL)
+ return("");
+ return(args_start);
+}
#endif
-static inline MagickBooleanType ExecuteGhostscriptCommand(
- const MagickBooleanType verbose,const char *command,char *message,
- ExceptionInfo *exception)
+static inline MagickBooleanType ExecuteGhostscriptCommand(const MagickBooleanType verbose,
+ const char *command,char *message,ExceptionInfo *exception)
{
int
status;
- status=ExternalDelegateCommand(MagickFalse,verbose,command,message,
- exception);
+ status=ExternalDelegateCommand(MagickFalse,verbose,command,message,exception);
if (status == 0)
return(MagickTrue);
if (status < 0)
return(MagickFalse);
- (void) ThrowMagickException(exception,GetMagickModule(),DelegateError,
- "FailedToExecuteCommand","`%s' (%d)",command,status);
+ (void) ThrowMagickException(exception,GetMagickModule(),DelegateError,"FailedToExecuteCommand",
+ "`%s' (%d)",command,status);
return(MagickFalse);
}
-static inline MagickBooleanType InvokeGhostscriptDelegate(
- const MagickBooleanType verbose,const char *command,char *message,
- ExceptionInfo *exception)
+static inline MagickBooleanType InvokeGhostscriptDelegate(const MagickBooleanType verbose,
+ const char *command,char *message,ExceptionInfo *exception)
{
#if defined(MAGICKCORE_GS_DELEGATE) || defined(MAGICKCORE_WINDOWS_SUPPORT)
-#define SetArgsStart(command,args_start) \
- if (args_start == (const char *) NULL) \
- { \
- if (*command != '"') \
- args_start=strchr(command,' '); \
- else \
- { \
- args_start=strchr(command+1,'"'); \
- if (args_start != (const char *) NULL) \
- args_start++; \
- } \
- }
-
char
**argv,
*errors;
const char
- *args_start = (const char *) NULL;
+ *args_start;
const GhostInfo
*ghost_info;
@@ -209,36 +231,29 @@ static inline MagickBooleanType InvokeGhostscriptDelegate(
#if defined(MAGICKCORE_WINDOWS_SUPPORT)
ghost_info=NTGhostscriptDLLVectors();
+ if (ghost_info == (const GhostInfo *) NULL)
+ return(ExecuteGhostscriptCommand(verbose,command,message,exception));
#else
GhostInfo
ghost_info_struct;
ghost_info=(&ghost_info_struct);
(void) memset(&ghost_info_struct,0,sizeof(ghost_info_struct));
- ghost_info_struct.delete_instance=(void (*)(gs_main_instance *))
- gsapi_delete_instance;
+ ghost_info_struct.delete_instance=(void (*)(gs_main_instance *)) gsapi_delete_instance;
ghost_info_struct.exit=(int (*)(gs_main_instance *)) gsapi_exit;
- ghost_info_struct.new_instance=(int (*)(gs_main_instance **,void *))
- gsapi_new_instance;
- ghost_info_struct.init_with_args=(int (*)(gs_main_instance *,int,char **))
- gsapi_init_with_args;
- ghost_info_struct.run_string=(int (*)(gs_main_instance *,const char *,int,
- int *)) gsapi_run_string;
- ghost_info_struct.set_arg_encoding=(int (*)(gs_main_instance*, int))
- gsapi_set_arg_encoding;
- ghost_info_struct.set_stdio=(int (*)(gs_main_instance *,int (*)(void *,char *,
- int),int (*)(void *,const char *,int),int (*)(void *, const char *, int)))
- gsapi_set_stdio;
+ ghost_info_struct.new_instance=(int (*)(gs_main_instance **,void *)) gsapi_new_instance;
+ ghost_info_struct.init_with_args=(int (*)(gs_main_instance *,int,char **)) gsapi_init_with_args;
+ ghost_info_struct.run_string=(int (*)(gs_main_instance *,const char *,int,int *))
+ gsapi_run_string;
+ ghost_info_struct.set_arg_encoding=(int (*)(gs_main_instance*, int)) gsapi_set_arg_encoding;
+ ghost_info_struct.set_stdio=(int (*)(gs_main_instance *,int (*)(void *,char *,int),
+ int (*)(void *,const char *,int),int (*)(void *, const char *, int))) gsapi_set_stdio;
ghost_info_struct.revision=(int (*)(gsapi_revision_t *,int)) gsapi_revision;
#endif
- if (ghost_info == (GhostInfo *) NULL)
- return(ExecuteGhostscriptCommand(verbose,command,message,exception));
if (verbose != MagickFalse)
{
- (void) fprintf(stdout,"[ghostscript library %.2f]",
- GhostscriptVersion(ghost_info));
- SetArgsStart(command,args_start);
- (void) fputs(args_start,stdout);
+ (void) fprintf(stdout,"[ghostscript library %.2f]",GhostscriptVersion(ghost_info));
+ (void) fputs(GhostscriptCommandArguments(command),stdout);
}
interpreter=(gs_main_instance *) NULL;
errors=(char *) NULL;
@@ -250,15 +265,17 @@ static inline MagickBooleanType InvokeGhostscriptDelegate(
if (argv == (char **) NULL)
{
(ghost_info->delete_instance)(interpreter);
+ (void) ThrowMagickException(exception,GetMagickModule(),
+ ResourceLimitError,"MemoryAllocationFailed","`%s'",command);
return(MagickFalse);
}
(void) (ghost_info->set_stdio)(interpreter,(int (MagickDLLCall *)(void *,
char *,int)) NULL,GhostscriptDelegateMessage,GhostscriptDelegateMessage);
- (void) ghost_info->set_arg_encoding(interpreter,1);
+ if (ghost_info->set_arg_encoding != (int (*)(gs_main_instance *,int)) NULL)
+ (void) (ghost_info->set_arg_encoding)(interpreter,1);
status=(ghost_info->init_with_args)(interpreter,argc-1,argv+1);
if (status == 0)
- status=(ghost_info->run_string)(interpreter,"systemdict /start get exec\n",
- 0,&code);
+ status=(ghost_info->run_string)(interpreter,"systemdict /start get exec\n",0,&code);
(ghost_info->exit)(interpreter);
(ghost_info->delete_instance)(interpreter);
for (i=0; i < (ssize_t) argc; i++)
@@ -266,17 +283,15 @@ static inline MagickBooleanType InvokeGhostscriptDelegate(
argv=(char **) RelinquishMagickMemory(argv);
if (status != 0)
{
- SetArgsStart(command,args_start);
+ args_start=GhostscriptCommandArguments(command);
if (status == -101) /* quit */
- (void) FormatLocaleString(message,MaxTextExtent,
- "[ghostscript library %.2f]%s: %s",GhostscriptVersion(ghost_info),
- args_start,errors);
+ (void) FormatLocaleString(message,MagickPathExtent,"[ghostscript library %.2f]%s: %s",
+ GhostscriptVersion(ghost_info),args_start,(errors != (char *) NULL) ? errors : "");
else
{
- (void) ThrowMagickException(exception,GetMagickModule(),
- DelegateError,"PostscriptDelegateFailed",
- "`[ghostscript library %.2f]%s': %s",GhostscriptVersion(ghost_info),
- args_start,errors);
+ (void) ThrowMagickException(exception,GetMagickModule(),DelegateError,
+ "PostscriptDelegateFailed","`[ghostscript library %.2f]%s': %s",
+ GhostscriptVersion(ghost_info),args_start,(errors != (char *) NULL) ? errors : "");
if (errors != (char *) NULL)
errors=DestroyString(errors);
(void) LogMagickEvent(CoderEvent,GetMagickModule(),
@@ -303,18 +318,20 @@ static inline MagickBooleanType IsGhostscriptRendered(const char *path)
if ((path == (const char *) NULL) || (*path == '\0'))
return(MagickFalse);
status=GetPathAttributes(path,&attributes);
- if ((status != MagickFalse) && S_ISREG(attributes.st_mode) &&
- (attributes.st_size > 0))
+ if ((status != MagickFalse) && S_ISREG(attributes.st_mode) && (attributes.st_size > 0))
return(MagickTrue);
return(MagickFalse);
}
-static inline void ReadGhostScriptXMPProfile(MagickByteBuffer *buffer,
- StringInfo **profile,ExceptionInfo *exception)
+static inline void ReadGhostScriptXMPProfile(MagickByteBuffer *buffer,StringInfo **profile,
+ ExceptionInfo *exception)
{
#define BeginXMPPacket "?xpacket begin="
#define EndXMPPacket "<?xpacket end="
+ char
+ *p;
+
int
c;
@@ -322,9 +339,6 @@ static inline void ReadGhostScriptXMPProfile(MagickByteBuffer *buffer,
found_end,
status;
- char
- *p;
-
size_t
length;
@@ -348,6 +362,8 @@ static inline void ReadGhostScriptXMPProfile(MagickByteBuffer *buffer,
{
if (count == (ssize_t) length)
{
+ if (length > (((size_t) SSIZE_MAX) >> 1))
+ break;
length<<=1;
SetStringInfoLength(*profile,length);
p=(char *) GetStringInfoDatum(*profile)+count;
@@ -355,26 +371,27 @@ static inline void ReadGhostScriptXMPProfile(MagickByteBuffer *buffer,
count++;
*p++=(char) c;
if (found_end == MagickFalse)
- found_end=CompareMagickByteBuffer(buffer,EndXMPPacket,
- strlen(EndXMPPacket));
+ found_end=CompareMagickByteBuffer(buffer,EndXMPPacket,strlen(EndXMPPacket));
else
{
if (c == (int) '>')
break;
}
}
- SetStringInfoLength(*profile,(size_t) count);
- /* ReadPDFImage() scans the profile as a C string. */
+ /*
+ ReadPDFImage() scans the profile as a C string: make room for the
+ terminator first, then trim the reported length back to the payload.
+ */
+ SetStringInfoLength(*profile,(size_t) count+1);
GetStringInfoDatum(*profile)[count]='\0';
+ SetStringInfoLength(*profile,(size_t) count);
}
-static inline char *EscapeParenthesis(const char *source,
- const size_t max_length,ExceptionInfo *exception)
+static inline char *EscapeParenthesis(const char *source,const size_t max_length,
+ ExceptionInfo *exception)
{
char
- *destination;
-
- char
+ *destination,
*q;
const char
@@ -399,12 +416,18 @@ static inline char *EscapeParenthesis(const char *source,
end=p+1;
}
if (*end != '\0')
- (void) ThrowMagickException(exception,GetMagickModule(),CoderWarning,
- "LabelTruncated","`%g'",(double) max_length);
+ {
+ /*
+ Do not cut a multi-byte UTF-8 sequence in half.
+ */
+ while ((end > source) && ((((unsigned char) *end) & 0xc0) == 0x80))
+ end--;
+ (void) ThrowMagickException(exception,GetMagickModule(),CoderWarning,"LabelTruncated",
+ "`%g'",(double) max_length);
+ }
destination=(char *) NULL;
if (~length >= (MagickPathExtent-1))
- destination=(char *) AcquireQuantumMemory(length+MagickPathExtent,
- sizeof(*destination));
+ destination=(char *) AcquireQuantumMemory(length+MagickPathExtent,sizeof(*destination));
if (destination == (char *) NULL)
ThrowFatalException(ResourceLimitFatalError,"UnableToEscapeString");
*destination='\0';