Skip site navigation (1)Skip section navigation (2)
Date:      13 Dec 2011 05:12:20 -0000
From:      "John Levine" <johnl@iecc.com>
To:        freebsd-questions@freebsd.org
Subject:   What's wrong with this code?
Message-ID:  <20111213051220.45894.qmail@joyce.lan>

next in thread | raw e-mail | index | archive | help
This tiny routine is in a .so loadable module I use.  (It's part of the mailfront
SMTP daemon.)

static const char* date_string(void)
{
  static char datebuf[64];
  time_t now = time(0);
  struct tm* tm = gmtime(&now);
  strftime(datebuf, sizeof datebuf - 1, "%d %b %Y %H:%M:%S -0000", tm);
  return datebuf;
}

I was getting bogus dates.  Running it under GDB, time() is returning
-1, and setting errno to 22, which is EINVAL.  Changing the call to
time to time(NULL) or time(&now) made no difference.

I changed it to a call to gettimeofday(), which works fine.  But what
could the problem have been?  When I splice this routine into a tiny
test program that calls it and prints out the result, it works fine.

The obvious problem, since it's in a .so, is that it's linking to something other than
the system library time() function, but I did an nm on the .so, and it said this,
which sure looks like the system time() function to me:

                 U time@@FBSD_1.0

Setting a breakpoint in gdb gets a complaint about trying to set a breakpoint in /lib/libc.so.7.

Any ideas what the problem was?

R's,
John







Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20111213051220.45894.qmail>