Merge pull request #455 from lionsoul2014/opt_missing_search_return

optimize the ptr checking and return empty string for missing data
This commit is contained in:
Leon / 狮子的魂 2026-03-03 13:45:13 +08:00 committed by GitHub
commit 0ad8031754
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
16 changed files with 65 additions and 10 deletions

View File

@ -196,6 +196,13 @@ XDB_PUBLIC(int) xdb_search(xdb_searcher_t *xdb, const bytes_ip_t *ip_bytes, int
}
// printf("s_ptr=%u, e_ptr=%u\n", s_ptr, e_ptr);
// @Note: ptr validate, zero ptr means source data missing
// so we could just stop here and return an empty string.
if (s_ptr == 0 || e_ptr == 0) {
xdb_region_buffer_empty(region);
return err;
}
// binary search to get the final region info
// segment_buffer = xdb_malloc(seg_index_size);
seg_index_size = xdb->version->segment_index_size;

View File

@ -2,7 +2,7 @@
<PropertyGroup>
<id>IP2Region.Net</id>
<version>3.0.1</version>
<version>3.0.2</version>
<title>IP2Region.Net</title>
<authors>Alan Lee;Argo Zhang(argo@live.ca)</authors>
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>

View File

@ -75,6 +75,14 @@ public class Searcher(CachePolicy cachePolicy, string xdbPath) : ISearcher
var sPtr = BinaryPrimitives.ReadUInt32LittleEndian(vector.Span);
var ePtr = BinaryPrimitives.ReadUInt32LittleEndian(vector.Span.Slice(4));
// @Note: ptr validate, zero ptr means source data missing
// so we could just stop here and return an empty string.
if (sPtr == 0 || ePtr == 0)
{
return "";
}
var length = ipBytes.Length;
var indexSize = length * 2 + 6;
var l = 0;

View File

@ -132,6 +132,11 @@ func (s *Searcher) Search(ip []byte) (string, error) {
}
// fmt.Printf("sPtr=%d, ePtr=%d\n", sPtr, ePtr)
// @Note: ptr validate, zero ptr means source data missing
// so we could just stop here and return an empty string.
if sPtr == 0 || ePtr == 0 {
return "", nil
}
// binary search the segment index to get the region
var bytes, dBytes = len(ip), len(ip) << 1

View File

@ -10,7 +10,7 @@
<dependency>
<groupId>org.lionsoul</groupId>
<artifactId>ip2region</artifactId>
<version>3.3.5</version>
<version>3.3.6</version>
</dependency>
```

View File

@ -9,7 +9,7 @@
<dependency>
<groupId>org.lionsoul</groupId>
<artifactId>ip2region</artifactId>
<version>3.3.5</version>
<version>3.3.6</version>
</dependency>
```

View File

@ -4,7 +4,7 @@
<groupId>org.lionsoul</groupId>
<artifactId>ip2region</artifactId>
<version>3.3.5</version>
<version>3.3.6</version>
<packaging>jar</packaging>
<name>ip2region</name>

View File

@ -138,6 +138,11 @@ public class Searcher {
}
// System.out.printf("sPtr: %d, ePtr: %d\n", sPtr, ePtr);
// @Note: ptr validate, zero ptr means source data missing
// so we could just stop here and return an empty string.
if (sPtr == 0 || ePtr == 0) {
return "";
}
// binary search the segment index block to get the region info
final int bytes = ip.length, dBytes = ip.length << 1;

View File

@ -1,6 +1,6 @@
{
"name": "ip2region.js",
"version": "3.1.7",
"version": "3.1.8",
"description": "official javascript binding for ip2region with both IPv4 and IPv6 supported ",
"type": "module",
"main": "index.js",

View File

@ -66,6 +66,12 @@ export class Searcher {
}
// console.log(`sPtr: ${sPtr}, ePtr: ${ePtr}`);
// @Note: ptr validate, zero ptr means source data missing
// so we could just stop here and return an empty string.
if (sPtr == 0 || ePtr == 0) {
return "";
}
// binary search the segment index block to get the region info
const bytes = ipBytes.length, dBytes = ipBytes.length << 1;
const indexSize = this.version.indexSize;

View File

@ -129,6 +129,13 @@ function xdb:search(ip_bytes)
end
-- print(string.format("s_ptr: %d, e_ptr: %d", s_ptr, e_ptr))
-- @Note: ptr validate, zero ptr means source data missing
-- so we could just stop here and return an empty string.
if s_ptr == 0 or e_ptr == 0 then
return "", nil
end
-- binary search to get the data
local index_size, ip_sub_compare = version.index_size, version.ip_sub_compare
local bytes, d_bytes = version.bytes, version.bytes << 1

View File

@ -504,6 +504,12 @@ class Searcher {
}
// printf("sPtr: %d, ePtr: %d\n", $sPtr, $ePtr);
// @Note: ptr validate, zero ptr means source data missing
// so we could just stop here and return an empty string.
if ($sPtr == 0 || $ePtr == 0) {
return "";
}
[$bytes, $dBytes] = [strlen($ipBytes), strlen($ipBytes) << 1];
// binary search the segment index to get the region info

View File

@ -68,6 +68,11 @@ class Searcher(object):
e_ptr = util.le_get_uint32(buff, 4)
# print("s_ptr: {}, e_ptr: {}".format(s_ptr, e_ptr))
# @Note: ptr validate, zero ptr means source data missing
# so we could just stop here and return an empty string.
if s_ptr == 0 or e_ptr == 0:
return ""
# binary search the segment index block to get the region info
_bytes, _d_bytes = len(ip_bytes), len(ip_bytes) << 1
index_size = self.version.index_size

View File

@ -2,7 +2,7 @@
setuptools.setup(
name="py-ip2region",
version="3.0.3",
version="3.0.4",
description="ip2region official python binding with both IPv4 and IPv6 supported",
long_description=open("README.md", encoding='utf-8').read(),
long_description_content_type='text/markdown',

View File

@ -1,6 +1,6 @@
[package]
name = "ip2region"
version = "0.2.0"
version = "0.2.1"
edition = "2024"
rust-version = "1.89.0"
description = "The rust binding for ip2region"

View File

@ -68,6 +68,12 @@ impl Searcher {
let end_ptr =
u32::from_le_bytes(vector_index[start_point + 4..start_point + 8].try_into()?) as usize;
// @Note: ptr validate, zero ptr means source data missing
// so we could just stop here and return an empty string.
if start_ptr == 0 || end_ptr == 0 {
return Ok(String::new())
}
// Binary search the segment index to get the region
let segment_index_size = self.header.segment_index_size();
let ip_bytes_len = self.header.ip_bytes_len();