Skip to content
This repository was archived by the owner on Jun 1, 2022. It is now read-only.

Commit 733b36e

Browse files
committed
Merge pull request #63 from maxmind/dave/fix-valgrind-issue
Fix a valgrind memcheck issue and check gettimeofday return value
2 parents 34d142a + f63ff8d commit 733b36e

File tree

4 files changed

+29
-15
lines changed

4 files changed

+29
-15
lines changed

ChangeLog

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66
GitHub #56.)
77
* Fixed segfault when doing a lookup on an empty database. (Fix by NesoK.
88
GitHub #62.)
9+
* Fixed a memcheck error from valgrind in the `_check_mtime`
10+
function. (Reported by yurivct. GitHub #60.)
11+
* Fixed `_check_mtime` to check the return value of `gettimeofday` rather than
12+
just assuming it worked.
13+
914

1015
1.6.5 2015-02-25
1116

apps/geoiplookup.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ void geoiplookup(GeoIP * gi, char *hostname, int i)
290290
}
291291
}else if (GEOIP_COUNTRY_EDITION == i) {
292292
country_id = GeoIP_id_by_ipnum(gi, ipnum);
293-
if (country_id < 0 || country_id >= (int) GeoIP_num_countries()) {
293+
if (country_id < 0 || country_id >= (int)GeoIP_num_countries()) {
294294
printf("%s: Invalid database\n", GeoIPDBDescription[i]);
295295
return;
296296
}

apps/geoiplookup6.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ geoiplookup(GeoIP * gi, char *hostname, int i)
177177
}
178178
}else if (GEOIP_COUNTRY_EDITION_V6 == i) {
179179
country_id = GeoIP_id_by_ipnum_v6(gi, ipnum);
180-
if (country_id < 0 || country_id >= (int) GeoIP_num_countries()) {
180+
if (country_id < 0 || country_id >= (int)GeoIP_num_countries()) {
181181
printf("%s: Invalid database\n", GeoIPDBDescription[i]);
182182
return;
183183
}

libGeoIP/GeoIP.c

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1071,7 +1071,7 @@ static void _setup_segments(GeoIP * gi)
10711071
}
10721072

10731073
static
1074-
int _check_mtime(GeoIP *gi)
1074+
void _check_mtime(GeoIP *gi)
10751075
{
10761076
struct stat buf;
10771077
ssize_t idx_size;
@@ -1088,9 +1088,15 @@ int _check_mtime(GeoIP *gi)
10881088
#if !defined(_WIN32)
10891089
/* stat only has second granularity, so don't
10901090
* call it more than once a second */
1091-
gettimeofday(&t, NULL);
1091+
if (0 != gettimeofday(&t, NULL)) {
1092+
DEBUG_MSGF(gi->flags,
1093+
"Error calling gettimeofday: %s\n",
1094+
strerror(errno));
1095+
return;
1096+
}
1097+
10921098
if (t.tv_sec == gi->last_mtime_check) {
1093-
return 0;
1099+
return;
10941100
}
10951101
gi->last_mtime_check = t.tv_sec;
10961102

@@ -1101,7 +1107,7 @@ int _check_mtime(GeoIP *gi)
11011107
GetSystemTimeAsFileTime(&ft);
11021108
t = FILETIME_TO_USEC(ft) / 1000 / 1000;
11031109
if (t == gi->last_mtime_check) {
1104-
return 0;
1110+
return;
11051111
}
11061112
gi->last_mtime_check = t;
11071113

@@ -1131,7 +1137,7 @@ int _check_mtime(GeoIP *gi)
11311137
DEBUG_MSGF(gi->flags,
11321138
"Out of memory when reloading %s\n",
11331139
gi->file_path);
1134-
return -1;
1140+
return;
11351141
}
11361142
}
11371143
}
@@ -1142,7 +1148,7 @@ int _check_mtime(GeoIP *gi)
11421148
DEBUG_MSGF(gi->flags,
11431149
"Error Opening file %s when reloading\n",
11441150
gi->file_path);
1145-
return -1;
1151+
return;
11461152
}
11471153
gi->mtime = buf.st_mtime;
11481154
gi->size = buf.st_size;
@@ -1152,7 +1158,7 @@ int _check_mtime(GeoIP *gi)
11521158
DEBUG_MSGF(gi->flags,
11531159
"GEOIP_MMAP_CACHE is not supported on WIN32\n");
11541160
gi->cache = 0;
1155-
return -1;
1161+
return;
11561162
#else
11571163
gi->cache =
11581164
mmap(NULL, buf.st_size, PROT_READ, MAP_PRIVATE, fileno(
@@ -1164,7 +1170,7 @@ int _check_mtime(GeoIP *gi)
11641170
gi->file_path);
11651171

11661172
gi->cache = NULL;
1167-
return -1;
1173+
return;
11681174
}
11691175
#endif
11701176
} else if (gi->flags & GEOIP_MEMORY_CACHE) {
@@ -1173,7 +1179,7 @@ int _check_mtime(GeoIP *gi)
11731179
DEBUG_MSGF(gi->flags,
11741180
"Error reading file %s when reloading\n",
11751181
gi->file_path);
1176-
return -1;
1182+
return;
11771183
}
11781184
}
11791185

@@ -1185,14 +1191,14 @@ int _check_mtime(GeoIP *gi)
11851191
if (gi->databaseSegments == NULL) {
11861192
DEBUG_MSGF(gi->flags, "Error reading file %s -- corrupt\n",
11871193
gi->file_path);
1188-
return -1;
1194+
return;
11891195
}
11901196

11911197
idx_size = get_index_size(gi, &buf);
11921198
if (idx_size < 0) {
11931199
DEBUG_MSGF(gi->flags, "Error file %s -- corrupt\n",
11941200
gi->file_path);
1195-
return -1;
1201+
return;
11961202
}
11971203

11981204
if (gi->flags & GEOIP_INDEX_CACHE) {
@@ -1205,14 +1211,14 @@ int _check_mtime(GeoIP *gi)
12051211
gi->flags,
12061212
"Error reading file %s where reloading\n",
12071213
gi->file_path);
1208-
return -1;
1214+
return;
12091215
}
12101216
}
12111217
}
12121218
}
12131219
}
12141220
}
1215-
return 0;
1221+
return;
12161222
}
12171223

12181224
#define ADDR_STR_LEN (8 * 4 + 7 + 1)
@@ -1586,6 +1592,9 @@ GeoIP * GeoIP_open(const char * filename, int flags)
15861592
} else {
15871593
gi->index_cache = NULL;
15881594
}
1595+
1596+
gi->last_mtime_check = 0;
1597+
15891598
return gi;
15901599
}
15911600

0 commit comments

Comments
 (0)