marcandrysco / Errol

Binary floating-point to decimal string conversion algorithm.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

errol3: Wrong result for 9.007199254740992e15

scurest opened this issue · comments

Reproduce with

$ cat a.c
#include "errol.h"
#include <stdio.h>

int main() {
    double x = 9.007199254740992e15;
    char buf[100] = {0};
    int exp = errol3_dtoa(x, buf);
    printf("buf: %s\nexp: %d\n", buf, exp);
    return 0;
}
$ g++ a.c liberrol.a && ./a.out
buf: 
exp: 37

Moreover, this also causes an OOB access at this line

p[-1] += (*p >= '5');

You can test this by compiling with -fsanitize=address.


9.007199254740992e15 is the smallest number that gets passed on to errol_int

Errol/lib/errol.c

Lines 390 to 391 in d31842f

if((val >= 9.007199254740992e15) && (val < 3.40282366920938e+38))
return errol_int(val, buf);

If the >= is changed to a >, it seem to work.