[Xapian-devel] Replace atoi and atol with strtol strtoul:Need Help

Olly Betts olly at survex.com
Mon Dec 15 23:04:35 GMT 2014


On Tue, Dec 16, 2014 at 02:32:31AM +0530, Priyank Bhatt wrote:
> I am working on replacing atoi () and atol() functions with strtol() and
> strtoul() . I came across many files which uses statement  like these
> time_t secs= atoi(data_span.c_str()), here time_t Datatype is not known but
> wikipedia says that it is integer so is it necessary to replace  atoi with
> strtol over here ??

The time_t type is a standard one - ISO C only says it's a "arithmetic
type" (so it could potentially be a double) but POSIX says it's an
"integer type":

http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/sys_types.h.html

While that would allow it to be unsigned, in practice it seems to be a
signed integer, and it is 64 bit on modern systems (since a signed 32
bit integer with the Unix epoch can only represent dates up to 2038).

You missed out a relevant part of line, which in full is:

xapian-applications/omega/date.cc:      time_t secs = atoi(date_span.c_str()) * (24 * 60 * 60);

So in this case, date_span is a number of days, so converting it via a
long is reasonable - even if long is 32 bits that can still represent a
span of 5.8 million years.  You'd want to make sure the multiplication
happens in type time_t though.

If we were actually converting a number of seconds, you'd want to use
strtoll(), at least if sizeof(long) < 8.  C++11 includes strtoll(), and
we decided to require a C++11 compiler for 1.3.x, so there's no need to
worry whether strtoll() is available.

> And is their any document which helps me what each file function does like
> date.cc,cgiparams.cc,omega.cc,query.cc.

There should be a short comment summarising the purpose of each file at
the top, for example:

| /* date.cc: date range parsing routines for omega

A few files may still lack these, but most have them.

Cheers,
    Olly



More information about the Xapian-devel mailing list