From 015a15f2e0afdb6a5160fdcf1e53e09d1b4a9e8c Mon Sep 17 00:00:00 2001 From: Lion Date: Wed, 29 Jun 2022 09:47:09 +0800 Subject: [PATCH] safe the type convertion --- binding/c/xdb_searcher.c | 8 ++++---- binding/c/xdb_searcher.h | 10 +--------- 2 files changed, 5 insertions(+), 13 deletions(-) diff --git a/binding/c/xdb_searcher.c b/binding/c/xdb_searcher.c index fd94145..7ec46a9 100644 --- a/binding/c/xdb_searcher.c +++ b/binding/c/xdb_searcher.c @@ -74,8 +74,8 @@ XDB_PUBLIC(int) xdb_search(xdb_searcher_t *xdb, unsigned int ip, char *region_bu xdb->io_count = 0; // locate the segment index block based on the vector index - il0 = (ip >> 24) & 0xFF; - il1 = (ip >> 16) & 0xFF; + il0 = ((int) (ip >> 24)) & 0xFF; + il1 = ((int) (ip >> 16)) & 0xFF; idx = il0 * xdb_vector_index_cols * xdb_vector_index_size + il1 * xdb_vector_index_size; if (xdb->vector_index != NULL) { s_ptr = get_unsigned_int(xdb->vector_index, idx); @@ -96,7 +96,7 @@ XDB_PUBLIC(int) xdb_search(xdb_searcher_t *xdb, unsigned int ip, char *region_bu // printf("s_ptr=%u, e_ptr=%u\n", s_ptr, e_ptr); // binary search to get the final region info data_len = 0, data_ptr = 0; - l = 0, h = (e_ptr - s_ptr) / xdb_segment_index_size; + l = 0, h = ((int) (e_ptr - s_ptr)) / xdb_segment_index_size; while (l <= h) { m = (l + h) >> 1; p = s_ptr + m * xdb_segment_index_size; @@ -284,7 +284,7 @@ XDB_PUBLIC(unsigned int) get_unsigned_int(const char *buffer, int offset) { } // get unsigned short (2bytes) from a specified buffer start from the specified offset -XDB_PUBLIC(unsigned int) get_unsigned_short(const char *buffer, int offset) { +XDB_PUBLIC(int) get_unsigned_short(const char *buffer, int offset) { return ( ((buffer[offset ]) & 0x000000FF) | ((buffer[offset+1] << 8) & 0x0000FF00) diff --git a/binding/c/xdb_searcher.h b/binding/c/xdb_searcher.h index 2ce0347..665f5d9 100644 --- a/binding/c/xdb_searcher.h +++ b/binding/c/xdb_searcher.h @@ -23,14 +23,6 @@ # define XDB_LINUX #endif -// memory allocation error -#define XDB_ALLOCATE_ERROR(func, bytes) \ -do { \ - printf(": Allocate Error In Function <%s> For %lu Bytes.\n", func, (unsigned long int) bytes); \ - return NULL; \ -} while (0); - - #define xdb_calloc( _blocks, _bytes ) calloc( _blocks, _bytes ) #define xdb_malloc( _bytes ) malloc( _bytes ) #define xdb_free( _ptr ) free( _ptr ) @@ -112,7 +104,7 @@ XDB_PUBLIC(char *) xdb_load_content_from_file(char *); XDB_PUBLIC(unsigned int) get_unsigned_int(const char *, int); // get unsigned short (2bytes) from a specified buffer start from the specified offset with little-endian -XDB_PUBLIC(unsigned int) get_unsigned_short(const char *, int); +XDB_PUBLIC(int) get_unsigned_short(const char *, int); // check the specified string ip and convert it to an unsigned int XDB_PUBLIC(int) check_ip(const char *, unsigned int *);