Use safer rstime_t instead of time_t

Avoid problems to serialization on different platforms, without breaking
nested STL containers serialization.

The conversion have been made with sed, and checked with grep, plus
kdiff3 visual ispection, plus rutime tests, so it should be fine.
This commit is contained in:
Gioacchino Mazzurco 2018-10-07 01:34:05 +02:00
parent 41aa675a9b
commit 329050a9c2
No known key found for this signature in database
GPG key ID: A1FBCA3872E87051
223 changed files with 930 additions and 911 deletions

View file

@ -287,7 +287,7 @@ bool setRawString(void *data, uint32_t size, uint32_t *offset, const std::string
return true;
}
bool getRawTimeT(const void *data,uint32_t size,uint32_t *offset,time_t& t)
bool getRawTimeT(const void *data,uint32_t size,uint32_t *offset,rstime_t& t)
{
uint64_t T ;
bool res = getRawUInt64(data,size,offset,&T) ;
@ -295,7 +295,7 @@ bool getRawTimeT(const void *data,uint32_t size,uint32_t *offset,time_t& t)
return res ;
}
bool setRawTimeT(void *data, uint32_t size, uint32_t *offset, const time_t& t)
bool setRawTimeT(void *data, uint32_t size, uint32_t *offset, const rstime_t& t)
{
return setRawUInt64(data,size,offset,t) ;
}

View file

@ -25,7 +25,9 @@
#include <string>
#include <stdlib.h>
#include <stdint.h>
#include <retroshare/rsids.h>
#include "retroshare/rsids.h"
#include "util/rstime.h"
/*******************************************************************
* This is at the lowlevel packing routines. They are usually
@ -63,8 +65,8 @@ uint32_t getRawStringSize(const std::string &outStr);
bool getRawString(const void *data, uint32_t size, uint32_t *offset, std::string &outStr);
bool setRawString(void *data, uint32_t size, uint32_t *offset, const std::string &inStr);
bool getRawTimeT(const void *data, uint32_t size, uint32_t *offset, time_t& outStr);
bool setRawTimeT(void *data, uint32_t size, uint32_t *offset, const time_t& inStr);
bool getRawTimeT(const void *data, uint32_t size, uint32_t *offset, rstime_t& outTime);
bool setRawTimeT(void *data, uint32_t size, uint32_t *offset, const rstime_t& inTime);
#endif

View file

@ -25,7 +25,7 @@
*/
#include <stdexcept>
#include <time.h>
#include "util/rstime.h"
#include "serialiser/rsbaseserial.h"
#include "serialiser/rsmailtransportitems.h"
#include "serialiser/rstlvbase.h"

View file

@ -75,7 +75,7 @@ class Counter
static int total_rsitem_mallocs = 0 ;
static int total_rsitem_frees = 0 ;
static int total_rsitem_freed = 0 ;
static time_t last_time = 0 ;
static rstime_t last_time = 0 ;
void *RsItem::operator new(size_t s)
{
@ -85,7 +85,7 @@ void *RsItem::operator new(size_t s)
++size_hits[ s ].v() ;
time_t now = time(NULL);
rstime_t now = time(NULL);
++nb_rsitem_creations ;
total_rsitem_mallocs += s ;

View file

@ -85,7 +85,7 @@
//
// private:
// uint32_t count ; // example of an int type. All int sizes are supported
// std::map<uint32_t,time_t> update_times ; // example of a std::map. All std containers are supported.
// std::map<uint32_t,rstime_t> update_times ; // example of a std::map. All std containers are supported.
// RsTlvSecurityKey key ; // example of a TlvItem class.
// BIGNUM *dh_key; // example of a class that needs its own serializer (see below)
// };

View file

@ -27,9 +27,9 @@
#include "serialiser/rsserializable.h"
#include "util/radix64.h"
#include "util/rsprint.h"
#include "util/rstime.h"
#include <iomanip>
#include <time.h>
#include <string>
#include <typeinfo> // for typeid
@ -93,7 +93,7 @@ template<> bool RsTypeSerializer::serialize(uint8_t data[], uint32_t size, uint3
{
return setRawUInt64(data,size,&offset,member);
}
template<> bool RsTypeSerializer::serialize(uint8_t data[], uint32_t size, uint32_t &offset, const time_t& member)
template<> bool RsTypeSerializer::serialize(uint8_t data[], uint32_t size, uint32_t &offset, const rstime_t& member)
{
return setRawTimeT(data,size,&offset,member);
}
@ -127,7 +127,7 @@ template<> bool RsTypeSerializer::deserialize(const uint8_t data[], uint32_t siz
{
return getRawUInt64(data,size,&offset,&member);
}
template<> bool RsTypeSerializer::deserialize(const uint8_t data[], uint32_t size, uint32_t &offset, time_t& member)
template<> bool RsTypeSerializer::deserialize(const uint8_t data[], uint32_t size, uint32_t &offset, rstime_t& member)
{
return getRawTimeT(data,size,&offset,member);
}
@ -158,7 +158,7 @@ template<> uint32_t RsTypeSerializer::serial_size(const uint64_t& /* member*/)
{
return 8;
}
template<> uint32_t RsTypeSerializer::serial_size(const time_t& /* member*/)
template<> uint32_t RsTypeSerializer::serial_size(const rstime_t& /* member*/)
{
return 8;
}
@ -187,9 +187,9 @@ template<> void RsTypeSerializer::print_data(const std::string& n, const uint64_
{
std::cerr << " [uint64_t ] " << n << ": " << std::to_string(V) << std::endl;
}
template<> void RsTypeSerializer::print_data(const std::string& n, const time_t& V)
template<> void RsTypeSerializer::print_data(const std::string& n, const rstime_t& V)
{
std::cerr << " [time_t ] " << n << ": " << V << " (" << time(NULL)-V << " secs ago)" << std::endl;
std::cerr << " [rstime_t ] " << n << ": " << V << " (" << time(NULL)-V << " secs ago)" << std::endl;
}
#define SIMPLE_TO_JSON_DEF(T) \
@ -210,23 +210,7 @@ template<> bool RsTypeSerializer::to_JSON( const std::string& memberName, \
SIMPLE_TO_JSON_DEF(bool)
SIMPLE_TO_JSON_DEF(int32_t)
template<> bool RsTypeSerializer::to_JSON( const std::string& memberName,
const time_t& member, RsJson& jDoc )
{
rapidjson::Document::AllocatorType& allocator = jDoc.GetAllocator();
rapidjson::Value key;
key.SetString(memberName.c_str(), memberName.length(), allocator);
// without this compilation may break depending on how time_t is defined
int64_t tValue = member;
rapidjson::Value value(tValue);
jDoc.AddMember(key, value, allocator);
return true;
}
SIMPLE_TO_JSON_DEF(rstime_t)
SIMPLE_TO_JSON_DEF(uint8_t)
SIMPLE_TO_JSON_DEF(uint16_t)
@ -254,12 +238,12 @@ bool RsTypeSerializer::from_JSON( const std::string& memberName,
}
template<> /*static*/
bool RsTypeSerializer::from_JSON( const std::string& memberName, time_t& member,
bool RsTypeSerializer::from_JSON( const std::string& memberName, rstime_t& member,
RsJson& jDoc )
{
SAFE_GET_JSON_V();
ret = ret && v.IsInt();
if(ret) member = v.GetInt();
ret = ret && v.IsInt64();
if(ret) member = v.GetInt64();
return ret;
}