Welcome to the Builder Academy

Question ibt.c - Wrong parameter passed to localtime()

More
Today 05:02 - Today 05:04 #11056 by krell
Line 522 in ibt.c localtime() is passed an address causing a compilation error. When I removed the ampersand the compilation succeeded.

original line:
strftime(timestr, sizeof(timestr), "%c", localtime(&(ibtData->dated)));

altered line:
strftime(timestr, sizeof(timestr), "%c", localtime(ibtData->dated));

man localtime() on my system, OpenBSD, and on Linux, say localtime() should receive a parameter of type const time_t *. Is my interpretation correct?
Last edit: Today 05:04 by krell.

Please Log in or Create an account to join the conversation.

More
Today 18:44 #11057 by thomas
This is interesting. The value is defined as a long:
Code:
long dated; /**< When the IBT what reported */
It should obviously be defined as time_t - it is treated as a time_t everywhere. But time_t is often a long, so it would work. But, in more modern versions of linux, the systems are getting ready for 2038, and thus expanding further; a long is no longer enough space to keep all seconds since Epoch.
In modern time.h, time_t is defined as something other than long, so it breaks. But often it is not, so it works.

Instead of using 0, or long here, we should be using time_t and time(NULL).
Code:
#include <time.h> time_t t = time(NULL); struct tm *local = localtime(&t);

In short; apparently this code (as well as all other code) should be rewritten to avoid using a long for time since epoch. But the change you are proposing will not work. ibt->dated is not a pointer to time_t. It is a long, and the code you are removing banks on this being the same as time_t. The referencing (&(ibt->dated)) works given that prerequisite holds.

Please Log in or Create an account to join the conversation.

More
Today 18:56 #11058 by thomas
Have a look at this PR: github.com/tbamud/tbamud/pull/367

I think it should fix it.

Please Log in or Create an account to join the conversation.

Time to create page: 0.559 seconds