chrislim2888 / IP2Location-C-Library

IP2Location C library enables the user to find the country, region, city, coordinates, zip code, time zone, ISP, domain name, connection type, area code, weather station code, weather station name, mobile, usage types, etc that any IP address or hostname originates from.
https://www.ip2location.com
MIT License
61 stars 30 forks source link

Read functions revert to reading from shm when file pointer is NULL #21

Closed DemonTPx closed 4 years ago

DemonTPx commented 5 years ago

I noticed something weird with the read functions where would be read from shm, even though IP2LOCATION_FILE_IO is configured as the access type when the file pointer is NULL.

Consider this snipper from the IP2Location_read32 function in IP2Loc_DBInterface.c:

if (DB_access_type == IP2LOCATION_FILE_IO && handle != NULL)
{
    // reads from file
}
else
{
    // reads from shm
}

I think the handle != NULL check should be done one level deeper. However, I'm not sure what the function should return in that case.

The following code would obviously result in a segfault:

auto db = IP2Location_open("data/IP-COUNTRY-SAMPLE.BIN");
db->filehandle = NULL;

auto record = IP2Location_get_all(db, "127.0.0.1");