From 839fe044a81f334cddbfeb48dd4bba565ddffa82 Mon Sep 17 00:00:00 2001 From: Dave Rolsky Date: Mon, 27 Jul 2015 11:39:02 -0500 Subject: [PATCH 1/4] Make _check_mtime check the return value of gettimeofday I also made this function return void, since nothing actually looks at it's return value. --- libGeoIP/GeoIP.c | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/libGeoIP/GeoIP.c b/libGeoIP/GeoIP.c index 3233405..063b1a5 100644 --- a/libGeoIP/GeoIP.c +++ b/libGeoIP/GeoIP.c @@ -1071,7 +1071,7 @@ static void _setup_segments(GeoIP * gi) } static -int _check_mtime(GeoIP *gi) +void _check_mtime(GeoIP *gi) { struct stat buf; ssize_t idx_size; @@ -1088,9 +1088,15 @@ int _check_mtime(GeoIP *gi) #if !defined(_WIN32) /* stat only has second granularity, so don't * call it more than once a second */ - gettimeofday(&t, NULL); + if (0 != gettimeofday(&t, NULL)) { + DEBUG_MSGF(gi->flags, + "Error calling gettimeofday: %s\n", + strerror(errno)); + return; + } + if (t.tv_sec == gi->last_mtime_check) { - return 0; + return; } gi->last_mtime_check = t.tv_sec; @@ -1101,7 +1107,7 @@ int _check_mtime(GeoIP *gi) GetSystemTimeAsFileTime(&ft); t = FILETIME_TO_USEC(ft) / 1000 / 1000; if (t == gi->last_mtime_check) { - return 0; + return; } gi->last_mtime_check = t; @@ -1131,7 +1137,7 @@ int _check_mtime(GeoIP *gi) DEBUG_MSGF(gi->flags, "Out of memory when reloading %s\n", gi->file_path); - return -1; + return; } } } @@ -1142,7 +1148,7 @@ int _check_mtime(GeoIP *gi) DEBUG_MSGF(gi->flags, "Error Opening file %s when reloading\n", gi->file_path); - return -1; + return; } gi->mtime = buf.st_mtime; gi->size = buf.st_size; @@ -1152,7 +1158,7 @@ int _check_mtime(GeoIP *gi) DEBUG_MSGF(gi->flags, "GEOIP_MMAP_CACHE is not supported on WIN32\n"); gi->cache = 0; - return -1; + return; #else gi->cache = mmap(NULL, buf.st_size, PROT_READ, MAP_PRIVATE, fileno( @@ -1164,7 +1170,7 @@ int _check_mtime(GeoIP *gi) gi->file_path); gi->cache = NULL; - return -1; + return; } #endif } else if (gi->flags & GEOIP_MEMORY_CACHE) { @@ -1173,7 +1179,7 @@ int _check_mtime(GeoIP *gi) DEBUG_MSGF(gi->flags, "Error reading file %s when reloading\n", gi->file_path); - return -1; + return; } } @@ -1185,14 +1191,14 @@ int _check_mtime(GeoIP *gi) if (gi->databaseSegments == NULL) { DEBUG_MSGF(gi->flags, "Error reading file %s -- corrupt\n", gi->file_path); - return -1; + return; } idx_size = get_index_size(gi, &buf); if (idx_size < 0) { DEBUG_MSGF(gi->flags, "Error file %s -- corrupt\n", gi->file_path); - return -1; + return; } if (gi->flags & GEOIP_INDEX_CACHE) { @@ -1205,14 +1211,14 @@ int _check_mtime(GeoIP *gi) gi->flags, "Error reading file %s where reloading\n", gi->file_path); - return -1; + return; } } } } } } - return 0; + return; } #define ADDR_STR_LEN (8 * 4 + 7 + 1) From 14c40059f1b9e4a4f94bfda9b6bfc3eb41cd5e76 Mon Sep 17 00:00:00 2001 From: Dave Rolsky Date: Mon, 27 Jul 2015 11:43:20 -0500 Subject: [PATCH 2/4] Set the GeoIP struct's last_mtime_check value to 0 This fixes a warning from valgrind reported in GH #60. --- libGeoIP/GeoIP.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/libGeoIP/GeoIP.c b/libGeoIP/GeoIP.c index 063b1a5..279161a 100644 --- a/libGeoIP/GeoIP.c +++ b/libGeoIP/GeoIP.c @@ -1592,6 +1592,9 @@ GeoIP * GeoIP_open(const char * filename, int flags) } else { gi->index_cache = NULL; } + + gi->last_mtime_check = 0; + return gi; } From e2d3e59959cc573f06cf382cc35892a85e3bddbf Mon Sep 17 00:00:00 2001 From: Dave Rolsky Date: Mon, 27 Jul 2015 11:45:52 -0500 Subject: [PATCH 3/4] Add changes for this branch to ChangeLog --- ChangeLog | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/ChangeLog b/ChangeLog index 5b3b347..d0a98df 100644 --- a/ChangeLog +++ b/ChangeLog @@ -6,6 +6,11 @@ GitHub #56.) * Fixed segfault when doing a lookup on an empty database. (Fix by NesoK. GitHub #62.) +* Fixed a memcheck error from valgrind in the `_check_mtime` + function. (Reported by yurivct. GitHub #60.) +* Fixed `_check_mtime` to check the return value of `gettimeofday` rather than + just assuming it worked. + 1.6.5 2015-02-25 From f63ff8dabb66bfc8ceb6d0ffec76a02d4dfe4673 Mon Sep 17 00:00:00 2001 From: Dave Rolsky Date: Mon, 27 Jul 2015 11:46:12 -0500 Subject: [PATCH 4/4] Run uncrustify-all.sh --- apps/geoiplookup.c | 2 +- apps/geoiplookup6.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/apps/geoiplookup.c b/apps/geoiplookup.c index 23984b8..2ad4513 100644 --- a/apps/geoiplookup.c +++ b/apps/geoiplookup.c @@ -290,7 +290,7 @@ void geoiplookup(GeoIP * gi, char *hostname, int i) } }else if (GEOIP_COUNTRY_EDITION == i) { country_id = GeoIP_id_by_ipnum(gi, ipnum); - if (country_id < 0 || country_id >= (int) GeoIP_num_countries()) { + if (country_id < 0 || country_id >= (int)GeoIP_num_countries()) { printf("%s: Invalid database\n", GeoIPDBDescription[i]); return; } diff --git a/apps/geoiplookup6.c b/apps/geoiplookup6.c index 6f9c213..801803f 100644 --- a/apps/geoiplookup6.c +++ b/apps/geoiplookup6.c @@ -177,7 +177,7 @@ geoiplookup(GeoIP * gi, char *hostname, int i) } }else if (GEOIP_COUNTRY_EDITION_V6 == i) { country_id = GeoIP_id_by_ipnum_v6(gi, ipnum); - if (country_id < 0 || country_id >= (int) GeoIP_num_countries()) { + if (country_id < 0 || country_id >= (int)GeoIP_num_countries()) { printf("%s: Invalid database\n", GeoIPDBDescription[i]); return; }