Skip to content

Commit

Permalink
runmodes: fix -Wshorten-64-to-32 warnings
Browse files Browse the repository at this point in the history
Ticket: OISF#6186
  • Loading branch information
catenacyber committed Jul 17, 2024
1 parent 576f87a commit cee8184
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 9 deletions.
6 changes: 4 additions & 2 deletions src/runmode-pcap.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ static void *ParsePcapConfig(const char *iface)
if ((ConfGetInt("pcap.buffer-size", &value)) == 1) {
if (value >= 0 && value <= INT_MAX) {
SCLogInfo("Pcap will use %d buffer size", (int)value);
aconf->buffer_size = value;
aconf->buffer_size = (int)value;
} else {
SCLogWarning("pcap.buffer-size "
"value of %" PRIiMAX " is invalid. Valid range is "
Expand Down Expand Up @@ -207,8 +207,10 @@ static void *ParsePcapConfig(const char *iface)
aconf->snaplen = 0;
if (ConfGetChildValueIntWithDefault(if_root, if_default, "snaplen", &snaplen) != 1) {
SCLogDebug("could not get snaplen or none specified");
} else if (snaplen < INT_MIN || snaplen > INT_MAX) {
SCLogDebug("snaplen value is not in the accepted range");
} else {
aconf->snaplen = snaplen;
aconf->snaplen = (int)snaplen;
}

return aconf;
Expand Down
59 changes: 52 additions & 7 deletions src/runmode-unix-socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -831,6 +831,16 @@ TmEcode UnixSocketDatasetLookup(json_t *cmd, json_t *answer, void *data)
}
}

static bool json_u32_value(json_t *jarg, uint32_t *ret)
{
int64_t r = json_integer_value(jarg);
if (r < 0 || r > UINT32_MAX) {
return false;
}
*ret = (uint32_t)r;
return true;
}

/**
* \brief Command to add a tenant handler
*
Expand All @@ -856,7 +866,12 @@ TmEcode UnixSocketRegisterTenantHandler(json_t *cmd, json_t* answer, void *data)
json_object_set_new(answer, "message", json_string("id is not an integer"));
return TM_ECODE_FAILED;
}
uint32_t tenant_id = json_integer_value(jarg);
uint32_t tenant_id;
if (!json_u32_value(jarg, &tenant_id)) {
SCLogInfo("tenant_id is not a uint32");
json_object_set_new(answer, "message", json_string("tenant_id is not a uint32"));
return TM_ECODE_FAILED;
}

/* 2 get tenant handler type */
jarg = json_object_get(cmd, "htype");
Expand Down Expand Up @@ -937,7 +952,12 @@ TmEcode UnixSocketUnregisterTenantHandler(json_t *cmd, json_t* answer, void *dat
json_object_set_new(answer, "message", json_string("id is not an integer"));
return TM_ECODE_FAILED;
}
uint32_t tenant_id = json_integer_value(jarg);
uint32_t tenant_id;
if (!json_u32_value(jarg, &tenant_id)) {
SCLogInfo("tenant_id is not a uint32");
json_object_set_new(answer, "message", json_string("tenant_id is not a uint32"));
return TM_ECODE_FAILED;
}

/* 2 get tenant handler type */
jarg = json_object_get(cmd, "htype");
Expand Down Expand Up @@ -1018,7 +1038,12 @@ TmEcode UnixSocketRegisterTenant(json_t *cmd, json_t* answer, void *data)
json_object_set_new(answer, "message", json_string("id is not an integer"));
return TM_ECODE_FAILED;
}
uint32_t tenant_id = json_integer_value(jarg);
uint32_t tenant_id;
if (!json_u32_value(jarg, &tenant_id)) {
SCLogInfo("tenant_id is not a uint32");
json_object_set_new(answer, "message", json_string("tenant_id is not a uint32"));
return TM_ECODE_FAILED;
}

/* 2 get tenant yaml */
jarg = json_object_get(cmd, "filename");
Expand Down Expand Up @@ -1086,7 +1111,12 @@ TmEcode UnixSocketReloadTenant(json_t *cmd, json_t* answer, void *data)
json_object_set_new(answer, "message", json_string("id is not an integer"));
return TM_ECODE_FAILED;
}
uint32_t tenant_id = json_integer_value(jarg);
uint32_t tenant_id;
if (!json_u32_value(jarg, &tenant_id)) {
SCLogInfo("tenant_id is not a uint32");
json_object_set_new(answer, "message", json_string("tenant_id is not a uint32"));
return TM_ECODE_FAILED;
}

/* 2 get tenant yaml */
jarg = json_object_get(cmd, "filename");
Expand Down Expand Up @@ -1180,7 +1210,12 @@ TmEcode UnixSocketUnregisterTenant(json_t *cmd, json_t* answer, void *data)
json_object_set_new(answer, "message", json_string("id is not an integer"));
return TM_ECODE_FAILED;
}
uint32_t tenant_id = json_integer_value(jarg);
uint32_t tenant_id;
if (!json_u32_value(jarg, &tenant_id)) {
SCLogInfo("tenant_id is not a uint32");
json_object_set_new(answer, "message", json_string("tenant_id is not a uint32"));
return TM_ECODE_FAILED;
}

SCLogInfo("remove-tenant: removing tenant %d", tenant_id);

Expand Down Expand Up @@ -1271,14 +1306,24 @@ TmEcode UnixSocketHostbitAdd(json_t *cmd, json_t* answer, void *data_usused)
json_object_set_new(answer, "message", json_string("expire is not an integer"));
return TM_ECODE_FAILED;
}
uint32_t expire = json_integer_value(jarg);
uint32_t expire;
if (!json_u32_value(jarg, &expire)) {
SCLogInfo("expire is not a uint32");
json_object_set_new(answer, "message", json_string("expire is not a uint32"));
return TM_ECODE_FAILED;
}

SCLogInfo("add-hostbit: ip %s hostbit %s expire %us", ipaddress, hostbit, expire);

SCTime_t current_time = TimeGet();
Host *host = HostGetHostFromHash(&a);
if (host) {
HostBitSet(host, idx, SCTIME_SECS(current_time) + expire);
if (SCTIME_SECS(current_time) + expire > UINT32_MAX) {
json_object_set_new(answer, "message", json_string("couldn't set host expire"));
HostRelease(host);
return TM_ECODE_FAILED;
}
HostBitSet(host, idx, (uint32_t)(SCTIME_SECS(current_time) + expire));
HostRelease(host);

json_object_set_new(answer, "message", json_string("hostbit added"));
Expand Down

0 comments on commit cee8184

Please sign in to comment.