From 8f107c82528888e08065dad8b570d2c224d471c2 Mon Sep 17 00:00:00 2001 From: Jeff Lucovsky Date: Tue, 9 Jul 2024 09:39:17 -0400 Subject: [PATCH] detect: Clear errno before strtoul Per the notes for strtoul, since 0 or ULONG_MAX is a legitimate return value, errno must be cleared before the call so an error can be checked after the call by testing errno. Issue: 7126 --- src/detect-gid.c | 5 +++-- src/detect-rev.c | 5 +++-- src/detect-sid.c | 5 +++-- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/detect-gid.c b/src/detect-gid.c index a278edbc26..e131b53591 100644 --- a/src/detect-gid.c +++ b/src/detect-gid.c @@ -73,8 +73,9 @@ static int DetectGidSetup (DetectEngineCtx *de_ctx, Signature *s, const char *ra { unsigned long gid = 0; char *endptr = NULL; + errno = 0; gid = strtoul(rawstr, &endptr, 10); - if (endptr == NULL || *endptr != '\0') { + if (errno == ERANGE || endptr == NULL || *endptr != '\0') { SCLogError("invalid character as arg " "to gid keyword"); goto error; @@ -154,4 +155,4 @@ static void GidRegisterTests(void) UtRegisterTest("GidTestParse02", GidTestParse02); UtRegisterTest("GidTestParse03", GidTestParse03); } -#endif /* UNITTESTS */ \ No newline at end of file +#endif /* UNITTESTS */ diff --git a/src/detect-rev.c b/src/detect-rev.c index dda513cc0a..6f0f94b130 100644 --- a/src/detect-rev.c +++ b/src/detect-rev.c @@ -43,8 +43,9 @@ static int DetectRevSetup (DetectEngineCtx *de_ctx, Signature *s, const char *ra { unsigned long rev = 0; char *endptr = NULL; + errno = 0; rev = strtoul(rawstr, &endptr, 10); - if (endptr == NULL || *endptr != '\0') { + if (errno == ERANGE || endptr == NULL || *endptr != '\0') { SCLogError("invalid character as arg " "to rev keyword"); goto error; @@ -68,4 +69,4 @@ static int DetectRevSetup (DetectEngineCtx *de_ctx, Signature *s, const char *ra error: return -1; -} \ No newline at end of file + } diff --git a/src/detect-sid.c b/src/detect-sid.c index 0002e10663..971be9df46 100644 --- a/src/detect-sid.c +++ b/src/detect-sid.c @@ -53,8 +53,9 @@ static int DetectSidSetup (DetectEngineCtx *de_ctx, Signature *s, const char *si { unsigned long id = 0; char *endptr = NULL; + errno = 0; id = strtoul(sidstr, &endptr, 10); - if (endptr == NULL || *endptr != '\0') { + if (errno == ERANGE || endptr == NULL || *endptr != '\0') { SCLogError("invalid character as arg " "to sid keyword"); goto error; @@ -145,4 +146,4 @@ static void DetectSidRegisterTests(void) UtRegisterTest("SidTestParse03", SidTestParse03); UtRegisterTest("SidTestParse04", SidTestParse04); } -#endif /* UNITTESTS */ \ No newline at end of file +#endif /* UNITTESTS */