[metapost] Bug? uncaught overflow with "length"

Hans Hagen j.hagen at xs4all.nl
Tue May 24 15:17:34 CEST 2022


On 5/24/2022 2:39 PM, Toby Thurston wrote:
> I was investigating Peano-Gosper curves this morning, and came across some surprising behaviour with “length”.  Consider this toy program
> 
>   path p;
>   p = (0, 0) -- (0, 2) -- (1, 3) -- (1, 1) -- (21, 0) -- (21, 2) -- (21, 4) -- (0, 0);
>   for i=1 upto 5:
>      p := p & p & p & p & p & p & p;
>      show (i, length p);
>   endfor
>   end.
> 
> Each loop makes the path 7 times longer.  Here’s my ouptput:
> 
> % This is MetaPost, version 2.02 (TeX Live 2022) (kpathsea version 6.3.4)
> % (/usr/local/texlive/2022/texmf-dist/metapost/base/mpost.mp
> % (/usr/local/texlive/2022/texmf-dist/metapost/base/plain.mp
> % Preloading the plain mem file, version 1.005) ) (./bug.mp
> % >> (1,49)
> % >> (2,343)
> % >> (3,2401)
> % >> (4,16807)
> % >> (5,-13423) )
> % Transcript written on bug.log.
> 
> As you can see on the fifth iteration, I get a negative number from length instead 7**6 = 117649.  Apparently the number has overflowed.  Since this is much larger than 2*15-1 I am not surprised about the overflow, but I *am* surprised that there was no Arithmetic Overflow error, or anything else to alert the user than the returned length was nonsense.
> 
> Of course, it works fine if I try it with -numbersystem=double
> 
> But what is even more surprising is that I get exactly the same surprising negative number with no warning or error from Metafont:
> 
> % This is METAFONT, Version 2.71828182 (TeX Live 2022) (preloaded base=mf)
> % (bug.mp
> % >> (1,49)
> % >> (2,343)
> % >> (3,2401)
> % >> (4,16807)
> % >> (5,-13423) )
> % Transcript written on bug.log.

Like tex there is not always checking (irr for performance reasons):

     \message{[\the\maxdimen]}
     \setbox0\vbox {x\vskip 16000pt  x}
     \message{[\the\ht0]}
     \setbox0\vbox {\unvcopy0 \unvcopy0 }
     \message{[\the\ht0]}
     \setbox0\vbox {\unvcopy0 \unvcopy0 }
     \message{[\the\ht0]}

it is also an escape as one can for instance split so who knows what 
dirty tricks one can do with mp (in scaled mode).

So, not really a bug but a side effect.

Hans

-----------------------------------------------------------------
                                           Hans Hagen | PRAGMA ADE
               Ridderstraat 27 | 8061 GH Hasselt | The Netherlands
        tel: 038 477 53 69 | www.pragma-ade.nl | www.pragma-pod.nl
-----------------------------------------------------------------


More information about the metapost mailing list.