Re: Uninitialized variable sometimes causing malformed TFM files

Marcel Krüger <[email protected]> Wed, 20 Mar 2019 18:54:06 +0100
Newsgroups gmane.comp.tex.metapost
Message-ID <[email protected]>
 ---- On Wed, 20 Mar 2019 18:29:09 +0100 luigi scarso <[email protected]> wrote ----
 > 
 > 
 > On Sat, Mar 16, 2019 at 6:04 PM Marcel Kr&uuml;ger <[email protected]> wrote:
 > 
 > sorry for delay, I will it asap. -- 
 > luigi

Thanks. There is one other thing, I do not really know if I would describe it as a backward compatibility bug or a feature request:

For non-"scaled" numbersystems, code like

filldraw stroke z1e--z2e;
(example taken from cmbase.mf, `left_bracket`)

fail, because z1e-- is interpreted as z 1e- - instead of z1e --. So z1e--z2e is equal to z1-z2e, leading to lots of errors.

I understand that the new numbersystems and the exponential syntax can lead to problems of this kind, but I think this could be avoided:
Maybe a `e+` / `e-` could only be interpreted as exponential notation if it is followed by a  digit? I do not think
anyone would write "1e-" or "3e+" for 1 or 3, so it should not lead to any breakage.
On the other hand it would catch almost all cases where this is invoked accidentially.

A possible implementation of this change  is attached.

Best regards
Marcel Krüger


 > Hi,
 > 
 > the `indep_value` of `mp->zero_val` is never initialized, but it is used the index written into the
 > depth / ... fields in the TFM files for glyphs with zero depth/.... This causes malformed TFM files.
 > 
 > On my system the TFM corruption can be observed when using LuaTeX to build
 > 
 > \directlua{
 > local find_file
 > do
 >   local kpse = kpse.new('lualatex', 'mpost')
 >   find_file = function(name, mode, type)
 >     return mode == 'w' and name or kpse:find_file(name, type) or (type == 'mp' and kpse:find_file(name, 'mf'))
 >   end
 > end
 > local mp = mplib.new{
 >   job_name = 'cmr10',
 >   find_file = find_file,
 >   math_mode = 'scaled',
 > }
 > local result = mp:execute'input mfplain;mode:=localfont;input cmr10.mf;'
 > print(result.term)
 > }
 > \bye
 > 
 > Using standalone MetaPost I could not trigger this and the problem does not appear with the
 > LuaTeX binary from TeXLive binary from pretest, but custom built luatex versions from both the experimental
 > and the trunk branch showed the problem. The luatex binary available through the contextgarden seems to be affected too.
 > 
 > The problem can be fixed by adding
 > 
 > set_indep_value (mp->zero_val, 0);
 > 
 > below
 > 
 > @ @<Initialize table entries@>=
 > mp->zero_val = mp_get_value_node (mp);
 > set_value_number (mp->zero_val, zero_t);
 > 
 > in mp.w (Around line 33774)
 > 
 > 
 > Best regards
 > Marcel Kr&uuml;ger
 > 
 >

--
http://tug.org/metapost/
MP-digit-change.diff (application/octet-stream, 2.1 KB)
diff --git a/source/texk/web2c/mplibdir/mpmathbinary.w b/source/texk/web2c/mplibdir/mpmathbinary.w
index a4312c52c..b5a2da736 100644
--- a/source/texk/web2c/mplibdir/mpmathbinary.w
+++ b/source/texk/web2c/mplibdir/mpmathbinary.w
@@ -982,6 +982,10 @@ static void find_exponent (MP mp)  {
      if (mp->buffer[mp->cur_input.loc_field] == '+' || 
         mp->buffer[mp->cur_input.loc_field] == '-') {
         mp->cur_input.loc_field++;
+        if (mp->char_class[mp->buffer[mp->cur_input.loc_field]] != digit_class) {
+          mp->cur_input.loc_field -= 2;
+          return;
+        }
      }
      while (mp->char_class[mp->buffer[mp->cur_input.loc_field]] == digit_class) {
        mp->cur_input.loc_field++;
diff --git a/source/texk/web2c/mplibdir/mpmathdecimal.w b/source/texk/web2c/mplibdir/mpmathdecimal.w
index 2ddc21647..b9971a914 100644
--- a/source/texk/web2c/mplibdir/mpmathdecimal.w
+++ b/source/texk/web2c/mplibdir/mpmathdecimal.w
@@ -1073,6 +1073,10 @@ static void find_exponent (MP mp)  {
      if (mp->buffer[mp->cur_input.loc_field] == '+' || 
         mp->buffer[mp->cur_input.loc_field] == '-') {
         mp->cur_input.loc_field++;
+        if (mp->char_class[mp->buffer[mp->cur_input.loc_field]] != digit_class) {
+          mp->cur_input.loc_field -= 2;
+          return;
+        }
      }
      while (mp->char_class[mp->buffer[mp->cur_input.loc_field]] == digit_class) {
        mp->cur_input.loc_field++;
diff --git a/source/texk/web2c/mplibdir/mpmathdouble.w b/source/texk/web2c/mplibdir/mpmathdouble.w
index cb1496936..c305cf290 100644
--- a/source/texk/web2c/mplibdir/mpmathdouble.w
+++ b/source/texk/web2c/mplibdir/mpmathdouble.w
@@ -705,6 +705,10 @@ static void find_exponent (MP mp)  {
      }     
      if (mp->buffer[mp->cur_input.loc_field] == '+' || 
         mp->buffer[mp->cur_input.loc_field] == '-') {
+        if (mp->char_class[mp->buffer[mp->cur_input.loc_field]] != digit_class) {
+          mp->cur_input.loc_field -= 2;
+          return;
+        }
         mp->cur_input.loc_field++;
      }
      while (mp->char_class[mp->buffer[mp->cur_input.loc_field]] == digit_class) {