From a1c5e93d4532e197bf611679e796f908387d63a1 Mon Sep 17 00:00:00 2001 From: Jason Ish Date: Mon, 21 Jul 2025 14:48:14 -0600 Subject: [PATCH] util-byte: fix usage of util-byte integer parsers Functions like ByteExtractStringUint8 return 0 or less on failure. Many usages of this function treat 0 as successful as its our common pattern. Ticket: #7836 --- src/detect-dnp3.c | 6 +++--- src/detect-engine-iponly.c | 2 +- src/detect-fast-pattern.c | 6 ++---- src/detect-http2.c | 4 ++-- src/detect-id.c | 2 +- src/output-json-dns.c | 2 +- src/reputation.c | 4 ++-- src/util-affinity.c | 6 +++--- src/util-byte.c | 12 ++++++++++++ src/util-radix4-tree.c | 2 +- src/util-radix6-tree.c | 2 +- 11 files changed, 29 insertions(+), 19 deletions(-) diff --git a/src/detect-dnp3.c b/src/detect-dnp3.c index ca481dc75f..53fe9bd542 100644 --- a/src/detect-dnp3.c +++ b/src/detect-dnp3.c @@ -186,7 +186,7 @@ static InspectionBuffer *GetDNP3Data(DetectEngineThreadCtx *det_ctx, */ static int DetectDNP3FuncParseFunctionCode(const char *str, uint8_t *fc) { - if (StringParseUint8(fc, 10, (uint16_t)strlen(str), str) >= 0) { + if (StringParseUint8(fc, 10, (uint16_t)strlen(str), str) > 0) { return 1; } @@ -335,11 +335,11 @@ static int DetectDNP3ObjParse(const char *str, uint8_t *group, uint8_t *var) *sep = '\0'; varstr = sep + 1; - if (StringParseUint8(group, 0, (uint16_t)strlen(groupstr), groupstr) < 0) { + if (StringParseUint8(group, 0, (uint16_t)strlen(groupstr), groupstr) <= 0) { return 0; } - if (StringParseUint8(var, 0, (uint16_t)strlen(varstr), varstr) < 0) { + if (StringParseUint8(var, 0, (uint16_t)strlen(varstr), varstr) <= 0) { return 0; } diff --git a/src/detect-engine-iponly.c b/src/detect-engine-iponly.c index f5aa6fbd94..b498103b38 100644 --- a/src/detect-engine-iponly.c +++ b/src/detect-engine-iponly.c @@ -279,7 +279,7 @@ static int IPOnlyCIDRItemParseSingle(IPOnlyCIDRItem **pdd, const char *str) } uint8_t cidr; - if (StringParseU8RangeCheck(&cidr, 10, 0, (const char *)mask, 0, 32) < 0) + if (StringParseU8RangeCheck(&cidr, 10, 0, (const char *)mask, 0, 32) <= 0) goto error; dd->netmask = cidr; diff --git a/src/detect-fast-pattern.c b/src/detect-fast-pattern.c index 19fdeba98a..5aca0e3766 100644 --- a/src/detect-fast-pattern.c +++ b/src/detect-fast-pattern.c @@ -319,8 +319,7 @@ static int DetectFastPatternSetup(DetectEngineCtx *de_ctx, Signature *s, const c goto error; } uint16_t offset; - if (StringParseUint16(&offset, 10, 0, - (const char *)arg_substr) < 0) { + if (StringParseUint16(&offset, 10, 0, (const char *)arg_substr) <= 0) { SCLogError("Invalid fast pattern offset:" " \"%s\"", arg_substr); @@ -335,8 +334,7 @@ static int DetectFastPatternSetup(DetectEngineCtx *de_ctx, Signature *s, const c goto error; } uint16_t length; - if (StringParseUint16(&length, 10, 0, - (const char *)arg_substr) < 0) { + if (StringParseUint16(&length, 10, 0, (const char *)arg_substr) <= 0) { SCLogError("Invalid value for fast " "pattern: \"%s\"", arg_substr); diff --git a/src/detect-http2.c b/src/detect-http2.c index ea65d9131c..181fdf537f 100644 --- a/src/detect-http2.c +++ b/src/detect-http2.c @@ -211,7 +211,7 @@ static int DetectHTTP2frametypeMatch(DetectEngineThreadCtx *det_ctx, static int DetectHTTP2FuncParseFrameType(const char *str, uint8_t *ft) { // first parse numeric value - if (ByteExtractStringUint8(ft, 10, (uint16_t)strlen(str), str) >= 0) { + if (ByteExtractStringUint8(ft, 10, (uint16_t)strlen(str), str) > 0) { return 1; } @@ -291,7 +291,7 @@ static int DetectHTTP2errorcodeMatch(DetectEngineThreadCtx *det_ctx, static int DetectHTTP2FuncParseErrorCode(const char *str, uint32_t *ec) { // first parse numeric value - if (ByteExtractStringUint32(ec, 10, (uint16_t)strlen(str), str) >= 0) { + if (ByteExtractStringUint32(ec, 10, (uint16_t)strlen(str), str) > 0) { return 1; } diff --git a/src/detect-id.c b/src/detect-id.c index bd2525901b..fd10300f55 100644 --- a/src/detect-id.c +++ b/src/detect-id.c @@ -156,7 +156,7 @@ static DetectIdData *DetectIdParse (const char *idstr) } /* ok, fill the id data */ - if (StringParseUint16(&temp, 10, 0, (const char *)tmp_str) < 0) { + if (StringParseUint16(&temp, 10, 0, (const char *)tmp_str) <= 0) { SCLogError("invalid id option '%s'", tmp_str); goto error; } diff --git a/src/output-json-dns.c b/src/output-json-dns.c index ec403ff853..03b6aa5ffd 100644 --- a/src/output-json-dns.c +++ b/src/output-json-dns.c @@ -571,7 +571,7 @@ static uint8_t GetDnsLogVersion(SCConfNode *conf) } uint8_t version; - if (StringParseUint8(&version, 10, 0, version_string) >= 0) { + if (StringParseUint8(&version, 10, 0, version_string) > 0) { return version; } SCLogWarning("Failed to parse EVE DNS log version of \"%s\"", version_string); diff --git a/src/reputation.c b/src/reputation.c index c0077fe0b2..f6750743fe 100644 --- a/src/reputation.c +++ b/src/reputation.c @@ -298,10 +298,10 @@ static int SRepSplitLine(SRepCIDRTree *cidr_ctx, char *line, Address *ip, uint8_ return 1; uint8_t c, v; - if (StringParseU8RangeCheck(&c, 10, 0, (const char *)ptrs[1], 0, SREP_MAX_CATS - 1) < 0) + if (StringParseU8RangeCheck(&c, 10, 0, (const char *)ptrs[1], 0, SREP_MAX_CATS - 1) <= 0) return -1; - if (StringParseU8RangeCheck(&v, 10, 0, (const char *)ptrs[2], 0, SREP_MAX_VAL) < 0) + if (StringParseU8RangeCheck(&v, 10, 0, (const char *)ptrs[2], 0, SREP_MAX_VAL) <= 0) return -1; if (strchr(ptrs[0], '/') != NULL) { diff --git a/src/util-affinity.c b/src/util-affinity.c index 1b65226b09..8bc43a16f5 100644 --- a/src/util-affinity.c +++ b/src/util-affinity.c @@ -242,11 +242,11 @@ int BuildCpusetWithCallback( stop = 1; } else if (strchr(lnode->val, '-') != NULL) { char *sep = strchr(lnode->val, '-'); - if (StringParseUint32(&a, 10, sep - lnode->val, lnode->val) < 0) { + if (StringParseUint32(&a, 10, sep - lnode->val, lnode->val) <= 0) { SCLogError("%s: invalid cpu range (start invalid): \"%s\"", name, lnode->val); return -1; } - if (StringParseUint32(&b, 10, strlen(sep) - 1, sep + 1) < 0) { + if (StringParseUint32(&b, 10, strlen(sep) - 1, sep + 1) <= 0) { SCLogError("%s: invalid cpu range (end invalid): \"%s\"", name, lnode->val); return -1; } @@ -260,7 +260,7 @@ int BuildCpusetWithCallback( return -1; } } else { - if (StringParseUint32(&a, 10, strlen(lnode->val), lnode->val) < 0) { + if (StringParseUint32(&a, 10, strlen(lnode->val), lnode->val) <= 0) { SCLogError("%s: invalid cpu range (not an integer): \"%s\"", name, lnode->val); return -1; } diff --git a/src/util-byte.c b/src/util-byte.c index def6d087c4..a0cb189460 100644 --- a/src/util-byte.c +++ b/src/util-byte.c @@ -140,6 +140,9 @@ int ByteExtractUint64(uint64_t *res, int e, uint16_t len, const uint8_t *bytes) return ret; } +/** + * \retval Greater than 0 if successful, 0 or negative on failure. + */ int ByteExtractUint32(uint32_t *res, int e, uint16_t len, const uint8_t *bytes) { uint64_t i64; @@ -334,6 +337,9 @@ int StringParseUint32(uint32_t *res, int base, size_t len, const char *str) return ret; } +/** + * \retval Greater than 0 if successful, 0 or negative on failure. + */ int StringParseUint16(uint16_t *res, int base, size_t len, const char *str) { uint64_t i64; @@ -358,6 +364,9 @@ int StringParseUint16(uint16_t *res, int base, size_t len, const char *str) return ret; } +/** + * \retval Greater than 0 if successful, 0 or negative on failure. + */ int StringParseUint8(uint8_t *res, int base, size_t len, const char *str) { uint64_t i64; @@ -459,6 +468,9 @@ int StringParseU16RangeCheck( return ret; } +/** + * \retval Greater than 0 if successful, 0 or negative on failure. + */ int StringParseU8RangeCheck( uint8_t *res, int base, size_t len, const char *str, uint8_t min, uint8_t max) { diff --git a/src/util-radix4-tree.c b/src/util-radix4-tree.c index 30031dc183..942023b81d 100644 --- a/src/util-radix4-tree.c +++ b/src/util-radix4-tree.c @@ -248,7 +248,7 @@ bool SCRadix4AddKeyIPV4String( } uint8_t cidr; - if (StringParseU8RangeCheck(&cidr, 10, 0, (const char *)mask_str, 0, 32) < 0) { + if (StringParseU8RangeCheck(&cidr, 10, 0, (const char *)mask_str, 0, 32) <= 0) { sc_errno = SC_EINVAL; return false; } diff --git a/src/util-radix6-tree.c b/src/util-radix6-tree.c index 27e917e8a1..eddbd39d53 100644 --- a/src/util-radix6-tree.c +++ b/src/util-radix6-tree.c @@ -281,7 +281,7 @@ bool SCRadix6AddKeyIPV6String( } uint8_t cidr; - if (StringParseU8RangeCheck(&cidr, 10, 0, (const char *)mask_str, 0, 128) < 0) { + if (StringParseU8RangeCheck(&cidr, 10, 0, (const char *)mask_str, 0, 128) <= 0) { sc_errno = SC_EINVAL; return false; }