Commit 02047dc18 for imagemagick.org

commit 02047dc18d48b111feac1c367c63bb4c153e8c9a
Author: Cristy <urban-warrior@imagemagick.org>
Date:   Fri Nov 27 17:36:10 2026 -0500

    WideCharToMultiByte() return value is now checked, so a failed or truncated conversion no longer leaves utf8 uninitialized

diff --git a/MagickCore/nt-feature.c b/MagickCore/nt-feature.c
index 72d818382..4a20bf006 100644
--- a/MagickCore/nt-feature.c
+++ b/MagickCore/nt-feature.c
@@ -80,10 +80,10 @@
 %                                                                             %
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 %
-%  IsMagickConflict() returns true if the image format conflicts with a logical
-%  drive (.e.g. X:).
+%  NTIsMagickConflict() returns true if the image format conflicts with a
+%  logical drive (e.g. X:).
 %
-%  The format of the IsMagickConflict method is:
+%  The format of the NTIsMagickConflict method is:
 %
 %      MagickBooleanType IsMagickConflict(const char *magick)
 %
@@ -94,16 +94,23 @@
 */
 MagickExport MagickBooleanType NTIsMagickConflict(const char *magick)
 {
-  MagickBooleanType
-    status;
+  int
+    c;

   assert(magick != (char *) NULL);
-  if (strlen(magick) > 1)
+  /*
+    A drive specifier is exactly one letter; reject "" and anything longer so
+    the shift below can never be negative or exceed the 26 drive-letter bits.
+  */
+  if ((magick[0] == '\0') || (magick[1] != '\0'))
     return(MagickFalse);
-  status=(GetLogicalDrives() & (1 << ((LocaleToUppercase((int)
-    (*magick)))-'A'))) != 0 ? MagickTrue : MagickFalse;
-  return(status);
+  c=LocaleToUppercase((int) ((unsigned char) magick[0]));
+  if ((c < 'A') || (c > 'Z'))
+    return(MagickFalse);
+  return((GetLogicalDrives() & (1UL << (c-'A'))) != 0 ? MagickTrue :
+    MagickFalse);
 }
+

 /*
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@@ -116,83 +123,137 @@ MagickExport MagickBooleanType NTIsMagickConflict(const char *magick)
 %                                                                             %
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 %
-%  NTAcquireTypeCache() loads a Windows TrueType fonts.
+%  NTAcquireTypeCache() loads the installed Windows TrueType fonts.
 %
 %  The format of the NTAcquireTypeCache method is:
 %
-%      MagickBooleanType NTAcquireTypeCache(SplayTreeInfo *type_cache)
+%      MagickBooleanType NTAcquireTypeCache(SplayTreeInfo *type_cache,
+%        ExceptionInfo *exception)
 %
 %  A description of each parameter follows:
 %
 %    o type_cache: A linked list of fonts.
 %
+%    o exception: return any errors or warnings in this structure.
+%
 */
