util: fix int warnings

Ticket: 4516
pull/7112/head
Philippe Antoine 4 years ago committed by Victor Julien
parent 1e65324940
commit b3ab126394

@ -109,7 +109,7 @@
#define TCP_GET_OFFSET(p) TCP_GET_RAW_OFFSET((p)->tcph) #define TCP_GET_OFFSET(p) TCP_GET_RAW_OFFSET((p)->tcph)
#define TCP_GET_X2(p) TCP_GET_RAW_X2((p)->tcph) #define TCP_GET_X2(p) TCP_GET_RAW_X2((p)->tcph)
#define TCP_GET_HLEN(p) (TCP_GET_OFFSET((p)) << 2) #define TCP_GET_HLEN(p) ((uint8_t)(TCP_GET_OFFSET((p)) << 2))
#define TCP_GET_SRC_PORT(p) TCP_GET_RAW_SRC_PORT((p)->tcph) #define TCP_GET_SRC_PORT(p) TCP_GET_RAW_SRC_PORT((p)->tcph)
#define TCP_GET_DST_PORT(p) TCP_GET_RAW_DST_PORT((p)->tcph) #define TCP_GET_DST_PORT(p) TCP_GET_RAW_DST_PORT((p)->tcph)
#define TCP_GET_SEQ(p) TCP_GET_RAW_SEQ((p)->tcph) #define TCP_GET_SEQ(p) TCP_GET_RAW_SEQ((p)->tcph)

@ -175,8 +175,8 @@ extern uint16_t g_vlan_mask;
extern bool g_disable_hashing; extern bool g_disable_hashing;
#include <ctype.h> #include <ctype.h>
#define u8_tolower(c) tolower((uint8_t)(c)) #define u8_tolower(c) ((uint8_t)tolower((uint8_t)(c)))
#define u8_toupper(c) toupper((uint8_t)(c)) #define u8_toupper(c) ((uint8_t)toupper((uint8_t)(c)))
void EngineStop(void); void EngineStop(void);
void EngineDone(void); void EngineDone(void);

