Commit 81a456b28f195b4d4e48aff03359aaa68ae6e3b4

Alexei Podtelezhnikov 2023-01-04T22:41:34

* src/base/ftobjs.c (FT_Request_Metrics): Avoid division by zero. The division-by-zero might happen in broken fonts (see #1194). Instead of returning a huge number from FT_DivFix and failing to scale later, we now bail immediately.

diff --git a/src/base/ftobjs.c b/src/base/ftobjs.c
index 7f1f063..4ef702a 100644
--- a/src/base/ftobjs.c
+++ b/src/base/ftobjs.c
@@ -3280,34 +3280,49 @@
       scaled_h = FT_REQUEST_HEIGHT( req );
 
       /* determine scales */
-      if ( req->width )
+      if ( req->height || !req->width )
       {
-        metrics->x_scale = FT_DivFix( scaled_w, w );
-
-        if ( req->height )
+        if ( h == 0 )
         {
-          metrics->y_scale = FT_DivFix( scaled_h, h );
-
-          if ( req->type == FT_SIZE_REQUEST_TYPE_CELL )
-          {
-            if ( metrics->y_scale > metrics->x_scale )
-              metrics->y_scale = metrics->x_scale;
-            else
-              metrics->x_scale = metrics->y_scale;
-          }
+          FT_ERROR(( "FT_Request_Metrics: Divide by zero\n" ));
+          error = FT_ERR( Divide_By_Zero );
+          goto Exit;
         }
-        else
+
+        metrics->y_scale = FT_DivFix( scaled_h, h );
+      }
+
+      if ( req->width )
+      {
+        if ( w == 0 )
         {
-          metrics->y_scale = metrics->x_scale;
-          scaled_h = FT_MulDiv( scaled_w, h, w );
+          FT_ERROR(( "FT_Request_Metrics: Divide by zero\n" ));
+          error = FT_ERR( Divide_By_Zero );
+          goto Exit;
         }
+
+        metrics->x_scale = FT_DivFix( scaled_w, w );
       }
       else
       {
-        metrics->x_scale = metrics->y_scale = FT_DivFix( scaled_h, h );
+        metrics->x_scale = metrics->y_scale;
         scaled_w = FT_MulDiv( scaled_h, w, h );
       }
 
+      if ( !req->height )
+      {
+        metrics->y_scale = metrics->x_scale;
+        scaled_h = FT_MulDiv( scaled_w, h, w );
+      }
+
+      if ( req->type == FT_SIZE_REQUEST_TYPE_CELL )
+      {
+        if ( metrics->y_scale > metrics->x_scale )
+          metrics->y_scale = metrics->x_scale;
+        else
+          metrics->x_scale = metrics->y_scale;
+      }
+
   Calculate_Ppem:
       /* calculate the ppems */
       if ( req->type != FT_SIZE_REQUEST_TYPE_NOMINAL )