sierrafoxtrot / srecord

SRecord github Mirror
https://srecord.sourceforge.net/
GNU General Public License v3.0
46 stars 23 forks source link

Floating point exception #35

Closed oceanofthelost closed 1 year ago

oceanofthelost commented 1 year ago

System

Distributor ID: Ubuntu Description: Ubuntu 22.04.1 LTS Release: 22.04 Codename: jammy

Error

A divide by zero error can occur when providing rounding:

case token_round_down:
        token_next();
        multiple = get_number("-round-down");
        if(multiple == 0)
        {
            multiple = 1;
        }
        value /= multiple;
        value *= multiple;
        cout<<"HERE"<<endl;
        break;

    case token_round_up:
        token_next();
        multiple = get_number("-round-up");
        value = (value + multiple - 1) / multiple;
        value *= multiple;
        break;

    case token_round_nearest:
        token_next();
        multiple = get_number("-round-nearest");
        value = (value + multiple / 2) / multiple;
        value *= multiple;
        break;

When providing the following as input:

$ srec_cat test.in  -offset  - 1 -Round_Up 0
$ srec_cat test.in  -offset  - 1 -Round_Down 0
$ srec_cat test.in  -offset  - 1 -Round_Nearest 0

In each of the above cases, output will be

Floating point exception (core dumped)

Issue is due to line

multiple = get_number("-round-up");

which is not verified to be non zero. In each of the above cases, immediately following parsing 0, 0 will be used and cause an exception.

jtxa commented 1 year ago

That's a nice catch and fix.

I was confused by the - 1, so needed to read the manual and play around a bit. It seems that those calculations can only be applied to other calculated values, not simple numbers. So an -offset 7 -Round_Up 4 has to be written as -offset - -7 -Round_Up 4

SeanAlling-DojoFive commented 1 year ago

With #36 merged I think we can close this bug.