@ -296,7 +296,7 @@ void AffinitySetupLoadFromConfig()
*/ */
int AffinityGetNextCPU(ThreadsAffinityType *taf) int AffinityGetNextCPU(ThreadsAffinityType *taf)
{ {
int ncpu = 0; uint16_t ncpu = 0;
#if !defined __CYGWIN__ && !defined OS_WIN32 && !defined __OpenBSD__ && !defined sun #if !defined __CYGWIN__ && !defined OS_WIN32 && !defined __OpenBSD__ && !defined sun
int iter = 0; int iter = 0;
SCMutexLock(&taf->taf_mutex); SCMutexLock(&taf->taf_mutex);

@ -116,7 +116,7 @@ uint32_t DecodeBase64(uint8_t *dest, const uint8_t *src, uint32_t len,
/* For each alpha-numeric letter in the source array, find the numeric /* For each alpha-numeric letter in the source array, find the numeric
* value */ * value */
b64[bbidx++] = (val > 0 ? val : 0); b64[bbidx++] = (val > 0 ? (uint8_t)val : 0);
/* Decode every 4 base64 bytes into 3 ascii bytes */ /* Decode every 4 base64 bytes into 3 ascii bytes */
if (bbidx == B64_BLOCK) { if (bbidx == B64_BLOCK) {

@ -121,7 +121,7 @@ uint16_t UtilCpuGetNumProcessorsOnline(void)
return UINT16_MAX; return UINT16_MAX;
} }
return nprocs; return (uint16_t)nprocs;
#elif OS_WIN32 #elif OS_WIN32
return UtilCpuGetNumProcessorsConfigured(); return UtilCpuGetNumProcessorsConfigured();
#else #else

@ -118,7 +118,7 @@ typedef struct SCLogOPIfaceCtx_ {
SCLogOPIface iface; SCLogOPIface iface;
int16_t use_color; int16_t use_color;
int16_t type; SCLogOPType type;
/* the output file to be used if the interface is SC_LOG_IFACE_FILE */ /* the output file to be used if the interface is SC_LOG_IFACE_FILE */
const char *file; const char *file;

@ -54,7 +54,6 @@ FixChecksum(uint16_t sum, uint16_t old, uint16_t new)
l = sum + old - new; l = sum + old - new;
l = (l >> 16) + (l & 65535); l = (l >> 16) + (l & 65535);
l = l & 65535;
return l; return (uint16_t)(l & 65535);
} }

@ -126,7 +126,7 @@ int SCHInfoAddHostOSInfo(const char *host_os, const char *host_os_ip_range, int
struct in_addr *ipv4_addr = NULL; struct in_addr *ipv4_addr = NULL;
struct in6_addr *ipv6_addr = NULL; struct in6_addr *ipv6_addr = NULL;
char *netmask_str = NULL; char *netmask_str = NULL;
int netmask_value = 0; uint8_t netmask_value = 0;
int *user_data = NULL; int *user_data = NULL;
bool recursive = false; bool recursive = false;
@ -185,7 +185,8 @@ int SCHInfoAddHostOSInfo(const char *host_os, const char *host_os_ip_range, int
SCRadixAddKeyIPV4((uint8_t *)ipv4_addr, sc_hinfo_tree, SCRadixAddKeyIPV4((uint8_t *)ipv4_addr, sc_hinfo_tree,
(void *)user_data); (void *)user_data);
} else { } else {
if (StringParseI32RangeCheck(&netmask_value, 10, 0, (const char *)netmask_str, 0, 32) < 0) { if (StringParseU8RangeCheck(&netmask_value, 10, 0, (const char *)netmask_str, 0, 32) <
0) {
SCLogError(SC_ERR_INVALID_IP_NETBLOCK, "Invalid IPV4 Netblock"); SCLogError(SC_ERR_INVALID_IP_NETBLOCK, "Invalid IPV4 Netblock");
SCHInfoFreeUserDataOSPolicy(user_data); SCHInfoFreeUserDataOSPolicy(user_data);
SCFree(ipv4_addr); SCFree(ipv4_addr);
@ -210,7 +211,8 @@ int SCHInfoAddHostOSInfo(const char *host_os, const char *host_os_ip_range, int
SCRadixAddKeyIPV6((uint8_t *)ipv6_addr, sc_hinfo_tree, SCRadixAddKeyIPV6((uint8_t *)ipv6_addr, sc_hinfo_tree,
(void *)user_data); (void *)user_data);
} else { } else {
if (StringParseI32RangeCheck(&netmask_value, 10, 0, (const char *)netmask_str, 0, 128) < 0) { if (StringParseU8RangeCheck(&netmask_value, 10, 0, (const char *)netmask_str, 0, 128) <
0) {
SCLogError(SC_ERR_INVALID_IP_NETBLOCK, "Invalid IPV6 Netblock"); SCLogError(SC_ERR_INVALID_IP_NETBLOCK, "Invalid IPV6 Netblock");
SCHInfoFreeUserDataOSPolicy(user_data); SCHInfoFreeUserDataOSPolicy(user_data);
SCFree(ipv6_addr); SCFree(ipv6_addr);

@ -206,7 +206,7 @@ int SetIfaceFlags(const char *ifname, int flags)
ifr.ifr_flags = flags & 0xffff; ifr.ifr_flags = flags & 0xffff;
ifr.ifr_flagshigh = flags >> 16; ifr.ifr_flagshigh = flags >> 16;
#else #else
ifr.ifr_flags = flags; ifr.ifr_flags = (uint16_t)flags;
#endif #endif
if (ioctl(fd, SIOCSIFFLAGS, &ifr) == -1) { if (ioctl(fd, SIOCSIFFLAGS, &ifr) == -1) {

@ -401,7 +401,7 @@ SCLogOpenFileFp(const char *path, const char *append_setting, uint32_t mode)
filename, strerror(errno)); filename, strerror(errno));
} else { } else {
if (mode != 0) { if (mode != 0) {
int r = chmod(filename, mode); int r = chmod(filename, (mode_t)mode);
if (r < 0) { if (r < 0) {
SCLogWarning(SC_WARN_CHMOD, "Could not chmod %s to %o: %s", SCLogWarning(SC_WARN_CHMOD, "Could not chmod %s to %o: %s",
filename, mode, strerror(errno)); filename, mode, strerror(errno));
@ -494,9 +494,7 @@ SCConfLogOpenGeneric(ConfNode *conf,
const char *filemode = ConfNodeLookupChildValue(conf, "filemode"); const char *filemode = ConfNodeLookupChildValue(conf, "filemode");
uint32_t mode = 0; uint32_t mode = 0;
if (filemode != NULL && if (filemode != NULL && StringParseUint32(&mode, 8, (uint16_t)strlen(filemode), filemode) > 0) {
StringParseUint32(&mode, 8, strlen(filemode),
filemode) > 0) {
log_ctx->filemode = mode; log_ctx->filemode = mode;
} }

@ -59,6 +59,7 @@
#include "util-unittest-helper.h" #include "util-unittest-helper.h"
#include "util-memcmp.h" #include "util-memcmp.h"
#include "util-memcpy.h" #include "util-memcpy.h"
#include "util-validate.h"
void SCACBSInitCtx(MpmCtx *); void SCACBSInitCtx(MpmCtx *);
void SCACBSInitThreadCtx(MpmCtx *, MpmThreadCtx *); void SCACBSInitThreadCtx(MpmCtx *, MpmThreadCtx *);
@ -495,7 +496,8 @@ static inline void SCACBSCreateDeltaTable(MpmCtx *mpm_ctx)
memset(&q, 0, sizeof(StateQueue)); memset(&q, 0, sizeof(StateQueue));
for (ascii_code = 0; ascii_code < 256; ascii_code++) { for (ascii_code = 0; ascii_code < 256; ascii_code++) {
SC_AC_BS_STATE_TYPE_U16 temp_state = ctx->goto_table[0][ascii_code]; DEBUG_VALIDATE_BUG_ON(ctx->goto_table[0][ascii_code] > UINT16_MAX);
SC_AC_BS_STATE_TYPE_U16 temp_state = (uint16_t)ctx->goto_table[0][ascii_code];
ctx->state_table_u16[0][ascii_code] = temp_state; ctx->state_table_u16[0][ascii_code] = temp_state;
if (temp_state != 0) if (temp_state != 0)
SCACBSEnqueue(&q, temp_state); SCACBSEnqueue(&q, temp_state);
@ -508,7 +510,8 @@ static inline void SCACBSCreateDeltaTable(MpmCtx *mpm_ctx)
int32_t temp_state = ctx->goto_table[r_state][ascii_code]; int32_t temp_state = ctx->goto_table[r_state][ascii_code];
if (temp_state != SC_AC_BS_FAIL) { if (temp_state != SC_AC_BS_FAIL) {
SCACBSEnqueue(&q, temp_state); SCACBSEnqueue(&q, temp_state);
ctx->state_table_u16[r_state][ascii_code] = temp_state; DEBUG_VALIDATE_BUG_ON(temp_state > UINT16_MAX);
ctx->state_table_u16[r_state][ascii_code] = (uint16_t)temp_state;
} else { } else {
ctx->state_table_u16[r_state][ascii_code] = ctx->state_table_u16[r_state][ascii_code] =
ctx->state_table_u16[ctx->failure_table[r_state]][ascii_code]; ctx->state_table_u16[ctx->failure_table[r_state]][ascii_code];

@ -78,6 +78,7 @@
#include "util-unittest-helper.h" #include "util-unittest-helper.h"
#include "util-memcmp.h" #include "util-memcmp.h"
#include "util-memcpy.h" #include "util-memcpy.h"
#include "util-validate.h"
#include "util-mpm-ac-ks.h" #include "util-mpm-ac-ks.h"
#if __BYTE_ORDER == __LITTLE_ENDIAN #if __BYTE_ORDER == __LITTLE_ENDIAN
@ -198,7 +199,8 @@ static void SCACTileInitTranslateTable(SCACTileCtx *ctx)
} }
if (ctx->alpha_hist[i]) { if (ctx->alpha_hist[i]) {
ctx->alphabet_size++; ctx->alphabet_size++;
ctx->translate_table[i] = ctx->alphabet_size; DEBUG_VALIDATE_BUG_ON(ctx->alphabet_size > UINT8_MAX);
ctx->translate_table[i] = (uint8_t)ctx->alphabet_size;
} else } else
ctx->translate_table[i] = 0; ctx->translate_table[i] = 0;
} }
@ -554,7 +556,8 @@ static void SCACTileSetState1Byte(SCACTileCtx *ctx, int state, int aa,
int next_state, int outputs) int next_state, int outputs)
{ {
uint8_t *state_table = (uint8_t*)ctx->state_table; uint8_t *state_table = (uint8_t*)ctx->state_table;
uint8_t encoded_next_state = next_state; DEBUG_VALIDATE_BUG_ON(next_state < 0 || next_state > UINT8_MAX);
uint8_t encoded_next_state = (uint8_t)next_state;
if (next_state == SC_AC_TILE_FAIL) { if (next_state == SC_AC_TILE_FAIL) {
FatalError(SC_ERR_FATAL, "Error FAIL state in output"); FatalError(SC_ERR_FATAL, "Error FAIL state in output");
@ -573,7 +576,8 @@ static void SCACTileSetState2Bytes(SCACTileCtx *ctx, int state, int aa,
int next_state, int outputs) int next_state, int outputs)
{ {
uint16_t *state_table = (uint16_t*)ctx->state_table; uint16_t *state_table = (uint16_t*)ctx->state_table;
uint16_t encoded_next_state = next_state; DEBUG_VALIDATE_BUG_ON(next_state < 0 || next_state > UINT16_MAX);
uint16_t encoded_next_state = (uint16_t)next_state;
if (next_state == SC_AC_TILE_FAIL) { if (next_state == SC_AC_TILE_FAIL) {
FatalError(SC_ERR_FATAL, "Error FAIL state in output"); FatalError(SC_ERR_FATAL, "Error FAIL state in output");
@ -929,7 +933,7 @@ int SCACTilePreparePatterns(MpmCtx *mpm_ctx)
/* Now make the copies of the no-case strings. */ /* Now make the copies of the no-case strings. */
for (i = 0; i < mpm_ctx->pattern_cnt; i++) { for (i = 0; i < mpm_ctx->pattern_cnt; i++) {
if (!(ctx->parray[i]->flags & MPM_PATTERN_FLAG_NOCASE)) { if (!(ctx->parray[i]->flags & MPM_PATTERN_FLAG_NOCASE)) {
uint32_t len = ctx->parray[i]->len; uint16_t len = ctx->parray[i]->len;
uint32_t space = ((len + 7) / 8) * 8; uint32_t space = ((len + 7) / 8) * 8;
memcpy(string_space, ctx->parray[i]->original_pat, len); memcpy(string_space, ctx->parray[i]->original_pat, len);
ctx->pattern_list[i].cs = string_space; ctx->pattern_list[i].cs = string_space;
@ -1248,7 +1252,8 @@ uint32_t SCACTileSearchLarge(const SCACTileSearchCtx *ctx, MpmThreadCtx *mpm_thr
for (i = 0; i < buflen; i++) { for (i = 0; i < buflen; i++) {
state = state_table_u32[state & 0x00FFFFFF][xlate[buf[i]]]; state = state_table_u32[state & 0x00FFFFFF][xlate[buf[i]]];
if (SCHECK(state)) { if (SCHECK(state)) {
matches = CheckMatch(ctx, pmq, buf, buflen, state, i, matches, mpm_bitarray); DEBUG_VALIDATE_BUG_ON(state < 0 || state > UINT16_MAX);
matches = CheckMatch(ctx, pmq, buf, buflen, (uint16_t)state, i, matches, mpm_bitarray);
} }
} /* for (i = 0; i < buflen; i++) */ } /* for (i = 0; i < buflen; i++) */

@ -59,6 +59,7 @@
#include "util-memcmp.h" #include "util-memcmp.h"
#include "util-mpm-ac.h" #include "util-mpm-ac.h"
#include "util-memcpy.h" #include "util-memcpy.h"
#include "util-validate.h"
void SCACInitCtx(MpmCtx *); void SCACInitCtx(MpmCtx *);
void SCACInitThreadCtx(MpmCtx *, MpmThreadCtx *); void SCACInitThreadCtx(MpmCtx *, MpmThreadCtx *);
@ -578,7 +579,8 @@ static inline void SCACCreateDeltaTable(MpmCtx *mpm_ctx)
memset(&q, 0, sizeof(StateQueue)); memset(&q, 0, sizeof(StateQueue));
for (ascii_code = 0; ascii_code < 256; ascii_code++) { for (ascii_code = 0; ascii_code < 256; ascii_code++) {
SC_AC_STATE_TYPE_U16 temp_state = ctx->goto_table[0][ascii_code]; DEBUG_VALIDATE_BUG_ON(ctx->goto_table[0][ascii_code] > UINT16_MAX);
SC_AC_STATE_TYPE_U16 temp_state = (uint16_t)ctx->goto_table[0][ascii_code];
ctx->state_table_u16[0][ascii_code] = temp_state; ctx->state_table_u16[0][ascii_code] = temp_state;
if (temp_state != 0) if (temp_state != 0)
SCACEnqueue(&q, temp_state); SCACEnqueue(&q, temp_state);
@ -591,7 +593,8 @@ static inline void SCACCreateDeltaTable(MpmCtx *mpm_ctx)
int32_t temp_state = ctx->goto_table[r_state][ascii_code]; int32_t temp_state = ctx->goto_table[r_state][ascii_code];
if (temp_state != SC_AC_FAIL) { if (temp_state != SC_AC_FAIL) {
SCACEnqueue(&q, temp_state); SCACEnqueue(&q, temp_state);
ctx->state_table_u16[r_state][ascii_code] = temp_state; DEBUG_VALIDATE_BUG_ON(temp_state > UINT16_MAX);
ctx->state_table_u16[r_state][ascii_code] = (uint16_t)temp_state;
} else { } else {
ctx->state_table_u16[r_state][ascii_code] = ctx->state_table_u16[r_state][ascii_code] =
ctx->state_table_u16[ctx->failure_table[r_state]][ascii_code]; ctx->state_table_u16[ctx->failure_table[r_state]][ascii_code];

@ -46,7 +46,7 @@
#endif #endif
MpmTableElmt mpm_table[MPM_TABLE_SIZE]; MpmTableElmt mpm_table[MPM_TABLE_SIZE];
uint16_t mpm_default_matcher; uint8_t mpm_default_matcher;
/** /**
* \brief Register a new Mpm Context. * \brief Register a new Mpm Context.
@ -197,7 +197,7 @@ void MpmInitThreadCtx(MpmThreadCtx *mpm_thread_ctx, uint16_t matcher)
mpm_table[matcher].InitThreadCtx(NULL, mpm_thread_ctx); mpm_table[matcher].InitThreadCtx(NULL, mpm_thread_ctx);
} }
void MpmInitCtx (MpmCtx *mpm_ctx, uint16_t matcher) void MpmInitCtx(MpmCtx *mpm_ctx, uint8_t matcher)
{ {
mpm_ctx->mpm_type = matcher; mpm_ctx->mpm_type = matcher;
mpm_table[matcher].InitCtx(mpm_ctx); mpm_table[matcher].InitCtx(mpm_ctx);

@ -170,7 +170,7 @@ typedef struct MpmTableElmt_ {
} MpmTableElmt; } MpmTableElmt;
extern MpmTableElmt mpm_table[MPM_TABLE_SIZE]; extern MpmTableElmt mpm_table[MPM_TABLE_SIZE];
extern uint16_t mpm_default_matcher; extern uint8_t mpm_default_matcher;
struct DetectEngineCtx_; struct DetectEngineCtx_;
@ -188,7 +188,7 @@ void PmqFree(PrefilterRuleStore *);
void MpmTableSetup(void); void MpmTableSetup(void);
void MpmRegisterTests(void); void MpmRegisterTests(void);
void MpmInitCtx(MpmCtx *mpm_ctx, uint16_t matcher); void MpmInitCtx(MpmCtx *mpm_ctx, uint8_t matcher);
void MpmInitThreadCtx(MpmThreadCtx *mpm_thread_ctx, uint16_t); void MpmInitThreadCtx(MpmThreadCtx *mpm_thread_ctx, uint16_t);
int MpmAddPatternCS(struct MpmCtx_ *mpm_ctx, uint8_t *pat, uint16_t patlen, int MpmAddPatternCS(struct MpmCtx_ *mpm_ctx, uint8_t *pat, uint16_t patlen,

@ -380,7 +380,7 @@ static char ProtoNameHashCompareFunc(void *data1, uint16_t datalen1, void *data2
return len1 == len2 && memcmp(p1->name, p2->name, len1) == 0; return len1 == len2 && memcmp(p1->name, p2->name, len1) == 0;
} }
static void ProtoNameAddEntry(const char *proto_name, const int proto_number) static void ProtoNameAddEntry(const char *proto_name, const uint8_t proto_number)
{ {
ProtoNameHashEntry *proto_ent = SCCalloc(1, sizeof(ProtoNameHashEntry)); ProtoNameHashEntry *proto_ent = SCCalloc(1, sizeof(ProtoNameHashEntry));
if (!proto_ent) { if (!proto_ent) {
@ -423,15 +423,15 @@ void SCProtoNameInit(void)
FatalError(SC_ERR_HASH_TABLE_INIT, "Unable to initialize protocol name/number table"); FatalError(SC_ERR_HASH_TABLE_INIT, "Unable to initialize protocol name/number table");
} }
for (uint32_t i = 0; i < ARRAY_SIZE(known_proto); i++) { for (uint16_t i = 0; i < ARRAY_SIZE(known_proto); i++) {
if (known_proto[i]) { if (known_proto[i]) {
ProtoNameAddEntry(known_proto[i], i); ProtoNameAddEntry(known_proto[i], (uint8_t)i);
} }
} }
for (uint32_t i = 0; i < ARRAY_SIZE(proto_aliases); i++) { for (uint16_t i = 0; i < ARRAY_SIZE(proto_aliases); i++) {
if (proto_aliases[i]) { if (proto_aliases[i]) {
ProtoNameAddEntry(proto_aliases[i], i); ProtoNameAddEntry(proto_aliases[i], (uint8_t)i);
} }
} }
} }

@ -488,8 +488,8 @@ void SCRadixReleaseRadixTree(SCRadixTree *tree)
* *
* \retval node Pointer to the newly created node * \retval node Pointer to the newly created node
*/ */
static SCRadixNode *SCRadixAddKey(uint8_t *key_stream, uint16_t key_bitlen, static SCRadixNode *SCRadixAddKey(
SCRadixTree *tree, void *user, uint8_t netmask) uint8_t *key_stream, uint8_t key_bitlen, SCRadixTree *tree, void *user, uint8_t netmask)
{ {
SCRadixNode *node = NULL; SCRadixNode *node = NULL;
SCRadixNode *new_node = NULL; SCRadixNode *new_node = NULL;
@ -501,11 +501,11 @@ static SCRadixNode *SCRadixAddKey(uint8_t *key_stream, uint16_t key_bitlen,
uint8_t *stream = NULL; uint8_t *stream = NULL;
uint8_t bitlen = 0; uint8_t bitlen = 0;
int check_bit = 0; uint16_t check_bit = 0;
int differ_bit = 0; uint16_t differ_bit = 0;
int i = 0; uint16_t i = 0;
int j = 0; uint16_t j = 0;
int temp = 0; int temp = 0;
if (tree == NULL) { if (tree == NULL) {
@ -683,14 +683,14 @@ static SCRadixNode *SCRadixAddKey(uint8_t *key_stream, uint16_t key_bitlen,
node->netmasks[node->netmask_cnt - 1] = netmask; node->netmasks[node->netmask_cnt - 1] = netmask;
for (i = node->netmask_cnt - 2; i >= 0; i--) { for (i = node->netmask_cnt - 1; i > 0; i--) {
if (netmask < node->netmasks[i]) { if (netmask < node->netmasks[i - 1]) {
node->netmasks[i + 1] = netmask; node->netmasks[i] = netmask;
break; break;
} }
node->netmasks[i + 1] = node->netmasks[i]; node->netmasks[i] = node->netmasks[i - 1];
node->netmasks[i] = netmask; node->netmasks[i - 1] = netmask;
} }
} }
} else { } else {
@ -811,40 +811,20 @@ static SCRadixNode *SCRadixAddKey(uint8_t *key_stream, uint16_t key_bitlen,
node->netmasks[node->netmask_cnt - 1] = netmask; node->netmasks[node->netmask_cnt - 1] = netmask;
for (i = node->netmask_cnt - 2; i >= 0; i--) { for (i = node->netmask_cnt - 1; i > 0; i--) {
if (netmask < node->netmasks[i]) { if (netmask < node->netmasks[i - 1]) {
node->netmasks[i + 1] = netmask; node->netmasks[i] = netmask;
break; break;
} }
node->netmasks[i + 1] = node->netmasks[i]; node->netmasks[i] = node->netmasks[i - 1];
node->netmasks[i] = netmask; node->netmasks[i - 1] = netmask;
} }
} }
return new_node; return new_node;
} }
/**
* \brief Adds a new generic key to the Radix tree
*
* \param key_stream Data that has to be added to the Radix tree
* \param key_bitlen The bitlen of the the above stream. For example if the
* stream is the string "abcd", the bitlen would be 32
* \param tree Pointer to the Radix tree
* \param user Pointer to the user data that has to be associated with the
* key
*
* \retval node Pointer to the newly created node
*/
SCRadixNode *SCRadixAddKeyGeneric(uint8_t *key_stream, uint16_t key_bitlen,
SCRadixTree *tree, void *user)
{
SCRadixNode *node = SCRadixAddKey(key_stream, key_bitlen, tree, user, 255);
return node;
}
/** /**
* \brief Adds a new IPV4 address to the Radix tree * \brief Adds a new IPV4 address to the Radix tree
* *
@ -1482,7 +1462,7 @@ static inline SCRadixNode *SCRadixFindKeyIPNetblock(
* \param exact_match The key to be searched is an ip address * \param exact_match The key to be searched is an ip address
* \param netmask Netmask used during exact match * \param netmask Netmask used during exact match
*/ */
static SCRadixNode *SCRadixFindKey(uint8_t *key_stream, uint16_t key_bitlen, uint8_t netmask, static SCRadixNode *SCRadixFindKey(uint8_t *key_stream, uint8_t key_bitlen, uint8_t netmask,
SCRadixTree *tree, int exact_match, void **user_data_result) SCRadixTree *tree, int exact_match, void **user_data_result)
{ {
if (tree == NULL || tree->head == NULL) if (tree == NULL || tree->head == NULL)
@ -1493,9 +1473,6 @@ static SCRadixNode *SCRadixFindKey(uint8_t *key_stream, uint16_t key_bitlen, uin
int bytes = 0; int bytes = 0;
uint8_t tmp_stream[255]; uint8_t tmp_stream[255];
if (key_bitlen > 255)
return NULL;
memset(tmp_stream, 0, 255); memset(tmp_stream, 0, 255);
memcpy(tmp_stream, key_stream, key_bitlen / 8); memcpy(tmp_stream, key_stream, key_bitlen / 8);
@ -1538,19 +1515,6 @@ static SCRadixNode *SCRadixFindKey(uint8_t *key_stream, uint16_t key_bitlen, uin
return ret; return ret;
} }
/**
* \brief Checks if a key is present in the tree
*
* \param key_stream Data that has to be found in the Radix tree
* \param key_bitlen The bitlen of the the above stream.
* \param tree Pointer to the Radix tree instance
*/
SCRadixNode *SCRadixFindKeyGeneric(uint8_t *key_stream, uint16_t key_bitlen,
SCRadixTree *tree, void **user_data_result)
{
return SCRadixFindKey(key_stream, key_bitlen, 0, tree, 1, user_data_result); /* TODO netmask? */
}
/** /**
* \brief Checks if an IPV4 address is present in the tree * \brief Checks if an IPV4 address is present in the tree
* *

@ -66,7 +66,7 @@ typedef struct SCRadixNode_ {
uint16_t pad0; uint16_t pad0;
/* total no of netmasks that are registered under this node */ /* total no of netmasks that are registered under this node */
int netmask_cnt; uint16_t netmask_cnt;
/* holds a list of netmaks that come under this node in the tree */ /* holds a list of netmaks that come under this node in the tree */
uint8_t *netmasks; uint8_t *netmasks;
@ -101,7 +101,6 @@ void SCRadixChopIPAddressAgainstNetmask(uint8_t *, uint8_t, uint16_t);
SCRadixTree *SCRadixCreateRadixTree(void (*Free)(void*), void (*PrintData)(void*)); SCRadixTree *SCRadixCreateRadixTree(void (*Free)(void*), void (*PrintData)(void*));
void SCRadixReleaseRadixTree(SCRadixTree *); void SCRadixReleaseRadixTree(SCRadixTree *);
SCRadixNode *SCRadixAddKeyGeneric(uint8_t *, uint16_t, SCRadixTree *, void *);
SCRadixNode *SCRadixAddKeyIPV4(uint8_t *, SCRadixTree *, void *); SCRadixNode *SCRadixAddKeyIPV4(uint8_t *, SCRadixTree *, void *);
SCRadixNode *SCRadixAddKeyIPV6(uint8_t *, SCRadixTree *, void *); SCRadixNode *SCRadixAddKeyIPV6(uint8_t *, SCRadixTree *, void *);
SCRadixNode *SCRadixAddKeyIPV4Netblock(uint8_t *, SCRadixTree *, void *, SCRadixNode *SCRadixAddKeyIPV4Netblock(uint8_t *, SCRadixTree *, void *,
@ -117,8 +116,6 @@ void SCRadixRemoveKeyIPV4(uint8_t *, SCRadixTree *);
void SCRadixRemoveKeyIPV6Netblock(uint8_t *, SCRadixTree *, uint8_t); void SCRadixRemoveKeyIPV6Netblock(uint8_t *, SCRadixTree *, uint8_t);
void SCRadixRemoveKeyIPV6(uint8_t *, SCRadixTree *); void SCRadixRemoveKeyIPV6(uint8_t *, SCRadixTree *);
SCRadixNode *SCRadixFindKeyGeneric(uint8_t *, uint16_t, SCRadixTree *, void **);
SCRadixNode *SCRadixFindKeyIPV4ExactMatch(uint8_t *, SCRadixTree *, void **); SCRadixNode *SCRadixFindKeyIPV4ExactMatch(uint8_t *, SCRadixTree *, void **);
SCRadixNode *SCRadixFindKeyIPV4Netblock(uint8_t *, SCRadixTree *, uint8_t, void **); SCRadixNode *SCRadixFindKeyIPV4Netblock(uint8_t *, SCRadixTree *, uint8_t, void **);
SCRadixNode *SCRadixFindKeyIPV4BestMatch(uint8_t *, SCRadixTree *, void **); SCRadixNode *SCRadixFindKeyIPV4BestMatch(uint8_t *, SCRadixTree *, void **);

@ -38,6 +38,7 @@
#include "util-debug.h" #include "util-debug.h"
#include "util-error.h" #include "util-error.h"
#include "util-memcpy.h" #include "util-memcpy.h"
#include "util-validate.h"
static int PreBmGs(const uint8_t *x, uint16_t m, uint16_t *bmGs); static int PreBmGs(const uint8_t *x, uint16_t m, uint16_t *bmGs);
static void PreBmBc(const uint8_t *x, uint16_t m, uint16_t *bmBc); static void PreBmBc(const uint8_t *x, uint16_t m, uint16_t *bmBc);
@ -137,7 +138,7 @@ void BoyerMooreCtxDeInit(BmCtx *bmctx)
*/ */
static void PreBmBc(const uint8_t *x, uint16_t m, uint16_t *bmBc) static void PreBmBc(const uint8_t *x, uint16_t m, uint16_t *bmBc)
{ {
int32_t i; uint16_t i;
for (i = 0; i < 256; ++i) { for (i = 0; i < 256; ++i) {
bmBc[i] = m; bmBc[i] = m;
@ -168,7 +169,8 @@ static void BoyerMooreSuffixes(const uint8_t *x, uint16_t m, uint16_t *suff)
f = i; f = i;
while (g >= 0 && x[g] == x[g + m - 1 - f]) while (g >= 0 && x[g] == x[g + m - 1 - f])
--g; --g;
suff[i] = f - g; DEBUG_VALIDATE_BUG_ON(f - g < 0 || f - g > UINT16_MAX);
suff[i] = (uint16_t)(f - g);
} }
} }
} }
@ -197,10 +199,10 @@ static int PreBmGs(const uint8_t *x, uint16_t m, uint16_t *bmGs)
if (i == -1 || suff[i] == i + 1) if (i == -1 || suff[i] == i + 1)
for (; j < m - 1 - i; ++j) for (; j < m - 1 - i; ++j)
if (bmGs[j] == m) if (bmGs[j] == m)
bmGs[j] = m - 1 - i; bmGs[j] = (uint16_t)(m - 1 - i);
for (i = 0; i <= m - 2; ++i) for (i = 0; i <= m - 2; ++i)
bmGs[m - 1 - suff[i]] = m - 1 - i; bmGs[m - 1 - suff[i]] = (uint16_t)(m - 1 - i);
return 0; return 0;
} }
@ -214,14 +216,14 @@ static int PreBmGs(const uint8_t *x, uint16_t m, uint16_t *bmGs)
*/ */
static void PreBmBcNocase(const uint8_t *x, uint16_t m, uint16_t *bmBc) static void PreBmBcNocase(const uint8_t *x, uint16_t m, uint16_t *bmBc)
{ {
int32_t i; uint16_t i;
for (i = 0; i < 256; ++i) { for (i = 0; i < 256; ++i) {
bmBc[i] = m; bmBc[i] = m;
} }
for (i = 0; i < m - 1; ++i) { for (i = 0; i < m - 1; ++i) {
bmBc[u8_tolower((unsigned char)x[i])] = m - 1 - i; bmBc[u8_tolower(x[i])] = m - 1 - i;
bmBc[u8_toupper((unsigned char)x[i])] = m - 1 - i; bmBc[u8_toupper(x[i])] = m - 1 - i;
} }
} }
@ -243,7 +245,8 @@ static void BoyerMooreSuffixesNocase(const uint8_t *x, uint16_t m,
while (g >= 0 && u8_tolower(x[g]) == u8_tolower(x[g + m - 1 - f])) { while (g >= 0 && u8_tolower(x[g]) == u8_tolower(x[g + m - 1 - f])) {
--g; --g;
} }
suff[i] = f - g; DEBUG_VALIDATE_BUG_ON(f - g < 0 || f - g > UINT16_MAX);
suff[i] = (uint16_t)(f - g);
} }
} }
} }
@ -258,7 +261,7 @@ static void BoyerMooreSuffixesNocase(const uint8_t *x, uint16_t m,
*/ */
static void PreBmGsNocase(const uint8_t *x, uint16_t m, uint16_t *bmGs) static void PreBmGsNocase(const uint8_t *x, uint16_t m, uint16_t *bmGs)
{ {
int32_t i, j; uint16_t i, j;
uint16_t suff[m + 1]; uint16_t suff[m + 1];
BoyerMooreSuffixesNocase(x, m, suff); BoyerMooreSuffixesNocase(x, m, suff);
@ -267,11 +270,11 @@ static void PreBmGsNocase(const uint8_t *x, uint16_t m, uint16_t *bmGs)
bmGs[i] = m; bmGs[i] = m;
} }
j = 0; j = 0;
for (i = m - 1; i >= 0; --i) { for (i = m; i > 0; --i) {
if (i == -1 || suff[i] == i + 1) { if (suff[i-1] == i) {
for (; j < m - 1 - i; ++j) { for (; j < m - i; ++j) {
if (bmGs[j] == m) { if (bmGs[j] == m) {
bmGs[j] = m - 1 - i; bmGs[j] = m - i;
} }
} }
} }

@ -231,8 +231,7 @@ static int THashInitConfig(THashTableContext *ctx, const char *cnf_prefix)
GET_VAR(cnf_prefix, "hash-size"); GET_VAR(cnf_prefix, "hash-size");
if ((ConfGet(varname, &conf_val)) == 1) if ((ConfGet(varname, &conf_val)) == 1)
{ {
if (StringParseUint32(&configval, 10, strlen(conf_val), if (StringParseUint32(&configval, 10, (uint16_t)strlen(conf_val), conf_val) > 0) {
conf_val) > 0) {
ctx->config.hash_size = configval; ctx->config.hash_size = configval;
} }
} }
@ -240,8 +239,7 @@ static int THashInitConfig(THashTableContext *ctx, const char *cnf_prefix)
GET_VAR(cnf_prefix, "prealloc"); GET_VAR(cnf_prefix, "prealloc");
if ((ConfGet(varname, &conf_val)) == 1) if ((ConfGet(varname, &conf_val)) == 1)
{ {
if (StringParseUint32(&configval, 10, strlen(conf_val), if (StringParseUint32(&configval, 10, (uint16_t)strlen(conf_val), conf_val) > 0) {
conf_val) > 0) {
ctx->config.prealloc = configval; ctx->config.prealloc = configval;
} else { } else {
WarnInvalidConfEntry(varname, "%"PRIu32, ctx->config.prealloc); WarnInvalidConfEntry(varname, "%"PRIu32, ctx->config.prealloc);

@ -856,7 +856,7 @@ static int ParseThresholdRule(DetectEngineCtx *de_ctx, char *rawstr,
/* TODO: implement option "apply_to" */ /* TODO: implement option "apply_to" */
if (StringParseUint32(&parsed_timeout, 10, strlen(th_timeout), th_timeout) <= 0) { if (StringParseUint32(&parsed_timeout, 10, sizeof(th_timeout), th_timeout) <= 0) {
goto error; goto error;
} }
@ -904,7 +904,7 @@ static int ParseThresholdRule(DetectEngineCtx *de_ctx, char *rawstr,
goto error; goto error;
} }
if (StringParseUint32(&parsed_count, 10, strlen(th_count), th_count) <= 0) { if (StringParseUint32(&parsed_count, 10, sizeof(th_count), th_count) <= 0) {
goto error; goto error;
} }
if (parsed_count == 0) { if (parsed_count == 0) {
@ -912,7 +912,7 @@ static int ParseThresholdRule(DetectEngineCtx *de_ctx, char *rawstr,
goto error; goto error;
} }
if (StringParseUint32(&parsed_seconds, 10, strlen(th_seconds), th_seconds) <= 0) { if (StringParseUint32(&parsed_seconds, 10, sizeof(th_seconds), th_seconds) <= 0) {
goto error; goto error;
} }
@ -935,11 +935,11 @@ static int ParseThresholdRule(DetectEngineCtx *de_ctx, char *rawstr,
break; break;
} }
if (StringParseUint32(&id, 10, strlen(th_sid), th_sid) <= 0) { if (StringParseUint32(&id, 10, sizeof(th_sid), th_sid) <= 0) {
goto error; goto error;
} }
if (StringParseUint32(&gid, 10, strlen(th_gid), th_gid) <= 0) { if (StringParseUint32(&gid, 10, sizeof(th_gid), th_gid) <= 0) {
goto error; goto error;
} }

@ -61,7 +61,7 @@ static SCMutex g_varnamestore_staging_m = SCMUTEX_INITIALIZER;
/** \brief Name2idx mapping structure for flowbits, flowvars and pktvars. */ /** \brief Name2idx mapping structure for flowbits, flowvars and pktvars. */
typedef struct VariableName_ { typedef struct VariableName_ {
char *name; char *name;
uint8_t type; /* flowbit, pktvar, etc */ enum VarTypes type; /* flowbit, pktvar, etc */
uint32_t idx; uint32_t idx;
} VariableName; } VariableName;

Loading…
Cancel
Save