@@ -872,7 +872,7 @@ char * _GeoIP_iso_8859_1__utf8(const char * iso)
872
872
if (p ) {
873
873
while ( ( c = * iso ++ ) ) {
874
874
if (c < 0 ) {
875
- k = 0xc2 ;
875
+ k = ( char ) 0xc2 ;
876
876
if (c >= -64 ) {
877
877
k ++ ;
878
878
}
@@ -959,7 +959,7 @@ static void _setup_segments(GeoIP * gi)
959
959
int i , j , segment_record_length ;
960
960
unsigned char delim [3 ];
961
961
unsigned char buf [LARGE_SEGMENT_RECORD_LENGTH ];
962
- ssize_t silence _UNUSED ;
962
+
963
963
int fno = fileno (gi -> GeoIPDatabase );
964
964
965
965
gi -> databaseSegments = NULL ;
@@ -971,9 +971,13 @@ static void _setup_segments(GeoIP * gi)
971
971
return ;
972
972
}
973
973
for (i = 0 ; i < STRUCTURE_INFO_MAX_SIZE ; i ++ ) {
974
- silence = read (fno , delim , 3 );
974
+ if (read (fno , delim , 3 ) != 3 ) {
975
+ return ;
976
+ }
975
977
if (delim [0 ] == 255 && delim [1 ] == 255 && delim [2 ] == 255 ) {
976
- silence = read (fno , & gi -> databaseType , 1 );
978
+ if (read (fno , & gi -> databaseType , 1 ) != 1 ) {
979
+ return ;
980
+ }
977
981
if (gi -> databaseType >= 106 ) {
978
982
/* backwards compatibility with databases from April 2003 and earlier */
979
983
gi -> databaseType -= 105 ;
@@ -1027,7 +1031,12 @@ static void _setup_segments(GeoIP * gi)
1027
1031
gi -> databaseSegments [0 ] = 0 ;
1028
1032
segment_record_length = SEGMENT_RECORD_LENGTH ;
1029
1033
1030
- silence = read (fno , buf , segment_record_length );
1034
+ if (read (fno , buf ,
1035
+ segment_record_length ) != segment_record_length ) {
1036
+ free (gi -> databaseSegments );
1037
+ gi -> databaseSegments = NULL ;
1038
+ return ;
1039
+ }
1031
1040
for (j = 0 ; j < segment_record_length ; j ++ ) {
1032
1041
gi -> databaseSegments [0 ] += (buf [j ] << (j * 8 ));
1033
1042
}
@@ -1206,7 +1215,7 @@ void _check_mtime(GeoIP *gi)
1206
1215
gi -> index_cache , sizeof (unsigned char ) * idx_size );
1207
1216
if (gi -> index_cache != NULL ) {
1208
1217
if (pread (fileno (gi -> GeoIPDatabase ), gi -> index_cache ,
1209
- idx_size , 0 ) != idx_size ) {
1218
+ idx_size , 0 ) != idx_size ) {
1210
1219
DEBUG_MSGF (
1211
1220
gi -> flags ,
1212
1221
"Error reading file %s where reloading\n" ,
@@ -1234,22 +1243,31 @@ unsigned int _GeoIP_seek_record_v6_gl(GeoIP *gi, geoipv6_t ipnum,
1234
1243
1235
1244
const unsigned char * p ;
1236
1245
int j ;
1237
- ssize_t silence _UNUSED ;
1238
1246
int fno = fileno (gi -> GeoIPDatabase );
1247
+
1248
+ unsigned int record_pair_length = gi -> record_length * 2 ;
1249
+
1239
1250
_check_mtime (gi );
1240
1251
if (GeoIP_teredo (gi ) ) {
1241
1252
__GEOIP_PREPARE_TEREDO (& ipnum );
1242
1253
}
1243
1254
for (depth = 127 ; depth >= 0 ; depth -- ) {
1255
+ unsigned int byte_offset = record_pair_length * offset ;
1256
+ if (byte_offset > gi -> size - record_pair_length ) {
1257
+ /* The pointer is invalid */
1258
+ break ;
1259
+ }
1244
1260
if (gi -> cache == NULL && gi -> index_cache == NULL ) {
1245
1261
/* read from disk */
1246
- silence = pread (fno , stack_buffer , gi -> record_length * 2 ,
1247
- (long )gi -> record_length * 2 * offset );
1262
+ if (pread (fno , stack_buffer , record_pair_length ,
1263
+ (long )byte_offset ) != record_pair_length ) {
1264
+ break ;
1265
+ }
1248
1266
} else if (gi -> index_cache == NULL ) {
1249
1267
/* simply point to record in memory */
1250
- buf = gi -> cache + (long )gi -> record_length * 2 * offset ;
1268
+ buf = gi -> cache + (long )byte_offset ;
1251
1269
} else {
1252
- buf = gi -> index_cache + (long )gi -> record_length * 2 * offset ;
1270
+ buf = gi -> index_cache + (long )byte_offset ;
1253
1271
}
1254
1272
1255
1273
if (GEOIP_CHKBIT_V6 (depth , ipnum .s6_addr )) {
@@ -1325,22 +1343,31 @@ unsigned int _GeoIP_seek_record_gl(GeoIP *gi, unsigned long ipnum,
1325
1343
unsigned char stack_buffer [2 * MAX_RECORD_LENGTH ];
1326
1344
const unsigned char * buf = (gi -> cache == NULL ) ? stack_buffer : NULL ;
1327
1345
unsigned int offset = 0 ;
1328
- ssize_t silence _UNUSED ;
1329
1346
1330
1347
const unsigned char * p ;
1331
1348
int j ;
1332
1349
int fno = fileno (gi -> GeoIPDatabase );
1350
+
1351
+ unsigned int record_pair_length = gi -> record_length * 2 ;
1352
+
1333
1353
_check_mtime (gi );
1334
1354
for (depth = 31 ; depth >= 0 ; depth -- ) {
1355
+ unsigned int byte_offset = record_pair_length * offset ;
1356
+ if (byte_offset > gi -> size - record_pair_length ) {
1357
+ /* The pointer is invalid */
1358
+ break ;
1359
+ }
1335
1360
if (gi -> cache == NULL && gi -> index_cache == NULL ) {
1336
1361
/* read from disk */
1337
- silence = pread (fno , stack_buffer , gi -> record_length * 2 ,
1338
- gi -> record_length * 2 * offset );
1362
+ if (pread (fno , stack_buffer , record_pair_length ,
1363
+ byte_offset ) != record_pair_length ) {
1364
+ break ;
1365
+ }
1339
1366
} else if (gi -> index_cache == NULL ) {
1340
1367
/* simply point to record in memory */
1341
- buf = gi -> cache + (long )gi -> record_length * 2 * offset ;
1368
+ buf = gi -> cache + (long )byte_offset ;
1342
1369
} else {
1343
- buf = gi -> index_cache + (long )gi -> record_length * 2 * offset ;
1370
+ buf = gi -> index_cache + (long )byte_offset ;
1344
1371
}
1345
1372
1346
1373
if (ipnum & (1 << depth )) {
@@ -1508,9 +1535,10 @@ GeoIP * GeoIP_open(const char * filename, int flags)
1508
1535
free (gi );
1509
1536
return NULL ;
1510
1537
}
1538
+
1539
+ gi -> size = buf .st_size ;
1511
1540
if (flags & (GEOIP_MEMORY_CACHE | GEOIP_MMAP_CACHE ) ) {
1512
1541
gi -> mtime = buf .st_mtime ;
1513
- gi -> size = buf .st_size ;
1514
1542
1515
1543
/* MMAP added my Peter Shipley */
1516
1544
if (flags & GEOIP_MMAP_CACHE ) {
@@ -1999,7 +2027,7 @@ char *GeoIP_database_info(GeoIP * gi)
1999
2027
unsigned char buf [3 ];
2000
2028
char * retval ;
2001
2029
int hasStructureInfo = 0 ;
2002
- ssize_t silence _UNUSED ;
2030
+
2003
2031
int fno ;
2004
2032
2005
2033
if (gi == NULL ) {
@@ -2015,7 +2043,9 @@ char *GeoIP_database_info(GeoIP * gi)
2015
2043
2016
2044
/* first get past the database structure information */
2017
2045
for (i = 0 ; i < STRUCTURE_INFO_MAX_SIZE ; i ++ ) {
2018
- silence = read (fno , buf , 3 );
2046
+ if (read (fno , buf , 3 ) != 3 ) {
2047
+ return NULL ;
2048
+ }
2019
2049
if (buf [0 ] == 255 && buf [1 ] == 255 && buf [2 ] == 255 ) {
2020
2050
hasStructureInfo = 1 ;
2021
2051
break ;
@@ -2036,13 +2066,17 @@ char *GeoIP_database_info(GeoIP * gi)
2036
2066
}
2037
2067
2038
2068
for (i = 0 ; i < DATABASE_INFO_MAX_SIZE ; i ++ ) {
2039
- silence = read (fno , buf , 3 );
2069
+ if (read (fno , buf , 3 ) != 3 ) {
2070
+ return NULL ;
2071
+ }
2040
2072
if (buf [0 ] == 0 && buf [1 ] == 0 && buf [2 ] == 0 ) {
2041
2073
retval = malloc (sizeof (char ) * (i + 1 ));
2042
2074
if (retval == NULL ) {
2043
2075
return NULL ;
2044
2076
}
2045
- silence = read (fno , retval , i );
2077
+ if (read (fno , retval , i ) != i ) {
2078
+ return NULL ;
2079
+ }
2046
2080
retval [i ] = '\0' ;
2047
2081
return retval ;
2048
2082
}
@@ -2310,7 +2344,6 @@ char *_get_name_gl(GeoIP * gi, unsigned long ipnum, GeoIPLookup * gl)
2310
2344
char * org_buf , * buf_pointer ;
2311
2345
int record_pointer ;
2312
2346
size_t len ;
2313
- ssize_t silence _UNUSED ;
2314
2347
2315
2348
if (gi -> databaseType != GEOIP_ORG_EDITION &&
2316
2349
gi -> databaseType != GEOIP_ISP_EDITION &&
@@ -2341,9 +2374,10 @@ char *_get_name_gl(GeoIP * gi, unsigned long ipnum, GeoIPLookup * gl)
2341
2374
(2 * gi -> record_length - 1 ) * gi -> databaseSegments [0 ];
2342
2375
2343
2376
if (gi -> cache == NULL ) {
2344
- silence = pread (fileno (
2345
- gi -> GeoIPDatabase ), buf , MAX_ORG_RECORD_LENGTH ,
2346
- record_pointer );
2377
+ if (pread (fileno (gi -> GeoIPDatabase ), buf , MAX_ORG_RECORD_LENGTH ,
2378
+ record_pointer ) == -1 ) {
2379
+ return NULL ;
2380
+ }
2347
2381
if (gi -> charset == GEOIP_CHARSET_UTF8 ) {
2348
2382
org_buf = _GeoIP_iso_8859_1__utf8 ( (const char * )buf );
2349
2383
} else {
@@ -2372,7 +2406,6 @@ char *_get_name_v6_gl(GeoIP * gi, geoipv6_t ipnum, GeoIPLookup * gl)
2372
2406
char * org_buf , * buf_pointer ;
2373
2407
int record_pointer ;
2374
2408
size_t len ;
2375
- ssize_t silence _UNUSED ;
2376
2409
2377
2410
if (
2378
2411
gi -> databaseType != GEOIP_ORG_EDITION_V6 &&
@@ -2400,9 +2433,10 @@ char *_get_name_v6_gl(GeoIP * gi, geoipv6_t ipnum, GeoIPLookup * gl)
2400
2433
(2 * gi -> record_length - 1 ) * gi -> databaseSegments [0 ];
2401
2434
2402
2435
if (gi -> cache == NULL ) {
2403
- silence = pread (fileno (
2404
- gi -> GeoIPDatabase ), buf , MAX_ORG_RECORD_LENGTH ,
2405
- record_pointer );
2436
+ if (pread (fileno (gi -> GeoIPDatabase ), buf , MAX_ORG_RECORD_LENGTH ,
2437
+ record_pointer ) == -1 ) {
2438
+ return NULL ;
2439
+ }
2406
2440
buf [MAX_ORG_RECORD_LENGTH ] = 0 ;
2407
2441
if (gi -> charset == GEOIP_CHARSET_UTF8 ) {
2408
2442
org_buf = _GeoIP_iso_8859_1__utf8 ( (const char * )buf );
0 commit comments