In this case now undef will be returned.
Signed-off-by: Stefan Schantl stefan.schantl@ipfire.org --- src/perl/Location.xs | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-)
diff --git a/src/perl/Location.xs b/src/perl/Location.xs index ab27f0b..cdbc7e7 100644 --- a/src/perl/Location.xs +++ b/src/perl/Location.xs @@ -92,27 +92,23 @@ get_license(db) # # Lookup functions # -char* +SV* lookup_country_code(db, address) struct loc_database* db; char* address;
CODE: + RETVAL = &PL_sv_undef; + // Lookup network struct loc_network *network; int err = loc_database_lookup_from_string(db, address, &network); - if (err) { - croak("Could not look up for %s\n", address); - } - - // Extract the country code - const char* country_code = loc_network_get_country_code(network); - loc_network_unref(network); + if (!err) { + // Extract the country code + const char* country_code = loc_network_get_country_code(network); + RETVAL = newSVpv(country_code, strlen(country_code));
- if (country_code) { - RETVAL = strdup(country_code); - } else { - RETVAL = NULL; + loc_network_unref(network); } OUTPUT: RETVAL
Signed-off-by: Stefan Schantl stefan.schantl@ipfire.org --- src/perl/t/Location.t | 6 ++++++ 1 file changed, 6 insertions(+)
diff --git a/src/perl/t/Location.t b/src/perl/t/Location.t index b583af4..55a18f3 100644 --- a/src/perl/t/Location.t +++ b/src/perl/t/Location.t @@ -36,3 +36,9 @@ ok($description eq "This is a geo location database", "Test 3 - Get Database Des
my $country_code = &Location::lookup_country_code($db, $address); ok($country_code eq "DE", "Test 4 - Lookup country code for $address"); + +$country_code = &Location::lookup_country_code($db, "1.1.1.1"); +if(defined($country_code)) { fail("Test 5 - Lookup country code for address not in Database."); } + +$country_code = &Location::lookup_country_code($db, "a.b.c.d"); +if(defined($country_code)) { fail("Test 6 - Lookup country code for invalid address.") }