From 670110b171fd7e405447ee1d4df101e4e4e86e60 Mon Sep 17 00:00:00 2001 From: Vincent Sanders Date: Wed, 8 Mar 2017 10:54:17 +0000 Subject: use robust handling of time_t value (de)serialisation The previous implementation was wrong on systems where time_t was not an int type. This changes urldb to use the portable implementation netsurf has available. --- content/urldb.c | 41 +++++++++++++++++++++++++++++++++++------ 1 file changed, 35 insertions(+), 6 deletions(-) (limited to 'content') diff --git a/content/urldb.c b/content/urldb.c index 7a17450ef..b578f2c13 100644 --- a/content/urldb.c +++ b/content/urldb.c @@ -311,6 +311,27 @@ static struct bloom_filter *url_bloom; #define BLOOM_SIZE (1024 * 32) +/** + * write a time_t to a file portably + * + * \param fp File to write to + * \param val the unix time value to output + * \return NSERROR_OK on success + */ +static nserror urldb_write_timet(FILE *fp, time_t val) +{ + int use; + char op[32]; + + use = nsc_sntimet(op, 32, &val); + if (use == 0) { + fprintf(fp, "%i\n", (int)val); + } else { + fprintf(fp, "%.*s\n", use, op); + } + return NSERROR_OK; +} + /** * Write paths associated with a host * @@ -383,9 +404,14 @@ urldb_write_paths(const struct path_data *parent, /** \todo handle fragments? */ - fprintf(fp, "%i\n%i\n%i\n", p->urld.visits, - (int)p->urld.last_visit, - (int)p->urld.type); + /* number of visits */ + fprintf(fp, "%i\n", p->urld.visits); + + /* time entry was last used */ + urldb_write_timet(fp, p->urld.last_visit); + + /* entry type */ + fprintf(fp, "%i\n", (int)p->urld.type); fprintf(fp, "\n"); @@ -2806,10 +2832,13 @@ nserror urldb_load(const char *filename) if (p) p->urld.visits = (unsigned int)atoi(s); - if (!fgets(s, MAXIMUM_URL_LENGTH, fp)) + /* entry last use time */ + if (!fgets(s, MAXIMUM_URL_LENGTH, fp)) { break; - if (p) - p->urld.last_visit = (time_t)atoi(s); + } + if (p) { + nsc_snptimet(s, strlen(s) - 1, &p->urld.last_visit); + } if (!fgets(s, MAXIMUM_URL_LENGTH, fp)) break; -- cgit v1.2.3