From 3579235602638631f5d6e10e18567291d5de1350 Mon Sep 17 00:00:00 2001 From: Haru Date: Thu, 23 Oct 2014 18:12:31 +0200 Subject: Added some validation checks in the geoip parse function This fixes the warning on some compilers, about the fread return value being ignored. Signed-off-by: Haru --- src/char/inter.c | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) (limited to 'src/char') diff --git a/src/char/inter.c b/src/char/inter.c index e5a7d411c..f676b8716 100644 --- a/src/char/inter.c +++ b/src/char/inter.c @@ -483,19 +483,19 @@ void geoip_init(void) { geoip.active = true; db = fopen("./db/GeoIP.dat","rb"); - if( db == NULL ) { + if (db == NULL) { ShowError("geoip_readdb: Error reading GeoIP.dat!\n"); geoip_final(false); return; } fno = fileno(db); - if( fstat(fno, &bufa) < 0 ) { + if (fstat(fno, &bufa) < 0) { ShowError("geoip_readdb: Error stating GeoIP.dat! Error %d\n", errno); geoip_final(false); return; } geoip.cache = aMalloc( (sizeof(geoip.cache) * bufa.st_size) ); - if( fread(geoip.cache, sizeof(unsigned char), bufa.st_size, db) != bufa.st_size ) { + if (fread(geoip.cache, sizeof(unsigned char), bufa.st_size, db) != bufa.st_size) { ShowError("geoip_cache: Couldn't read all elements!\n"); fclose(db); geoip_final(false); @@ -504,10 +504,15 @@ void geoip_init(void) { // Search database type fseek(db, -3l, SEEK_END); - for( i = 0; i < GEOIP_STRUCTURE_INFO_MAX_SIZE; i++ ) { - fread(delim, sizeof(delim[0]), 3, db); - if( delim[0] == 255 && delim[1] == 255 && delim[2] == 255 ) { - fread(&db_type, sizeof(db_type), 1, db); + for (i = 0; i < GEOIP_STRUCTURE_INFO_MAX_SIZE; i++) { + if (fread(delim, sizeof(delim[0]), 3, db) != 3) { + db_type = 0; + break; + } + if (delim[0] == 255 && delim[1] == 255 && delim[2] == 255) { + if (fread(&db_type, sizeof(db_type), 1, db) != 1) { + db_type = 0; + } break; } else { fseek(db, -4l, SEEK_CUR); @@ -516,8 +521,8 @@ void geoip_init(void) { fclose(db); - if( db_type != 1 ) { - if( db_type ) + if (db_type != 1) { + if (db_type) ShowError("geoip_init(): Database type is not supported %d!\n", db_type); else ShowError("geoip_init(): GeoIP is corrupted!\n"); -- cgit v1.2.3-70-g09d2