This project github url: https://github.com/armink/FlashDB
#ifdef FDB_USING_TIMESTAMP_64BIT
typedef int64_t fdb_time_t;
#else
typedef int32_t fdb_time_t;
#endif /* FDB_USING_TIMESTAMP_64BIT */
static fdb_err_t tsl_append(fdb_tsdb_t db, fdb_blob_t blob, fdb_time_t *timestamp)
{
fdb_err_t result = FDB_NO_ERR;
fdb_time_t cur_time = timestamp == NULL ? db->get_time() : *timestamp;
/* check the append length, MUST less than the db->max_len */
if(blob->size > db->max_len)
{
FDB_INFO("Warning: append length (%" PRIdMAX ") is more than the db->max_len (%" PRIdMAX "). This tsl will be dropped.rn",
(intmax_t)blob->size, (intmax_t)(db->max_len));
return FDB_WRITE_ERR;
}
/* check the current timestamp, MUST more than the last save timestamp */
if (cur_time <= db->last_time) {
FDB_INFO("Warning: current timestamp (%" PRIdMAX ") is less than or equal to the last save timestamp (%" PRIdMAX "). This tsl will be dropped.rn",
(intmax_t )cur_time, (intmax_t )(db->last_time));
return FDB_WRITE_ERR;
}
result = update_sec_status(db, &db->cur_sec, blob, cur_time);
if (result != FDB_NO_ERR) {
FDB_INFO("Error: update the sector status failed (%d)", result);
return result;
}
/* write the TSL node */
result = write_tsl(db, blob, cur_time);
if (result != FDB_NO_ERR) {
FDB_INFO("Error: write tsl failed (%d)", result);
return result;
}
/* recalculate the current using sector info */
db->cur_sec.end_idx = db->cur_sec.empty_idx;
db->cur_sec.end_time = cur_time;
db->cur_sec.empty_idx += LOG_IDX_DATA_SIZE;
db->cur_sec.empty_data -= FDB_WG_ALIGN(blob->size);
db->cur_sec.remain -= LOG_IDX_DATA_SIZE + FDB_WG_ALIGN(blob->size);
db->last_time = cur_time;
return result;
}
If you are not defined fdb_using_timestamp_64bit. And when CUR_TIME <= DB-> Last_time, it will increase the 32-bit type to 64-bit due to the conversion of compulsory types, resulting in inconsistent printing and expectations.
if (cur_time <= db->last_time) {
FDB_INFO("Warning: current timestamp (%" PRIdMAX ") is less than or equal to the last save timestamp (%" PRIdMAX "). This tsl will be dropped.rn",
(intmax_t )cur_time, (intmax_t )(db->last_time));
return FDB_WRITE_ERR;
}
question:
- I found that there is no good way to be compatible with 32-bit and 64-bit printing. Except write an if else!.
- The type is forced to be converted in this code. Where does the extra 32-bit content come from? Will there be other bugs caused by unexpected memory operations here?