Skip to content

Commit

Permalink
app-layer: fix -Wshorten-64-to-32 warnings
Browse files Browse the repository at this point in the history
Ticket: OISF#6186

Warnings about downcast from 64 to 32 bits
  • Loading branch information
catenacyber authored and victorjulien committed Jun 22, 2024
1 parent 1790aa4 commit 0d4efe0
Show file tree
Hide file tree
Showing 12 changed files with 90 additions and 68 deletions.
2 changes: 1 addition & 1 deletion src/app-layer-detect-proto.c
Original file line number Diff line number Diff line change
Expand Up @@ -691,7 +691,7 @@ static uint32_t AppLayerProtoDetectProbingParserGetMask(AppProto alproto)
FatalError("Unknown protocol detected - %u", alproto);
}

SCReturnUInt(1UL << (uint32_t)alproto);
SCReturnUInt(BIT_U32(alproto));
}

static AppLayerProtoDetectProbingParserElement *AppLayerProtoDetectProbingParserElementAlloc(void)
Expand Down
3 changes: 1 addition & 2 deletions src/app-layer-dnp3-objects.c
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,7 @@ static int DNP3ReadUint24(const uint8_t **buf, uint32_t *len, uint32_t *out)
*out = ((uint32_t)(*buf)[0] << 16) | ((uint32_t)(*buf)[1] << 8) |
(uint32_t)(*buf)[2];
#elif __BYTE_ORDER == __LITTLE_ENDIAN
*out = ((uint64_t)(*buf)[0]) | ((uint64_t)(*buf)[1] << 8) |
((uint64_t)(*buf)[2] << 16);
*out = ((uint32_t)(*buf)[0]) | ((uint32_t)(*buf)[1] << 8) | ((uint32_t)(*buf)[2] << 16);
#endif

*buf += 3;
Expand Down
10 changes: 6 additions & 4 deletions src/app-layer-frames.c
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,9 @@ static int FrameSlide(const char *ds, Frames *frames, const TcpStream *stream, c
}
frames->cnt = x;
uint64_t o = STREAM_BASE_OFFSET(stream) + slide;
frames->left_edge_rel = le - (STREAM_BASE_OFFSET(stream) + slide);
DEBUG_VALIDATE_BUG_ON(o > le);
DEBUG_VALIDATE_BUG_ON(le - o > UINT32_MAX);
frames->left_edge_rel = (uint32_t)(le - o);

