diff --git a/src/tm-threads.c b/src/tm-threads.c index 5da183c311be..7be52a708b57 100644 --- a/src/tm-threads.c +++ b/src/tm-threads.c @@ -700,7 +700,7 @@ static int SetCPUAffinitySet(cpu_set_t *cs) int r = thread_policy_set(mach_thread_self(), THREAD_AFFINITY_POLICY, (void*)cs, THREAD_AFFINITY_POLICY_COUNT); #else - pid_t tid = syscall(SYS_gettid); + pid_t tid = (pid_t)syscall(SYS_gettid); int r = sched_setaffinity(tid, sizeof(cpu_set_t), cs); #endif /* OS_FREEBSD */ diff --git a/src/unix-manager.c b/src/unix-manager.c index 38baaac22011..e592cd67d8e6 100644 --- a/src/unix-manager.c +++ b/src/unix-manager.c @@ -161,7 +161,7 @@ static int UnixNew(UnixCommand * this) addr.sun_family = AF_UNIX; strlcpy(addr.sun_path, sockettarget, sizeof(addr.sun_path)); addr.sun_path[sizeof(addr.sun_path) - 1] = 0; - len = strlen(addr.sun_path) + sizeof(addr.sun_family) + 1; + len = (int)(strlen(addr.sun_path) + sizeof(addr.sun_family) + 1); /* create socket */ this->socket = socket(AF_UNIX, SOCK_STREAM, 0); @@ -335,7 +335,7 @@ static int UnixCommandAccept(UnixCommand *this) json_error_t jerror; int client; int client_version; - int ret; + size_t ret; UnixClient *uclient = NULL; /* accept client socket */ @@ -351,12 +351,7 @@ static int UnixCommandAccept(UnixCommand *this) /* read client version */ buffer[sizeof(buffer)-1] = 0; - ret = recv(client, buffer, sizeof(buffer)-1, 0); - if (ret < 0) { - SCLogInfo("Command server: client doesn't send version"); - close(client); - return 0; - } + ret = recv(client, buffer, sizeof(buffer) - 1, 0); if (ret >= (int)(sizeof(buffer)-1)) { SCLogInfo("Command server: client message is too long, " "disconnect him."); @@ -537,7 +532,7 @@ static int UnixCommandExecute(UnixCommand * this, char *command, UnixClient *cli static void UnixCommandRun(UnixCommand * this, UnixClient *client) { char buffer[4096]; - int ret; + size_t ret; if (client->version <= UNIX_PROTO_V1) { ret = recv(client->fd, buffer, sizeof(buffer) - 1, 0); if (ret <= 0) { @@ -570,7 +565,7 @@ static void UnixCommandRun(UnixCommand * this, UnixClient *client) UnixCommandClose(this, client->fd); return; } - if (ret >= (int)(sizeof(buffer)- offset - 1)) { + if (ret >= (sizeof(buffer) - offset - 1)) { SCLogInfo("Command server: client command is too long, " "disconnect him."); UnixCommandClose(this, client->fd); @@ -582,15 +577,16 @@ static void UnixCommandRun(UnixCommand * this, UnixClient *client) struct timeval tv; fd_set select_set; offset += ret; + int rets = 0; do { FD_ZERO(&select_set); FD_SET(client->fd, &select_set); tv.tv_sec = 0; tv.tv_usec = 200 * 1000; try++; - ret = select(client->fd, &select_set, NULL, NULL, &tv); + rets = select(client->fd, &select_set, NULL, NULL, &tv); /* catch select() error */ - if (ret == -1) { + if (rets == -1) { /* Signal was caught: just ignore it */ if (errno != EINTR) { SCLogInfo("Unix socket: lost connection with client"); @@ -598,8 +594,8 @@ static void UnixCommandRun(UnixCommand * this, UnixClient *client) return; } } - } while (ret == 0 && try < 3); - if (ret > 0) { + } while (rets == 0 && try < 3); + if (rets > 0) { ret = recv(client->fd, buffer + offset, sizeof(buffer) - offset - 1, 0); } @@ -695,7 +691,7 @@ static TmEcode UnixManagerUptimeCommand(json_t *cmd, json_t *server_msg, void *data) { SCEnter(); - int uptime; + int64_t uptime; UnixCommand *ucmd = (UnixCommand *)data; uptime = time(NULL) - ucmd->start_timestamp; diff --git a/src/util-affinity.c b/src/util-affinity.c index 8a224711e884..de3a76428019 100644 --- a/src/util-affinity.c +++ b/src/util-affinity.c @@ -101,7 +101,7 @@ void BuildCpusetWithCallback(const char *name, ConfNode *node, ConfNode *lnode; TAILQ_FOREACH(lnode, &node->head, next) { int i; - long int a,b; + int a, b; int stop = 0; int max = UtilCpuGetNumProcessorsOnline() - 1; if (!strcmp(lnode->val, "all")) { @@ -111,12 +111,12 @@ void BuildCpusetWithCallback(const char *name, ConfNode *node, } else if (strchr(lnode->val, '-') != NULL) { char *sep = strchr(lnode->val, '-'); char *end; - a = strtoul(lnode->val, &end, 10); + a = (int)strtoul(lnode->val, &end, 10); if (end != sep) { SCLogError("%s: invalid cpu range (start invalid): \"%s\"", name, lnode->val); exit(EXIT_FAILURE); } - b = strtol(sep + 1, &end, 10); + b = (int)strtol(sep + 1, &end, 10); if (end != sep + strlen(sep)) { SCLogError("%s: invalid cpu range (end invalid): \"%s\"", name, lnode->val); exit(EXIT_FAILURE); @@ -126,12 +126,12 @@ void BuildCpusetWithCallback(const char *name, ConfNode *node, exit(EXIT_FAILURE); } if (b > max) { - SCLogError("%s: upper bound (%ld) of cpu set is too high, only %d cpu(s)", name, b, + SCLogError("%s: upper bound (%d) of cpu set is too high, only %d cpu(s)", name, b, max + 1); } } else { char *end; - a = strtoul(lnode->val, &end, 10); + a = (int)strtoul(lnode->val, &end, 10); if (end != lnode->val + strlen(lnode->val)) { SCLogError("%s: invalid cpu range (not an integer): \"%s\"", name, lnode->val); exit(EXIT_FAILURE); diff --git a/src/util-byte.c b/src/util-byte.c index d2f39fb2d211..1b06ccd628ba 100644 --- a/src/util-byte.c +++ b/src/util-byte.c @@ -228,7 +228,7 @@ int ByteExtractString(uint64_t *res, int base, size_t len, const char *str, bool return -1; } - return (endptr - ptr); + return (int)(endptr - ptr); } int ByteExtractStringUint64(uint64_t *res, int base, size_t len, const char *str) @@ -531,7 +531,7 @@ int ByteExtractStringSigned(int64_t *res, int base, size_t len, const char *str, //fprintf(stderr, "ByteExtractStringSigned: Extracted base %d: 0x%" PRIx64 "\n", base, *res); - return (endptr - ptr); + return (int)(endptr - ptr); } int ByteExtractStringInt64(int64_t *res, int base, size_t len, const char *str) diff --git a/src/util-classification-config.c b/src/util-classification-config.c index 07f18c2caaaa..c42c2d1a55dd 100644 --- a/src/util-classification-config.c +++ b/src/util-classification-config.c @@ -448,9 +448,9 @@ uint32_t SCClassConfClasstypeHashFunc(HashTable *ht, void *data, uint16_t datale { SCClassConfClasstype *ct = (SCClassConfClasstype *)data; uint32_t hash = 0; - int i = 0; + size_t i = 0; - int len = strlen(ct->classtype); + size_t len = strlen(ct->classtype); for (i = 0; i < len; i++) hash += u8_tolower((unsigned char)(ct->classtype)[i]); @@ -478,8 +478,8 @@ char SCClassConfClasstypeHashCompareFunc(void *data1, uint16_t datalen1, { SCClassConfClasstype *ct1 = (SCClassConfClasstype *)data1; SCClassConfClasstype *ct2 = (SCClassConfClasstype *)data2; - int len1 = 0; - int len2 = 0; + size_t len1 = 0; + size_t len2 = 0; if (ct1 == NULL || ct2 == NULL) return 0; diff --git a/src/util-debug.c b/src/util-debug.c index 56258a034f94..a766a2a118d1 100644 --- a/src/util-debug.c +++ b/src/util-debug.c @@ -286,25 +286,25 @@ static const char *SCTransformModule(const char *module_name, int *dn_len) * app-layer-* */ if (strncmp("tm-", module_name, 3) == 0) { - *dn_len = strlen(module_name) - 3; + *dn_len = (int)strlen(module_name) - 3; return module_name + 3; } else if (strncmp("util-", module_name, 5) == 0) { - *dn_len = strlen(module_name) - 5; + *dn_len = (int)strlen(module_name) - 5; return module_name + 5; } else if (strncmp("source-pcap-file", module_name, 16) == 0) { - *dn_len = strlen("pcap"); + *dn_len = (int)strlen("pcap"); return "pcap"; } else if (strncmp("source-", module_name, 7) == 0) { - *dn_len = strlen(module_name) - 7; + *dn_len = (int)strlen(module_name) - 7; return module_name + 7; } else if (strncmp("runmode-", module_name, 8) == 0) { - *dn_len = strlen(module_name) - 8; + *dn_len = (int)strlen(module_name) - 8; return module_name + 8; } else if (strncmp("app-layer-", module_name, 10) == 0) { - *dn_len = strlen(module_name); + *dn_len = (int)strlen(module_name); return module_name; } else if (strncmp("detect-engine", module_name, 13) == 0) { - *dn_len = strlen("detect"); + *dn_len = (int)strlen("detect"); return "detect"; } @@ -319,9 +319,9 @@ static const char *SCTransformModule(const char *module_name, int *dn_len) } if (seg_cnt < transform_max_segs) - *dn_len = strlen(module_name); + *dn_len = (int)strlen(module_name); else - *dn_len = last - module_name; + *dn_len = (int)(last - module_name); return module_name; } diff --git a/src/util-fmemopen.c b/src/util-fmemopen.c index 412b9783ef3a..9ea536e1878c 100644 --- a/src/util-fmemopen.c +++ b/src/util-fmemopen.c @@ -117,20 +117,20 @@ static fpos_t SeekFn(void *handler, fpos_t offset, int whence) */ static int ReadFn(void *handler, char *buf, int size) { - size_t count = 0; + int count = 0; SCFmem *mem = handler; size_t available = mem->size - mem->pos; int is_eof = 0; if (size < 0) return - 1; - if ((size_t)size > available) { - size = available; + if (size > (int)available) { + size = (int)available; } else { is_eof = 1; } - while (count < (size_t)size) + while (count < size) buf[count++] = mem->buffer[mem->pos++]; if (is_eof == 1) @@ -148,16 +148,16 @@ static int ReadFn(void *handler, char *buf, int size) */ static int WriteFn(void *handler, const char *buf, int size) { - size_t count = 0; + int count = 0; SCFmem *mem = handler; size_t available = mem->size - mem->pos; if (size < 0) return - 1; - if ((size_t)size > available) - size = available; + if (size > (int)available) + size = (int)available; - while (count < (size_t)size) + while (count < size) mem->buffer[mem->pos++] = buf[count++]; return count; diff --git a/src/util-ip.c b/src/util-ip.c index 8792253cc3c9..f57878e0dd86 100644 --- a/src/util-ip.c +++ b/src/util-ip.c @@ -40,8 +40,8 @@ bool IPv4AddressStringIsValid(const char *str) memset(&addr, 0, sizeof(addr)); - uint32_t len = strlen(str); - uint32_t i = 0; + size_t len = strlen(str); + size_t i = 0; for (i = 0; i < len; i++) { if (!(str[i] == '.' || isdigit(str[i]))) { return false; @@ -85,8 +85,8 @@ bool IPv6AddressStringIsValid(const char *str) int sep = 0; bool colon_seen = false; - uint32_t len = strlen(str); - uint32_t i = 0; + size_t len = strlen(str); + size_t i = 0; for (i = 0; i < len && str[i] != 0; i++) { if (!(str[i] == '.' || str[i] == ':' || isxdigit(str[i]))) diff --git a/src/util-ja3.h b/src/util-ja3.h index f7ecb3079bc2..5211b85d559e 100644 --- a/src/util-ja3.h +++ b/src/util-ja3.h @@ -30,8 +30,8 @@ typedef struct JA3Buffer_ { char *data; - size_t size; - size_t used; + uint32_t size; + uint32_t used; } JA3Buffer; JA3Buffer *Ja3BufferInit(void); diff --git a/src/util-landlock.c b/src/util-landlock.c index 245be5634fd5..17bb5d5366cf 100644 --- a/src/util-landlock.c +++ b/src/util-landlock.c @@ -43,7 +43,7 @@ void LandlockSandboxing(SCInstance *suri) static inline int landlock_create_ruleset( const struct landlock_ruleset_attr *const attr, const size_t size, const __u32 flags) { - return syscall(__NR_landlock_create_ruleset, attr, size, flags); + return (int)syscall(__NR_landlock_create_ruleset, attr, size, flags); } #endif @@ -51,14 +51,14 @@ static inline int landlock_create_ruleset( static inline int landlock_add_rule(const int ruleset_fd, const enum landlock_rule_type rule_type, const void *const rule_attr, const __u32 flags) { - return syscall(__NR_landlock_add_rule, ruleset_fd, rule_type, rule_attr, flags); + return (int)syscall(__NR_landlock_add_rule, ruleset_fd, rule_type, rule_attr, flags); } #endif #ifndef landlock_restrict_self static inline int landlock_restrict_self(const int ruleset_fd, const __u32 flags) { - return syscall(__NR_landlock_restrict_self, ruleset_fd, flags); + return (int)syscall(__NR_landlock_restrict_self, ruleset_fd, flags); } #endif diff --git a/src/util-logopenfile.c b/src/util-logopenfile.c index 484481100490..504f27981658 100644 --- a/src/util-logopenfile.c +++ b/src/util-logopenfile.c @@ -785,7 +785,7 @@ static bool LogFileThreadedName( * for update */ dot = strrchr(original_name, '.'); - int dotpos = dot - original_name; + long dotpos = dot - original_name; tname[dotpos] = '\0'; char *ext = tname + dotpos + 1; if (strlen(tname) && strlen(ext)) { diff --git a/src/util-lua-hashlib.c b/src/util-lua-hashlib.c index 9c8bf7dab2b3..f0bdbfb155e2 100644 --- a/src/util-lua-hashlib.c +++ b/src/util-lua-hashlib.c @@ -74,7 +74,7 @@ static int LuaHashLibSha256Update(lua_State *L) } size_t data_len; const char *data = luaL_checklstring(L, 2, &data_len); - SCSha256Update(*hasher, (const uint8_t *)data, data_len); + SCSha256Update(*hasher, (const uint8_t *)data, (uint32_t)data_len); return 0; } @@ -123,7 +123,7 @@ static int LuaHashLibSha256Digest(lua_State *L) size_t buf_len; const char *input = luaL_checklstring(L, 1, &buf_len); - size_t output_len = SC_SHA256_LEN; + uint32_t output_len = SC_SHA256_LEN; uint8_t output[output_len]; if (!SCSha256HashBuffer((uint8_t *)input, (uint32_t)buf_len, output, output_len)) { return luaL_error(L, "sha256 hashing failed"); @@ -178,7 +178,7 @@ static int LuaHashLibSha1Update(lua_State *L) size_t data_len; const char *data = luaL_checklstring(L, 2, &data_len); - SCSha1Update(*hasher, (const uint8_t *)data, data_len); + SCSha1Update(*hasher, (const uint8_t *)data, (uint32_t)data_len); return 0; } @@ -280,7 +280,7 @@ static int LuaHashLibMd5Update(lua_State *L) size_t data_len; const char *data = luaL_checklstring(L, 2, &data_len); - SCMd5Update(*hasher, (const uint8_t *)data, data_len); + SCMd5Update(*hasher, (const uint8_t *)data, (uint32_t)data_len); return 0; } @@ -344,7 +344,7 @@ static int LuaHashLibMd5HexDigest(lua_State *L) const char *input = luaL_checklstring(L, 1, &buf_len); char output[SC_MD5_HEX_LEN + 1]; - if (!SCMd5HashBufferToHex((uint8_t *)input, (size_t)buf_len, output, sizeof(output))) { + if (!SCMd5HashBufferToHex((uint8_t *)input, (uint32_t)buf_len, output, sizeof(output))) { return luaL_error(L, "md5 hashing failed"); } diff --git a/src/util-lua-sandbox.h b/src/util-lua-sandbox.h index b04ab746d230..7eb9ccc2c494 100644 --- a/src/util-lua-sandbox.h +++ b/src/util-lua-sandbox.h @@ -47,7 +47,7 @@ typedef struct SCLuaSbState { /* Execution Limits */ uint64_t instruction_count; uint64_t instruction_limit; - uint64_t hook_instruction_count; + uint32_t hook_instruction_count; /* Errors. */ bool blocked_function_error; diff --git a/src/util-proto-name.c b/src/util-proto-name.c index c6d91ae76d7e..0c562885d3ef 100644 --- a/src/util-proto-name.c +++ b/src/util-proto-name.c @@ -361,7 +361,7 @@ static uint32_t ProtoNameHashFunc(HashTable *ht, void *data, uint16_t datalen) * as the proto number is not used for lookups */ ProtoNameHashEntry *p = (ProtoNameHashEntry *)data; - return StringHashDjb2((uint8_t *)p->name, strlen(p->name)) % ht->array_size; + return StringHashDjb2((uint8_t *)p->name, (uint32_t)strlen(p->name)) % ht->array_size; } static char ProtoNameHashCompareFunc(void *data1, uint16_t datalen1, void *data2, uint16_t datalen2) @@ -375,8 +375,8 @@ static char ProtoNameHashCompareFunc(void *data1, uint16_t datalen1, void *data2 if (p1->name == NULL || p2->name == NULL) return 0; - int len1 = strlen(p1->name); - int len2 = strlen(p2->name); + size_t len1 = strlen(p1->name); + size_t len2 = strlen(p2->name); return len1 == len2 && memcmp(p1->name, p2->name, len1) == 0; } diff --git a/src/util-random.c b/src/util-random.c index 0beee3287880..012073d9ca4e 100644 --- a/src/util-random.c +++ b/src/util-random.c @@ -38,7 +38,7 @@ static long int RandomGetClock(void) clock_gettime(CLOCK_REALTIME, &ts); // coverity[dont_call : FALSE] - srandom(ts.tv_nsec ^ ts.tv_sec); + srandom((unsigned int)(ts.tv_nsec ^ ts.tv_sec)); long int value = random(); return value; } @@ -103,7 +103,7 @@ long int RandomGet(void) return 0; long int value = 0; - int ret = getrandom(&value, sizeof(value), 0); + ssize_t ret = getrandom(&value, sizeof(value), 0); /* ret should be sizeof(value), but if it is > 0 and < sizeof(value) * it's still better than nothing so we return what we have */ if (ret <= 0) { diff --git a/src/util-reference-config.c b/src/util-reference-config.c index 1f7dc39573fd..9b2b09337c01 100644 --- a/src/util-reference-config.c +++ b/src/util-reference-config.c @@ -408,9 +408,9 @@ uint32_t SCRConfReferenceHashFunc(HashTable *ht, void *data, uint16_t datalen) { SCRConfReference *ref = (SCRConfReference *)data; uint32_t hash = 0; - int i = 0; + size_t i = 0; - int len = strlen(ref->system); + size_t len = strlen(ref->system); for (i = 0; i < len; i++) hash += u8_tolower((unsigned char)ref->system[i]); @@ -438,8 +438,8 @@ char SCRConfReferenceHashCompareFunc(void *data1, uint16_t datalen1, { SCRConfReference *ref1 = (SCRConfReference *)data1; SCRConfReference *ref2 = (SCRConfReference *)data2; - int len1 = 0; - int len2 = 0; + size_t len1 = 0; + size_t len2 = 0; if (ref1 == NULL || ref2 == NULL) return 0; diff --git a/src/util-streaming-buffer.c b/src/util-streaming-buffer.c index 9ae3039087c8..7a025cd1041b 100644 --- a/src/util-streaming-buffer.c +++ b/src/util-streaming-buffer.c @@ -441,7 +441,8 @@ static inline void ConsolidateFwd(StreamingBuffer *sb, const StreamingBufferConf if (sa->offset == region->stream_offset && sa_re > (region->stream_offset + region->buf_offset)) { DEBUG_VALIDATE_BUG_ON(sa_re < region->stream_offset); - region->buf_offset = sa_re - region->stream_offset; + DEBUG_VALIDATE_BUG_ON(sa_re - region->stream_offset > UINT32_MAX); + region->buf_offset = (uint32_t)(sa_re - region->stream_offset); SCLogDebug("-> (fwd) tr %p %" PRIu64 "/%u region %p so %" PRIu64 " bo %u sz %u BUF_OFFSET UPDATED", sa, sa->offset, sa->len, region, region->stream_offset, region->buf_offset, @@ -457,8 +458,10 @@ static inline void ConsolidateFwd(StreamingBuffer *sb, const StreamingBufferConf sa_re >= tr->offset && sa_re < tr_re) // ends inside { // merge. sb->sbb_size includes both so we need to adjust that too. - uint32_t combined_len = sa->len + tr->len; - sa->len = tr_re - sa->offset; + DEBUG_VALIDATE_BUG_ON(sa->len + tr->len > UINT32_MAX); + uint32_t combined_len = (uint32_t)(sa->len + tr->len); + DEBUG_VALIDATE_BUG_ON(tr_re - sa->offset > UINT32_MAX); + sa->len = (uint32_t)(tr_re - sa->offset); sa_re = sa->offset + sa->len; SCLogDebug("-> (fwd) tr %p %" PRIu64 "/%u REMOVED MERGED", tr, tr->offset, tr->len); SBB_RB_REMOVE(tree, tr); @@ -471,7 +474,8 @@ static inline void ConsolidateFwd(StreamingBuffer *sb, const StreamingBufferConf if (sa->offset == region->stream_offset && sa_re > (region->stream_offset + region->buf_offset)) { DEBUG_VALIDATE_BUG_ON(sa_re < region->stream_offset); - region->buf_offset = sa_re - region->stream_offset; + DEBUG_VALIDATE_BUG_ON(sa_re - region->stream_offset > UINT32_MAX); + region->buf_offset = (uint32_t)(sa_re - region->stream_offset); SCLogDebug("-> (fwd) tr %p %" PRIu64 "/%u region %p so %" PRIu64 " bo %u sz %u BUF_OFFSET UPDATED", sa, sa->offset, sa->len, region, region->stream_offset, region->buf_offset, @@ -538,7 +542,8 @@ static inline void ConsolidateBackward(StreamingBuffer *sb, const StreamingBuffe if (sa->offset == region->stream_offset && sa_re > (region->stream_offset + region->buf_offset)) { DEBUG_VALIDATE_BUG_ON(sa_re < region->stream_offset); - region->buf_offset = sa_re - region->stream_offset; + DEBUG_VALIDATE_BUG_ON(sa_re - region->stream_offset > UINT32_MAX); + region->buf_offset = (uint32_t)(sa_re - region->stream_offset); SCLogDebug("-> (bwd) tr %p %" PRIu64 "/%u region %p so %" PRIu64 " bo %u sz %u BUF_OFFSET UPDATED", sa, sa->offset, sa->len, region, region->stream_offset, region->buf_offset, @@ -553,8 +558,10 @@ static inline void ConsolidateBackward(StreamingBuffer *sb, const StreamingBuffe */ } else if (sa->offset > tr->offset && sa_re > tr_re && sa->offset <= tr_re) { // merge. sb->sbb_size includes both so we need to adjust that too. - uint32_t combined_len = sa->len + tr->len; - sa->len = sa_re - tr->offset; + DEBUG_VALIDATE_BUG_ON(sa->len + tr->len > UINT32_MAX); + uint32_t combined_len = (uint32_t)(sa->len + tr->len); + DEBUG_VALIDATE_BUG_ON(sa_re - tr->offset > UINT32_MAX); + sa->len = (uint32_t)(sa_re - tr->offset); sa->offset = tr->offset; sa_re = sa->offset + sa->len; SCLogDebug("-> (bwd) tr %p %" PRIu64 "/%u REMOVED MERGED", tr, tr->offset, tr->len); @@ -570,7 +577,8 @@ static inline void ConsolidateBackward(StreamingBuffer *sb, const StreamingBuffe if (sa->offset == region->stream_offset && sa_re > (region->stream_offset + region->buf_offset)) { DEBUG_VALIDATE_BUG_ON(sa_re < region->stream_offset); - region->buf_offset = sa_re - region->stream_offset; + DEBUG_VALIDATE_BUG_ON(sa_re - region->stream_offset > UINT32_MAX); + region->buf_offset = (uint32_t)(sa_re - region->stream_offset); SCLogDebug("-> (bwd) tr %p %" PRIu64 "/%u region %p so %" PRIu64 " bo %u sz %u BUF_OFFSET UPDATED", sa, sa->offset, sa->len, region, region->stream_offset, region->buf_offset, @@ -656,7 +664,8 @@ static void SBBPrune(StreamingBuffer *sb, const StreamingBufferConfig *cfg) /* partly before, partly beyond. Adjust */ if (sbb->offset < sb->region.stream_offset && sbb->offset + sbb->len > sb->region.stream_offset) { - uint32_t shrink_by = sb->region.stream_offset - sbb->offset; + DEBUG_VALIDATE_BUG_ON(sb->region.stream_offset - sbb->offset > UINT32_MAX); + uint32_t shrink_by = (uint32_t)(sb->region.stream_offset - sbb->offset); DEBUG_VALIDATE_BUG_ON(shrink_by > sbb->len); if (sbb->len >= shrink_by) { sbb->len -= shrink_by; @@ -876,7 +885,8 @@ static inline void StreamingBufferSlideToOffsetWithRegions( if (to_shift) { // Do the shift. If new region is exactly at the slide offset we can skip this. DEBUG_VALIDATE_BUG_ON(to_shift->stream_offset > slide_offset); - const uint32_t s = slide_offset - to_shift->stream_offset; + DEBUG_VALIDATE_BUG_ON(slide_offset - to_shift->stream_offset > UINT32_MAX); + const uint32_t s = (uint32_t)(slide_offset - to_shift->stream_offset); if (s > 0) { const uint32_t new_data_size = to_shift->buf_size - s; uint32_t new_mem_size = ToNextMultipleOf(new_data_size, cfg->buf_size); @@ -891,15 +901,21 @@ static inline void StreamingBufferSlideToOffsetWithRegions( StreamingBufferRegion *start = to_shift; StreamingBufferRegion *next = start->next; const uint64_t next_re = next->stream_offset + next->buf_size; - const uint32_t mem_size = ToNextMultipleOf(next_re - slide_offset, cfg->buf_size); + DEBUG_VALIDATE_BUG_ON(next_re - slide_offset > UINT32_MAX); + const uint32_t mem_size = + ToNextMultipleOf((uint32_t)(next_re - slide_offset), cfg->buf_size); /* using next as the new main */ if (start->buf_size < next->buf_size) { SCLogDebug("replace main with the next bigger region"); - const uint32_t next_data_offset = next->stream_offset - slide_offset; + DEBUG_VALIDATE_BUG_ON(next->stream_offset - slide_offset > UINT32_MAX); + const uint32_t next_data_offset = + (uint32_t)(next->stream_offset - slide_offset); const uint32_t prev_buf_size = next->buf_size; - const uint32_t start_data_offset = slide_offset - start->stream_offset; + DEBUG_VALIDATE_BUG_ON(slide_offset - start->stream_offset > UINT32_MAX); + const uint32_t start_data_offset = + (uint32_t)(slide_offset - start->stream_offset); DEBUG_VALIDATE_BUG_ON(start_data_offset > start->buf_size); if (start_data_offset > start->buf_size) { new_mem_size = new_data_size; @@ -1033,7 +1049,8 @@ void StreamingBufferSlideToOffset( } if (offset > sb->region.stream_offset) { - const uint32_t slide = offset - sb->region.stream_offset; + DEBUG_VALIDATE_BUG_ON(offset - sb->region.stream_offset > UINT32_MAX); + const uint32_t slide = (uint32_t)(offset - sb->region.stream_offset); if (sb->head != NULL) { /* have sbb's, so can't rely on buf_offset for the slide */ if (slide < sb->region.buf_size) { @@ -1273,7 +1290,8 @@ static StreamingBufferRegion *BufferInsertAtRegionConsolidate(StreamingBuffer *s // 2. resize dst const uint32_t old_size = dst->buf_size; - const uint32_t dst_copy_offset = dst->stream_offset - dst_offset; + DEBUG_VALIDATE_BUG_ON(dst->stream_offset - dst_offset > UINT32_MAX); + const uint32_t dst_copy_offset = (uint32_t)(dst->stream_offset - dst_offset); #ifdef DEBUG const uint32_t old_offset = dst->buf_offset; SCLogDebug("old_size %u, old_offset %u, dst_copy_offset %u", old_size, old_offset, @@ -1330,7 +1348,8 @@ static StreamingBufferRegion *BufferInsertAtRegionConsolidate(StreamingBuffer *s r = r->next; continue; } - const uint32_t target_offset = r->stream_offset - dst_offset; + DEBUG_VALIDATE_BUG_ON(r->stream_offset - dst_offset > UINT32_MAX); + const uint32_t target_offset = (uint32_t)(r->stream_offset - dst_offset); SCLogDebug("r %p: target_offset %u", r, target_offset); DEBUG_VALIDATE_BUG_ON(target_offset > dst->buf_size); DEBUG_VALIDATE_BUG_ON(target_offset + r->buf_size > dst->buf_size); @@ -1409,8 +1428,9 @@ static StreamingBufferRegion *BufferInsertAtRegionDo(StreamingBuffer *sb, } insert_adjusted_re = MAX(insert_adjusted_re, (end->stream_offset + end->buf_size)); - uint32_t new_buf_size = - ToNextMultipleOf(insert_adjusted_re - insert_start_offset, cfg->buf_size); + DEBUG_VALIDATE_BUG_ON(insert_adjusted_re - insert_start_offset > UINT32_MAX); + uint32_t new_buf_size = ToNextMultipleOf( + (uint32_t)(insert_adjusted_re - insert_start_offset), cfg->buf_size); SCLogDebug("new_buf_size %u", new_buf_size); /* see if our new buf size + cfg->buf_size margin leads to an overlap with the next region. @@ -1419,8 +1439,9 @@ static StreamingBufferRegion *BufferInsertAtRegionDo(StreamingBuffer *sb, SCLogDebug("adjusted end from %p to %p", end, end->next); end = end->next; insert_adjusted_re = MAX(insert_adjusted_re, (end->stream_offset + end->buf_size)); - new_buf_size = - ToNextMultipleOf(insert_adjusted_re - insert_start_offset, cfg->buf_size); + DEBUG_VALIDATE_BUG_ON(insert_adjusted_re - insert_start_offset > UINT32_MAX); + new_buf_size = ToNextMultipleOf( + (uint32_t)(insert_adjusted_re - insert_start_offset), cfg->buf_size); SCLogDebug("new_buf_size %u", new_buf_size); } @@ -1531,7 +1552,8 @@ int StreamingBufferInsertAt(StreamingBuffer *sb, const StreamingBufferConfig *cf SCLogDebug("inserting %" PRIu64 "/%u using %s region %p", offset, data_len, region == &sb->region ? "main" : "aux", region); - uint32_t rel_offset = offset - region->stream_offset; + DEBUG_VALIDATE_BUG_ON(offset - region->stream_offset > UINT32_MAX); + uint32_t rel_offset = (uint32_t)(offset - region->stream_offset); int r = DataFitsAtOffset(region, data_len, rel_offset); if (r < 0) { DEBUG_VALIDATE_BUG_ON(1); @@ -1687,7 +1709,8 @@ void StreamingBufferSBBGetData(const StreamingBuffer *sb, uint64_t offset = region->stream_offset - sbb->offset; if (offset < sbb->len) { *data = region->buf; - *data_len = sbb->len - offset; + DEBUG_VALIDATE_BUG_ON(sbb->len - offset > UINT32_MAX); + *data_len = (uint32_t)(sbb->len - offset); return; } SCLogDebug("3"); @@ -1713,22 +1736,26 @@ void StreamingBufferSBBGetDataAtOffset(const StreamingBuffer *sb, const StreamingBufferRegion *region = GetRegionForOffset(sb, offset); if (region) { - uint32_t sbblen = sbb->len - (offset - sbb->offset); + DEBUG_VALIDATE_BUG_ON(sbb->len - (offset - sbb->offset) > UINT32_MAX); + uint32_t sbblen = (uint32_t)(sbb->len - (offset - sbb->offset)); if (offset >= region->stream_offset) { uint64_t data_offset = offset - region->stream_offset; *data = region->buf + data_offset; - if (data_offset + sbblen > region->buf_size) - *data_len = region->buf_size - data_offset; - else + if (data_offset + sbblen > region->buf_size) { + DEBUG_VALIDATE_BUG_ON(region->buf_size - data_offset > UINT32_MAX); + *data_len = (uint32_t)(region->buf_size - data_offset); + } else { *data_len = sbblen; + } DEBUG_VALIDATE_BUG_ON(*data_len > sbblen); return; } else { uint64_t data_offset = region->stream_offset - sbb->offset; if (data_offset < sbblen) { *data = region->buf; - *data_len = sbblen - data_offset; + DEBUG_VALIDATE_BUG_ON(sbblen - data_offset > UINT32_MAX); + *data_len = (uint32_t)(sbblen - data_offset); DEBUG_VALIDATE_BUG_ON(*data_len > sbblen); return; } @@ -1748,17 +1775,20 @@ void StreamingBufferSegmentGetData(const StreamingBuffer *sb, if (seg->stream_offset >= region->stream_offset) { uint64_t offset = seg->stream_offset - region->stream_offset; *data = region->buf + offset; - if (offset + seg->segment_len > region->buf_size) - *data_len = region->buf_size - offset; - else + if (offset + seg->segment_len > region->buf_size) { + DEBUG_VALIDATE_BUG_ON(region->buf_size - offset > UINT32_MAX); + *data_len = (uint32_t)(region->buf_size - offset); + } else { *data_len = seg->segment_len; + } SCLogDebug("*data_len %u", *data_len); return; } else { uint64_t offset = region->stream_offset - seg->stream_offset; if (offset < seg->segment_len) { *data = region->buf; - *data_len = seg->segment_len - offset; + DEBUG_VALIDATE_BUG_ON(seg->segment_len - offset > UINT32_MAX); + *data_len = (uint32_t)(seg->segment_len - offset); SCLogDebug("*data_len %u", *data_len); return; } @@ -1812,7 +1842,8 @@ int StreamingBufferGetDataAtOffset (const StreamingBuffer *sb, const StreamingBufferRegion *region = GetRegionForOffset(sb, offset); if (region != NULL && region->buf != NULL && offset >= region->stream_offset && offset < (region->stream_offset + region->buf_offset)) { - uint32_t skip = offset - region->stream_offset; + DEBUG_VALIDATE_BUG_ON(offset - region->stream_offset > UINT32_MAX); + uint32_t skip = (uint32_t)(offset - region->stream_offset); *data = region->buf + skip; *data_len = region->buf_offset - skip; return 1; diff --git a/src/util-thash.c b/src/util-thash.c index be6e930bcc5f..dd222569bcff 100644 --- a/src/util-thash.c +++ b/src/util-thash.c @@ -311,7 +311,8 @@ THashTableContext *THashInit(const char *cnf_prefix, size_t data_size, THashTableContext *ctx = SCCalloc(1, sizeof(*ctx)); BUG_ON(!ctx); - ctx->config.data_size = data_size; + BUG_ON(data_size > UINT32_MAX); + ctx->config.data_size = (uint32_t)data_size; ctx->config.DataSet = DataSet; ctx->config.DataFree = DataFree; ctx->config.DataHash = DataHash; diff --git a/src/util-threshold-config.c b/src/util-threshold-config.c index 6df1eebb0798..6bedfcd94c91 100644 --- a/src/util-threshold-config.c +++ b/src/util-threshold-config.c @@ -957,12 +957,12 @@ static int SCThresholdConfLineIsMultiline(char *line) { int flag = 0; char *rline = line; - int len = strlen(line); + size_t len = strlen(line); while (line < rline + len && *line != '\n') { /* we have a comment */ if (*line == '\\') - flag = line - rline; + flag = (int)(line - rline); else if (!isspace((unsigned char)*line)) flag = 0; diff --git a/src/util-time.c b/src/util-time.c index 9900f1b94bd3..67b3fd49444f 100644 --- a/src/util-time.c +++ b/src/util-time.c @@ -549,7 +549,7 @@ int SCTimeToStringPattern (time_t epoch, const char *pattern, char *str, size_t return 1; } - int r = strftime(buffer, sizeof(buffer), pattern, tp); + size_t r = strftime(buffer, sizeof(buffer), pattern, tp); if (r == 0) { return 1; } diff --git a/src/util-var-name.c b/src/util-var-name.c index 6bcb0331a2e3..a93109442bd1 100644 --- a/src/util-var-name.c +++ b/src/util-var-name.c @@ -329,7 +329,8 @@ uint32_t VarNameStoreLookupByName(const char *name, const enum VarTypes type) static uint32_t VariableNameHash(HashListTable *ht, void *buf, uint16_t buflen) { VariableName *vn = (VariableName *)buf; - uint32_t hash = StringHashDjb2((const uint8_t *)vn->name, strlen(vn->name)) + vn->type; + uint32_t hash = + StringHashDjb2((const uint8_t *)vn->name, (uint32_t)strlen(vn->name)) + vn->type; return (hash % VARNAME_HASHSIZE); }