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: #6186

Warnings about downcast from 64 to 32 bits
  • Loading branch information
catenacyber committed Jun 5, 2024
1 parent 9bb1338 commit ea77629
Show file tree
Hide file tree
Showing 12 changed files with 85 additions and 69 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 @@ -368,7 +368,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);
BUG_ON(o > le);
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 @@ -379,7 +381,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 @@ -790,15 +791,16 @@ static void FramePrune(Frames *frames, const TcpStream *stream, const bool eof)
}
}
frames->cnt = x;
frames->left_edge_rel = le - STREAM_BASE_OFFSET(stream);
BUG_ON(le < STREAM_BASE_OFFSET(stream));
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
23 changes: 17 additions & 6 deletions src/app-layer-htp-range.c
Original file line number Diff line number Diff line change
Expand Up @@ -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, uint64_t expire)
{
cu->expire = expire;
}
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 @@ -552,7 +552,13 @@ File *HttpRangeClose(const StreamingBufferConfig *sbcfg, HttpRangeContainerBlock
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 @@ -573,8 +579,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
4 changes: 2 additions & 2 deletions src/app-layer-htp-range.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ typedef struct HttpRangeContainerBuffer {
/** offset of bytes written in buffer (relative to the start of the range) */
uint64_t offset;
/** number of gaped bytes */
uint64_t gap;
uint32_t gap;
} HttpRangeContainerBuffer;

int HttpRangeContainerBufferCompare(HttpRangeContainerBuffer *a, HttpRangeContainerBuffer *b);
Expand All @@ -63,7 +63,7 @@ typedef struct HttpRangeContainerFile {
/** key length */
uint32_t len;
/** expire time in epoch */
uint32_t expire;
uint64_t expire;
/** pointer to hashtable data, for locking and use count */
THashData *hdata;
/** total expected size of the file in ranges */
Expand Down
16 changes: 6 additions & 10 deletions src/app-layer-htp.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,6 @@ StreamingBufferConfig htp_sbcfg = STREAMING_BUFFER_CONFIG_INITIALIZER;
/** Limit to the number of libhtp messages that can be handled */
#define HTP_MAX_MESSAGES 512

/** a boundary should be smaller in size */
// RFC 2046 states that max boundary size is 70
#define HTP_BOUNDARY_MAX 200U

SC_ATOMIC_DECLARE(uint32_t, htp_config_flags);

#ifdef DEBUG
Expand Down Expand Up @@ -552,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 @@ -956,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 @@ -975,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 @@ -1136,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 @@ -2464,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 @@ -760,8 +760,9 @@ static int SMTPProcessCommandDATA(SMTPState *state, SMTPTransaction *tx, Flow *f
// 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 @@ -781,8 +782,9 @@ static int SMTPProcessCommandDATA(SMTPState *state, SMTPTransaction *tx, Flow *f
// 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 @@ -793,8 +795,9 @@ static int SMTPProcessCommandDATA(SMTPState *state, SMTPTransaction *tx, Flow *f
/* 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 @@ -808,7 +811,8 @@ static int SMTPProcessCommandDATA(SMTPState *state, SMTPTransaction *tx, Flow *f
} 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 @@ -979,17 +983,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 ea77629

Please sign in to comment.