#ifdef DEBUG
SCLogDebug("end: left edge %" PRIu64 ", left_edge_rel %u, stream base %" PRIu64
Expand All @@ -383,7 +385,6 @@ static int FrameSlide(const char *ds, Frames *frames, const TcpStream *stream, c
snprintf(pf, sizeof(pf), "%s:post_slide", ds);
AppLayerFrameDumpForFrames(pf, frames);
#endif
BUG_ON(o > le);
BUG_ON(x != start - removed);
return 0;
}
Expand Down Expand Up @@ -796,15 +797,16 @@ static void FramePrune(Frames *frames, const TcpStream *stream, const bool eof)
}
}
frames->cnt = x;
frames->left_edge_rel = le - STREAM_BASE_OFFSET(stream);
DEBUG_VALIDATE_BUG_ON(le < STREAM_BASE_OFFSET(stream));
DEBUG_VALIDATE_BUG_ON(le - STREAM_BASE_OFFSET(stream) > UINT32_MAX);
frames->left_edge_rel = (uint32_t)(le - STREAM_BASE_OFFSET(stream));
#ifdef DEBUG
SCLogDebug("end: left edge %" PRIu64 ", left_edge_rel %u, stream base %" PRIu64
", cnt %u, removed %u, start %u",
(uint64_t)frames->left_edge_rel + STREAM_BASE_OFFSET(stream), frames->left_edge_rel,
STREAM_BASE_OFFSET(stream), frames->cnt, removed, start);
AppLayerFrameDumpForFrames("post_slide", frames);
#endif
BUG_ON(le < STREAM_BASE_OFFSET(stream));
if (frames->cnt > 0) { // if we removed all this can fail
BUG_ON(frames_le_start > le);
}
Expand Down
2 changes: 1 addition & 1 deletion src/app-layer-ftp.c
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ static AppLayerResult FTPGetLineForDirection(
// lf_idx = 5010
// max_line_len = 4096
uint32_t o_consumed = input->consumed;
input->consumed = lf_idx - input->buf + 1;
input->consumed = (uint32_t)(lf_idx - input->buf + 1);
line->len = input->consumed - o_consumed;
input->len -= line->len;
line->lf_found = true;
Expand Down
12 changes: 8 additions & 4 deletions src/app-layer-htp-file.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ int HTPFileOpen(HtpState *s, HtpTxUserData *tx, const uint8_t *filename, uint16_
*/
int HTPParseContentRange(bstr *rawvalue, HTTPContentRange *range)
{
uint32_t len = bstr_len(rawvalue);
uint32_t len = (uint32_t)bstr_len(rawvalue);
return rs_http_parse_content_range(range, bstr_ptr(rawvalue), len);
}

Expand Down Expand Up @@ -182,13 +182,17 @@ int HTPFileOpenWithRange(HtpState *s, HtpTxUserData *txud, const uint8_t *filena
uint8_t *keyurl;
uint32_t keylen;
if (tx->request_hostname != NULL) {
keylen = bstr_len(tx->request_hostname) + filename_len;
uint32_t hlen = (uint32_t)bstr_len(tx->request_hostname);
if (bstr_len(tx->request_hostname) > UINT16_MAX) {
hlen = UINT16_MAX;
}
keylen = hlen + filename_len;
keyurl = SCMalloc(keylen);
if (keyurl == NULL) {
SCReturnInt(-1);
}
memcpy(keyurl, bstr_ptr(tx->request_hostname), bstr_len(tx->request_hostname));
memcpy(keyurl + bstr_len(tx->request_hostname), filename, filename_len);
memcpy(keyurl, bstr_ptr(tx->request_hostname), hlen);
memcpy(keyurl + hlen, filename, filename_len);
} else {
// do not reassemble file without host info
SCReturnInt(0);
Expand Down
33 changes: 23 additions & 10 deletions src/app-layer-htp-range.c
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ static void ContainerUrlRangeFree(void *s)
static inline bool ContainerValueRangeTimeout(HttpRangeContainerFile *cu, const SCTime_t ts)
{
// we only timeout if we have no flow referencing us
if ((uint32_t)SCTIME_SECS(ts) > cu->expire || cu->error) {
if (SCTIME_CMP_GT(ts, cu->expire) || cu->error) {
if (SC_ATOMIC_GET(cu->hdata->use_cnt) == 0) {
DEBUG_VALIDATE_BUG_ON(cu->files == NULL);
return true;
Expand All @@ -139,7 +139,7 @@ static inline bool ContainerValueRangeTimeout(HttpRangeContainerFile *cu, const
return false;
}

static void ContainerUrlRangeUpdate(HttpRangeContainerFile *cu, uint32_t expire)
static void ContainerUrlRangeUpdate(HttpRangeContainerFile *cu, SCTime_t expire)
{
cu->expire = expire;
}
Expand Down Expand Up @@ -246,7 +246,7 @@ static void *HttpRangeContainerUrlGet(const uint8_t *key, uint32_t keylen, const
struct THashDataGetResult res = THashGetFromHash(ContainerUrlRangeList.ht, &lookup);
if (res.data) {
// nothing more to do if (res.is_new)
ContainerUrlRangeUpdate(res.data->data, SCTIME_SECS(ts) + ContainerUrlRangeList.timeout);
ContainerUrlRangeUpdate(res.data->data, SCTIME_ADD_SECS(ts, ContainerUrlRangeList.timeout));
HttpRangeContainerFile *c = res.data->data;
c->hdata = res.data;
SCLogDebug("c %p", c);
Expand Down Expand Up @@ -412,9 +412,9 @@ int HttpRangeAppendData(const StreamingBufferConfig *sbcfg, HttpRangeContainerBl
if (c->files) {
if (data == NULL) {
// gap overlapping already known data
r = FileAppendData(c->files, sbcfg, NULL, len - c->toskip);
r = FileAppendData(c->files, sbcfg, NULL, (uint32_t)(len - c->toskip));
} else {
r = FileAppendData(c->files, sbcfg, data + c->toskip, len - c->toskip);
r = FileAppendData(c->files, sbcfg, data + c->toskip, (uint32_t)(len - c->toskip));
}
}
c->toskip = 0;
Expand Down Expand Up @@ -545,14 +545,21 @@ File *HttpRangeClose(const StreamingBufferConfig *sbcfg, HttpRangeContainerBlock
// a new range just begins where we ended, append it
if (range->gap > 0) {
// if the range had a gap, begin by it
if (FileAppendData(c->container->files, sbcfg, NULL, range->gap) != 0) {
uint32_t gap = range->gap <= UINT32_MAX ? (uint32_t)range->gap : UINT32_MAX;
if (FileAppendData(c->container->files, sbcfg, NULL, gap) != 0) {
c->container->lastsize = f->size;
HttpRangeFileClose(sbcfg, c->container, flags | FILE_TRUNCATED);
c->container->error = true;
return f;
}
}
if (FileAppendData(c->container->files, sbcfg, range->buffer, range->offset) != 0) {
if (range->offset > UINT32_MAX) {
c->container->lastsize = f->size;
HttpRangeFileClose(sbcfg, c->container, flags | FILE_TRUNCATED);
c->container->error = true;
return f;
} else if (FileAppendData(c->container->files, sbcfg, range->buffer,
(uint32_t)range->offset) != 0) {
c->container->lastsize = f->size;
HttpRangeFileClose(sbcfg, c->container, flags | FILE_TRUNCATED);
c->container->error = true;
Expand All @@ -564,7 +571,8 @@ File *HttpRangeClose(const StreamingBufferConfig *sbcfg, HttpRangeContainerBlock
if (overlap < range->offset) {
if (range->gap > 0) {
// if the range had a gap, begin by it
if (FileAppendData(c->container->files, sbcfg, NULL, range->gap) != 0) {
uint32_t gap = range->gap <= UINT32_MAX ? (uint32_t)range->gap : UINT32_MAX;
if (FileAppendData(c->container->files, sbcfg, NULL, gap) != 0) {
c->container->lastsize = f->size;
HttpRangeFileClose(sbcfg, c->container, flags | FILE_TRUNCATED);
c->container->error = true;
Expand All @@ -573,8 +581,13 @@ File *HttpRangeClose(const StreamingBufferConfig *sbcfg, HttpRangeContainerBlock
}
// And the range ends beyond where we ended
// in this case of overlap, only add the extra data
if (FileAppendData(c->container->files, sbcfg, range->buffer + overlap,
range->offset - overlap) != 0) {
if (range->offset - overlap > UINT32_MAX) {
c->container->lastsize = f->size;
HttpRangeFileClose(sbcfg, c->container, flags | FILE_TRUNCATED);
c->container->error = true;
return f;
} else if (FileAppendData(c->container->files, sbcfg, range->buffer + overlap,
(uint32_t)(range->offset - overlap)) != 0) {
c->container->lastsize = f->size;
HttpRangeFileClose(sbcfg, c->container, flags | FILE_TRUNCATED);
c->container->error = true;
Expand Down
2 changes: 1 addition & 1 deletion src/app-layer-htp-range.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ typedef struct HttpRangeContainerFile {
/** key length */
uint32_t len;
/** expire time in epoch */
uint32_t expire;
SCTime_t expire;
/** pointer to hashtable data, for locking and use count */
THashData *hdata;
/** total expected size of the file in ranges */
Expand Down
12 changes: 6 additions & 6 deletions src/app-layer-htp.c
Original file line number Diff line number Diff line change
Expand Up @@ -548,12 +548,12 @@ static uint32_t AppLayerHtpComputeChunkLength(uint64_t content_len_so_far, uint3
(content_len_so_far < (uint64_t)body_limit) &&
(content_len_so_far + (uint64_t)data_len) > body_limit)
{
chunk_len = body_limit - content_len_so_far;
chunk_len = (uint32_t)(body_limit - content_len_so_far);
} else if ((flags & HTP_STREAM_DEPTH_SET) && stream_depth > 0 &&
(content_len_so_far < (uint64_t)stream_depth) &&
(content_len_so_far + (uint64_t)data_len) > stream_depth)
{
chunk_len = stream_depth - content_len_so_far;
chunk_len = (uint32_t)(stream_depth - content_len_so_far);
}
SCLogDebug("len %u", chunk_len);
return (chunk_len == 0 ? data_len : chunk_len);
Expand Down Expand Up @@ -952,7 +952,7 @@ static AppLayerResult HTPHandleResponseData(Flow *f, void *htp_state, AppLayerPa

htp_time_t ts = { SCTIME_SECS(f->startts), SCTIME_USECS(f->startts) };
htp_tx_t *tx = NULL;
size_t consumed = 0;
uint32_t consumed = 0;
if (input_len > 0) {
const int r = htp_connp_res_data(hstate->connp, &ts, input, input_len);
switch (r) {
Expand All @@ -971,7 +971,7 @@ static AppLayerResult HTPHandleResponseData(Flow *f, void *htp_state, AppLayerPa
if (tx->request_port_number != -1) {
dp = (uint16_t)tx->request_port_number;
}
consumed = htp_connp_res_data_consumed(hstate->connp);
consumed = (uint32_t)htp_connp_res_data_consumed(hstate->connp);
if (bstr_cmp_c(h->value, "h2c") == 0) {
if (AppLayerProtoDetectGetProtoName(ALPROTO_HTTP2) == NULL) {
// if HTTP2 is disabled, keep the HTP_STREAM_TUNNEL mode
Expand Down Expand Up @@ -1132,7 +1132,7 @@ static int HtpRequestBodySetupMultipart(htp_tx_t *tx, HtpTxUserData *htud)
htp_header_t *h = (htp_header_t *)htp_table_get_c(tx->request_headers,
"Content-Type");
if (h != NULL && bstr_len(h->value) > 0) {
htud->mime_state = SCMimeStateInit(bstr_ptr(h->value), bstr_len(h->value));
htud->mime_state = SCMimeStateInit(bstr_ptr(h->value), (uint32_t)bstr_len(h->value));
if (htud->mime_state) {
htud->tsflags |= HTP_BOUNDARY_SET;
SCReturnInt(1);
Expand Down Expand Up @@ -2460,7 +2460,7 @@ static void HTPConfigParseParameters(HTPCfgRec *cfg_prec, ConfNode *s,
}
/* set default soft-limit with our new hard limit */
SCLogConfig("Setting HTTP max-tx limit to %" PRIu32 " bytes", limit);
htp_config_set_max_tx(cfg_prec->cfg, (size_t)limit);
htp_config_set_max_tx(cfg_prec->cfg, limit);
#endif
} else if (strcasecmp("randomize-inspection-sizes", p->name) == 0) {
if (!g_disable_randomness) {
Expand Down
10 changes: 6 additions & 4 deletions src/app-layer-http2.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,16 +82,18 @@ void HTTP2MimicHttp1Request(void *alstate_orig, void *h2s)
return;
}
// else
rs_http2_tx_set_method(h2s, bstr_ptr(h1tx->request_method), bstr_len(h1tx->request_method));
rs_http2_tx_set_method(
h2s, bstr_ptr(h1tx->request_method), (uint32_t)bstr_len(h1tx->request_method));
if (h1tx->request_uri != NULL) {
// A request line without spaces gets interpreted as a request_method
// and has request_uri=NULL
rs_http2_tx_set_uri(h2s, bstr_ptr(h1tx->request_uri), bstr_len(h1tx->request_uri));
rs_http2_tx_set_uri(
h2s, bstr_ptr(h1tx->request_uri), (uint32_t)bstr_len(h1tx->request_uri));
}
size_t nbheaders = htp_table_size(h1tx->request_headers);
for (size_t i = 0; i < nbheaders; i++) {
htp_header_t *h = htp_table_get_index(h1tx->request_headers, i, NULL);
rs_http2_tx_add_header(
h2s, bstr_ptr(h->name), bstr_len(h->name), bstr_ptr(h->value), bstr_len(h->value));
rs_http2_tx_add_header(h2s, bstr_ptr(h->name), (uint32_t)bstr_len(h->name),
bstr_ptr(h->value), (uint32_t)bstr_len(h->value));
}
}
26 changes: 14 additions & 12 deletions src/app-layer-smtp.c
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,7 @@ static AppLayerResult SMTPGetLine(Flow *f, StreamSlice *slice, SMTPState *state,
* lf_idx = 5010
* max_line_len = 4096 */
uint32_t o_consumed = input->consumed;
input->consumed = lf_idx - input->buf + 1;
input->consumed = (uint32_t)(lf_idx - input->buf + 1);
line->len = input->consumed - o_consumed;
line->lf_found = true;
DEBUG_VALIDATE_BUG_ON(line->len < 0);
Expand Down Expand Up @@ -759,8 +759,9 @@ static int SMTPProcessCommandDATA(
// not an attachment
break;
}
depth = smtp_config.content_inspect_min_size +
(state->toserver_data_count - state->toserver_last_data_stamp);
depth = (uint32_t)(smtp_config.content_inspect_min_size +
(state->toserver_data_count -
state->toserver_last_data_stamp));
SCLogDebug("StreamTcpReassemblySetMinInspectDepth STREAM_TOSERVER %" PRIu32,
depth);
StreamTcpReassemblySetMinInspectDepth(f->protoctx, STREAM_TOSERVER, depth);
Expand All @@ -780,8 +781,9 @@ static int SMTPProcessCommandDATA(
// rust already run FileAppendData
if (tx->files_ts.tail && tx->files_ts.tail->content_inspected == 0 &&
tx->files_ts.tail->size >= smtp_config.content_inspect_min_size) {
depth = smtp_config.content_inspect_min_size +
(state->toserver_data_count - state->toserver_last_data_stamp);
depth = (uint32_t)(smtp_config.content_inspect_min_size +
(state->toserver_data_count -
state->toserver_last_data_stamp));
AppLayerParserTriggerRawStreamReassembly(f, STREAM_TOSERVER);
SCLogDebug(
"StreamTcpReassemblySetMinInspectDepth STREAM_TOSERVER %u", depth);
Expand All @@ -792,8 +794,9 @@ static int SMTPProcessCommandDATA(
/* expand the limit as long as we get file data, as the file data is bigger
* on the wire due to base64 */
} else {
depth = smtp_config.content_inspect_min_size +
(state->toserver_data_count - state->toserver_last_data_stamp);
depth = (uint32_t)(smtp_config.content_inspect_min_size +
(state->toserver_data_count -
state->toserver_last_data_stamp));
SCLogDebug("StreamTcpReassemblySetMinInspectDepth STREAM_TOSERVER %" PRIu32,
depth);
StreamTcpReassemblySetMinInspectDepth(f->protoctx, STREAM_TOSERVER, depth);
Expand All @@ -807,7 +810,8 @@ static int SMTPProcessCommandDATA(
} else {
SCLogDebug("File already closed");
}
depth = state->toserver_data_count - state->toserver_last_data_stamp;
depth = (uint32_t)(state->toserver_data_count -
state->toserver_last_data_stamp);
AppLayerParserTriggerRawStreamReassembly(f, STREAM_TOSERVER);
SCLogDebug("StreamTcpReassemblySetMinInspectDepth STREAM_TOSERVER %u", depth);
StreamTcpReassemblySetMinInspectDepth(f->protoctx, STREAM_TOSERVER, depth);
Expand Down Expand Up @@ -972,17 +976,15 @@ static int SMTPParseCommandBDAT(SMTPState *state, const SMTPLine *line)
/* decoder event */
return -1;
}
char *endptr = NULL;
// copy in temporary null-terminated buffer to call strtoul
// copy in temporary null-terminated buffer to call StringParseUint32
char strbuf[24];
int len = 23;
if (line->len - i < len) {
len = line->len - i;
}
memcpy(strbuf, line->buf + i, len);
strbuf[len] = '\0';
state->bdat_chunk_len = strtoul((const char *)strbuf, (char **)&endptr, 10);
if ((uint8_t *)endptr == line->buf + i) {
if (StringParseUint32(&state->bdat_chunk_len, 10, 0, strbuf) < 0) {
/* decoder event */
return -1;
}
Expand Down
Loading

0 comments on commit 0d4efe0

Please sign in to comment.