+
+static void DestroyNTTypeInfo(TypeInfo *type_info)
+{
+  /*
+    Releases a TypeInfo that was never handed over to the type cache.
+  */
+  if (type_info->path != (char *) NULL)
+    type_info->path=DestroyString(type_info->path);
+  if (type_info->name != (char *) NULL)
+    type_info->name=DestroyString(type_info->name);
+  if (type_info->description != (char *) NULL)
+    type_info->description=DestroyString(type_info->description);
+  if (type_info->family != (char *) NULL)
+    type_info->family=DestroyString(type_info->family);
+  if (type_info->encoding != (char *) NULL)
+    type_info->encoding=DestroyString(type_info->encoding);
+  if (type_info->foundry != (char *) NULL)
+    type_info->foundry=DestroyString(type_info->foundry);
+  if (type_info->format != (char *) NULL)
+    type_info->format=DestroyString(type_info->format);
+  if (type_info->metrics != (char *) NULL)
+    type_info->metrics=DestroyString(type_info->metrics);
+  if (type_info->glyphs != (char *) NULL)
+    type_info->glyphs=DestroyString(type_info->glyphs);
+  type_info=(TypeInfo *) RelinquishMagickMemory(type_info);
+}
+
 MagickExport MagickBooleanType NTAcquireTypeCache(SplayTreeInfo *type_cache,
   ExceptionInfo *exception)
 {
-  HKEY
-    reg_key;
-
-  LONG
-    res;
+  static const char
+    *version_keys[] =
+    {
+      "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion",
+      "SOFTWARE\\Microsoft\\Windows\\CurrentVersion"
+    };

   char
     buffer[MagickPathExtent],
     font_root[MagickPathExtent];

   DWORD
-    type,
-    length;
+    length,
+    type;
+
+  HKEY
+    fonts_key,
+    version_key;
+
+  LONG
+    res;

   MagickBooleanType
+    found,
     status;

+  size_t
+    i,
+    root_length;
+
   /*
     Try to find the right Windows*\CurrentVersion key, the SystemRoot and
-    then the Fonts key
+    then the Fonts key.  Every handle that is opened is closed again.
   */
-  reg_key=(HKEY) INVALID_HANDLE_VALUE;
-  res=RegOpenKeyExA(HKEY_LOCAL_MACHINE,
-    "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion",0,KEY_READ,&reg_key);
-  length=sizeof(font_root)-1;
-  if (res == ERROR_SUCCESS)
-    res=RegQueryValueExA(reg_key,"SystemRoot",NULL,&type,(BYTE*) font_root,
+  assert(type_cache != (SplayTreeInfo *) NULL);
+  found=MagickFalse;
+  version_key=(HKEY) NULL;
+  for (i=0; i < (sizeof(version_keys)/sizeof(*version_keys)); i++)
+  {
+    (void) memset(font_root,0,sizeof(font_root));
+    version_key=(HKEY) NULL;
+    res=RegOpenKeyExA(HKEY_LOCAL_MACHINE,version_keys[i],0,KEY_READ,
+      &version_key);
+    if (res != ERROR_SUCCESS)
+      continue;
+    length=(DWORD) (sizeof(font_root)-1);  /* leave room for a terminator */
+    type=REG_NONE;
+    res=RegQueryValueExA(version_key,"SystemRoot",NULL,&type,(BYTE *) font_root,
       &length);
-  if (res != ERROR_SUCCESS)
-    {
-      res=RegOpenKeyExA(HKEY_LOCAL_MACHINE,
-        "SOFTWARE\\Microsoft\\Windows\\CurrentVersion",0,KEY_READ,&reg_key);
-      if (res == ERROR_SUCCESS)
-        res=RegQueryValueExA(reg_key,"SystemRoot",NULL,&type,(BYTE*) font_root,
-          &length);
-    }
-  if (res == ERROR_SUCCESS)
-    res=RegOpenKeyExA(reg_key,"Fonts",0,KEY_READ,&reg_key);
+    if ((res == ERROR_SUCCESS) && ((type == REG_SZ) || (type == REG_EXPAND_SZ)))
+      {
+        found=MagickTrue;
+        break;
+      }
+    (void) RegCloseKey(version_key);
+    version_key=(HKEY) NULL;
+  }
+  if (found == MagickFalse)
+    return(MagickFalse);
+  fonts_key=(HKEY) NULL;
+  res=RegOpenKeyExA(version_key,"Fonts",0,KEY_READ,&fonts_key);
+  (void) RegCloseKey(version_key);
   if (res != ERROR_SUCCESS)
     return(MagickFalse);
-  (void) ConcatenateMagickString(font_root,"\\fonts\\arial.ttf",
-    MagickPathExtent);
-  if (IsPathAccessible(font_root) != MagickFalse)
-    {
-      font_root[length-1]=0;
-      (void) ConcatenateMagickString(font_root,"\\fonts\\",MagickPathExtent);
-    }
+  /*
+    Drop any trailing separators, then pick the fonts directory.
+  */
+  root_length=strlen(font_root);
+  while ((root_length > 0) && (font_root[root_length-1] == '\\'))
+    font_root[--root_length]='\0';
+  (void) CopyMagickString(buffer,font_root,MagickPathExtent);
+  (void) ConcatenateMagickString(buffer,"\\fonts\\arial.ttf",MagickPathExtent);
+  if (IsPathAccessible(buffer) != MagickFalse)
+    (void) ConcatenateMagickString(font_root,"\\fonts\\",MagickPathExtent);
   else
-    {
-      font_root[length-1]=0;
-      (void) ConcatenateMagickString(font_root,"\\",MagickPathExtent);
-    }
+    (void) ConcatenateMagickString(font_root,"\\",MagickPathExtent);

   {
-    TypeInfo
-      *type_info;
-
     DWORD
       registry_index;

     char
       utf8[MagickPathExtent];

+    TypeInfo
+      *type_info;
+
     wchar_t
       wide_name[MagickPathExtent],
       wide_value[MagickPathExtent];
@@ -210,18 +271,33 @@ MagickExport MagickBooleanType NTAcquireTypeCache(SplayTreeInfo *type_cache,
           name_length,
           value_length;

-        name_length=MagickPathExtent-1;
-        value_length=MagickPathExtent-1;
-        res=RegEnumValueW(reg_key,registry_index,(wchar_t *) wide_name,
-          &name_length,0,&type,(BYTE *) wide_value,&value_length);
+        /*
+          RegEnumValueW() counts the name in characters (including the
+          terminator on input) and the data in bytes.
+        */
+        name_length=MagickPathExtent;
+        value_length=(DWORD) (sizeof(wide_value)-sizeof(wchar_t));
+        type=REG_NONE;
+        res=RegEnumValueW(fonts_key,registry_index,(wchar_t *) wide_name,
+          &name_length,NULL,&type,(BYTE *) wide_value,&value_length);
         registry_index++;
         if (res != ERROR_SUCCESS)
           continue;
-        WideCharToMultiByte(CP_UTF8,0,wide_name,-1,utf8,sizeof(utf8),NULL,
-          NULL);
+        if (type != REG_SZ)
+          continue;
+        /*
+          Registry strings are not guaranteed to be terminated.
+        */
+        wide_name[MagickPathExtent-1]=L'\0';
+        wide_value[value_length/sizeof(wchar_t)]=L'\0';
+        if (WideCharToMultiByte(CP_UTF8,0,wide_name,-1,utf8,(int) sizeof(utf8),
+              NULL,NULL) == 0)
+          continue;
         if ((pos=strstr(utf8," (TrueType)")) == (char*) NULL)
           continue;
         *pos='\0'; /* Remove (TrueType) from string */
+        if (*utf8 == '\0')
+          continue;
         type_info=(TypeInfo *) AcquireCriticalMemory(sizeof(*type_info));
         (void) memset(type_info,0,sizeof(TypeInfo));
         type_info->path=ConstantString("Windows Fonts");
@@ -297,11 +373,15 @@ MagickExport MagickBooleanType NTAcquireTypeCache(SplayTreeInfo *type_cache,
             else
               family_extent=q;
           }
-        (void) CopyMagickString(buffer,utf8,family_extent-utf8+1);
+        (void) CopyMagickString(buffer,utf8,(size_t) (family_extent-utf8)+1);
         (void) StripMagickString(buffer);
         type_info->family=ConstantString(buffer);
-        WideCharToMultiByte(CP_UTF8,0,wide_value,-1,utf8,sizeof(utf8),NULL,
-          NULL);
+        if (WideCharToMultiByte(CP_UTF8,0,wide_value,-1,utf8,(int) sizeof(utf8),
+              NULL,NULL) == 0)
+          {
+            DestroyNTTypeInfo(type_info);
+            continue;
+          }
         if (strchr(utf8,'\\') != (char *) NULL)
           (void) CopyMagickString(buffer,utf8,MagickPathExtent);
         else
@@ -313,12 +393,17 @@ MagickExport MagickBooleanType NTAcquireTypeCache(SplayTreeInfo *type_cache,
         type_info->glyphs=ConstantString(buffer);
         status=AddValueToSplayTree(type_cache,type_info->name,type_info);
         if (status == MagickFalse)
-          (void) ThrowMagickException(exception,GetMagickModule(),
-            ResourceLimitError,"MemoryAllocationFailed","`%s'",type_info->name);
+          {
+            (void) ThrowMagickException(exception,GetMagickModule(),
+              ResourceLimitError,"MemoryAllocationFailed","`%s'",
+              type_info->name);
+            DestroyNTTypeInfo(type_info);  /* the tree did not take it */
+          }
       }
   }
-  RegCloseKey(reg_key);
+  (void) RegCloseKey(fonts_key);
   return(MagickTrue);
 }
+

 #endif