lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Hello,

This seems to be a problem with the C library in Open Watcom 1.9 and not a Lua problem but I wanted to mention it here just in case anyone else runs into it.

When I compile Lua (5.2.3) with Open Watcom (1.9) and run the 5.2.2 test suite with _U=true, I get the following assertion failure:

    ***** FILE 'math.lua'*****
    testing numbers and math lib
    ../lua-5.2.3/src/lua: math.lua:96: assertion failed!
    stack traceback:
            [C]: in function 'assert'
            math.lua:96: in main chunk
            (...tail calls...)
            all.lua:188: in main chunk
            [C]: in ?
    ..>>> closing state <<<

The relevant line in math.lua is:

    assert(f(tonumber('  ')) == nil)

A quick test shows the different behaviors:

This lua was built with gcc:

    Lua 5.2.3  Copyright (C) 1994-2013 Lua.org, PUC-Rio
    > =tonumber('  ')
    nil

And this lua was built with Open Watcom:

    Lua 5.2.3  Copyright (C) 1994-2013 Lua.org, PUC-Rio
    > =tonumber('  ')
    0

Digging deeper, the problem seems to be in the behavior of Open Watcom's "strtod" function. The tonumber() conversion seems to wind its way down into the luaO_std2d function which calls the native strtod function (via the lua_str2number macro). Open Watcom's strtod increments its "endptr" parameter even though no conversion took place, thwarting the "if (endptr == s) return 0; " test in luaO_str2d.

Here's a quick C program to test the strtod behavior:

    #include <stdio.h>
    #include <stdlib.h>
    #include <errno.h>

    void try (char *s) {
            double d;
            char *endptr;

            errno = 0;
            d = strtod (s, &endptr);
            printf ("s=\"%s\"  d=%g  errno=%d\n", s, d, errno);
            printf ("s=%p  endptr=%p\n", s, endptr);
            printf ("\n");
    }

    int main (int argc, char *argv[]) {
            try("123");
            try("  ");
            return 0;
    }

Compiled with gcc:

    s="123"  d=123  errno=0
    s=0x400771  endptr=0x400774

    s="  "  d=0  errno=0
    s=0x400775  endptr=0x400775

Compiled with Open Watcom:

    s="123"  d=123  errno=0
    s=0804c02f  endptr=0804c032

    s="  "  d=0  errno=0
    s=0804c033  endptr=0804c035

The Watcom version increments endptr for the " " case even though no conversion took place.

If I comment out the offending line in math.lua, the rest of the _U=true tests run successfully.

--
Edward Berner