Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

util: fix -Wshorten-64-to-32 warnings #12481

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/tm-threads.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */

Expand Down
26 changes: 11 additions & 15 deletions src/unix-manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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 */
Expand All @@ -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);
catenacyber marked this conversation as resolved.
Show resolved Hide resolved
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.");
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand All @@ -582,24 +577,25 @@ 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");
UnixCommandClose(this, client->fd);
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);
}
Expand Down Expand Up @@ -695,7 +691,7 @@ static TmEcode UnixManagerUptimeCommand(json_t *cmd,
json_t *server_msg, void *data)
{
SCEnter();
int uptime;
int64_t uptime;
catenacyber marked this conversation as resolved.
Show resolved Hide resolved
UnixCommand *ucmd = (UnixCommand *)data;

uptime = time(NULL) - ucmd->start_timestamp;
Expand Down
10 changes: 5 additions & 5 deletions src/util-affinity.c
Original file line number Diff line number Diff line change
Expand Up @@ -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")) {
Expand 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);
Expand All @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/util-byte.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions src/util-classification-config.c
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down Expand Up @@ -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;
Expand Down
18 changes: 9 additions & 9 deletions src/util-debug.c
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}

Expand All @@ -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;
}
Expand Down
16 changes: 8 additions & 8 deletions src/util-fmemopen.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
catenacyber marked this conversation as resolved.
Show resolved Hide resolved
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)
Expand All @@ -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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

where is checked that his result fits an int?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

while (count < size) where size is an int from the function prototype

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it looks very hacky. size_t actually does look like the correct type here, and the change should probably that the function arg size is also made size_t


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;
Expand Down
8 changes: 4 additions & 4 deletions src/util-ip.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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])))
Expand Down
4 changes: 2 additions & 2 deletions src/util-ja3.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions src/util-landlock.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,22 +43,22 @@ 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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

syscall returns long, which is 64 bits on 64 bit archs. However this int usage seems to match example code for landlock, and casting syscall returns to other types seems quite normal, in fact the man page example does it as well

           pid_t tid;

           tid = syscall(SYS_gettid);
           syscall(SYS_tgkill, getpid(), tid, SIGHUP);

So I guess it is ok?
Might be good to do a long type and a bounds check before casting to int though

}
#endif

#ifndef landlock_add_rule
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

Expand Down
2 changes: 1 addition & 1 deletion src/util-logopenfile.c
Original file line number Diff line number Diff line change
Expand Up @@ -785,7 +785,7 @@ static bool LogFileThreadedName(
* for update
*/
dot = strrchr(original_name, '.');
int dotpos = dot - original_name;
long dotpos = dot - original_name;
catenacyber marked this conversation as resolved.
Show resolved Hide resolved
tname[dotpos] = '\0';
char *ext = tname + dotpos + 1;
if (strlen(tname) && strlen(ext)) {
Expand Down
10 changes: 5 additions & 5 deletions src/util-lua-hashlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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");
}

Expand Down
2 changes: 1 addition & 1 deletion src/util-lua-sandbox.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
catenacyber marked this conversation as resolved.
Show resolved Hide resolved

/* Errors. */
bool blocked_function_error;
Expand Down
Loading
Loading