lua-users home
lua-l archive

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


The other day I discovered that the C code for
io_date in liolib.c (Lua 5.0 and previous)
was causing floating point exceptions on my platform.
For some mysterious reason coercion of (double) -1 
to a time_t makes my compiler produce code that raises
an error. The code for io_date starts:

static int io_date (lua_State *L) {
  const char *s = luaL_optstring(L, 1, "%c");
  time_t t = (time_t)(luaL_optnumber(L, 2, -1));
  struct tm *stm;
  if (t == (time_t)(-1))  /* no time given? */
    t = time(NULL);  /* use current time */
  .........
  
You will note that argument 2 is pulled from the
Lua stack, immediately coerced to a time_t, then
compared with a (time_t) -1. This seems to me a bit
like travelling from Manhattan to Newark via
Montreal. So I altered the code to

static int io_date (lua_State *L) {
  const char *s = luaL_optstring(L, 1, "%c");
  time_t t;
  struct tm *stm;  
  const lua_Number n = luaL_optnumber(L, 2, -1);
  if (n == -1)
    t = time(NULL);
  else
    t = (time_t) n;   
  ........
 
which seems to have cured the problem. Anybody see
any snags I may have overlooked?
     
-- 
Gavin Wraith (gavin@wraith.u-net.com)
Home page: http://www.wraith.u-net.com/